00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #undef NDEBUG
00031 #include <assert.h>
00032
00033 #include <schroedinger/schro.h>
00034 #include <schroedinger/schrodebug.h>
00035 #include <schroedinger/schrovideoformat.h>
00036
00037 #include "avcodec.h"
00038 #include "internal.h"
00039 #include "libschroedinger.h"
00040 #include "bytestream.h"
00041
00042
00044 typedef struct SchroEncoderParams {
00046 SchroVideoFormat *format;
00047
00049 SchroFrameFormat frame_format;
00050
00052 AVFrame picture;
00053
00055 int frame_size;
00056
00058 SchroEncoder* encoder;
00059
00061 unsigned char *enc_buf;
00062
00064 int enc_buf_size;
00065
00067 FFSchroQueue enc_frame_queue;
00068
00070 int eos_signalled;
00071
00073 int eos_pulled;
00074
00075
00076 int64_t dts;
00077 } SchroEncoderParams;
00078
00082 static int set_chroma_format(AVCodecContext *avccontext)
00083 {
00084 int num_formats = sizeof(schro_pixel_format_map) /
00085 sizeof(schro_pixel_format_map[0]);
00086 int idx;
00087
00088 SchroEncoderParams *p_schro_params = avccontext->priv_data;
00089
00090 for (idx = 0; idx < num_formats; ++idx) {
00091 if (schro_pixel_format_map[idx].ff_pix_fmt ==
00092 avccontext->pix_fmt) {
00093 p_schro_params->format->chroma_format =
00094 schro_pixel_format_map[idx].schro_pix_fmt;
00095 return 0;
00096 }
00097 }
00098
00099 av_log(avccontext, AV_LOG_ERROR,
00100 "This codec currently only supports planar YUV 4:2:0, 4:2:2"
00101 " and 4:4:4 formats.\n");
00102
00103 return -1;
00104 }
00105
00106 static int libschroedinger_encode_init(AVCodecContext *avccontext)
00107 {
00108 SchroEncoderParams *p_schro_params = avccontext->priv_data;
00109 SchroVideoFormatEnum preset;
00110
00111
00112 schro_init();
00113
00114
00115 p_schro_params->encoder = schro_encoder_new();
00116
00117 if (!p_schro_params->encoder) {
00118 av_log(avccontext, AV_LOG_ERROR,
00119 "Unrecoverable Error: schro_encoder_new failed. ");
00120 return -1;
00121 }
00122
00123
00124 preset = ff_get_schro_video_format_preset(avccontext);
00125 p_schro_params->format =
00126 schro_encoder_get_video_format(p_schro_params->encoder);
00127 schro_video_format_set_std_video_format(p_schro_params->format, preset);
00128 p_schro_params->format->width = avccontext->width;
00129 p_schro_params->format->height = avccontext->height;
00130
00131 if (set_chroma_format(avccontext) == -1)
00132 return -1;
00133
00134 if (avccontext->color_primaries == AVCOL_PRI_BT709) {
00135 p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_HDTV;
00136 } else if (avccontext->color_primaries == AVCOL_PRI_BT470BG) {
00137 p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_625;
00138 } else if (avccontext->color_primaries == AVCOL_PRI_SMPTE170M) {
00139 p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_525;
00140 }
00141
00142 if (avccontext->colorspace == AVCOL_SPC_BT709) {
00143 p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_HDTV;
00144 } else if (avccontext->colorspace == AVCOL_SPC_BT470BG) {
00145 p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_SDTV;
00146 }
00147
00148 if (avccontext->color_trc == AVCOL_TRC_BT709) {
00149 p_schro_params->format->transfer_function = SCHRO_TRANSFER_CHAR_TV_GAMMA;
00150 }
00151
00152 if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
00153 &p_schro_params->frame_format) == -1) {
00154 av_log(avccontext, AV_LOG_ERROR,
00155 "This codec currently supports only planar YUV 4:2:0, 4:2:2"
00156 " and 4:4:4 formats.\n");
00157 return -1;
00158 }
00159
00160 p_schro_params->format->frame_rate_numerator = avccontext->time_base.den;
00161 p_schro_params->format->frame_rate_denominator = avccontext->time_base.num;
00162
00163 p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
00164 avccontext->width,
00165 avccontext->height);
00166
00167 avccontext->coded_frame = &p_schro_params->picture;
00168
00169 if (!avccontext->gop_size) {
00170 schro_encoder_setting_set_double(p_schro_params->encoder,
00171 "gop_structure",
00172 SCHRO_ENCODER_GOP_INTRA_ONLY);
00173
00174 if (avccontext->coder_type == FF_CODER_TYPE_VLC)
00175 schro_encoder_setting_set_double(p_schro_params->encoder,
00176 "enable_noarith", 1);
00177 } else {
00178 schro_encoder_setting_set_double(p_schro_params->encoder,
00179 "au_distance", avccontext->gop_size);
00180 avccontext->has_b_frames = 1;
00181 p_schro_params->dts = -1;
00182 }
00183
00184
00185 if (avccontext->flags & CODEC_FLAG_QSCALE) {
00186 if (!avccontext->global_quality) {
00187
00188 schro_encoder_setting_set_double(p_schro_params->encoder,
00189 "rate_control",
00190 SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
00191 } else {
00192 int quality;
00193 schro_encoder_setting_set_double(p_schro_params->encoder,
00194 "rate_control",
00195 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY);
00196
00197 quality = avccontext->global_quality / FF_QP2LAMBDA;
00198 if (quality > 10)
00199 quality = 10;
00200 schro_encoder_setting_set_double(p_schro_params->encoder,
00201 "quality", quality);
00202 }
00203 } else {
00204 schro_encoder_setting_set_double(p_schro_params->encoder,
00205 "rate_control",
00206 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
00207
00208 schro_encoder_setting_set_double(p_schro_params->encoder,
00209 "bitrate",
00210 avccontext->bit_rate);
00211
00212 }
00213
00214 if (avccontext->flags & CODEC_FLAG_INTERLACED_ME)
00215
00216
00217 schro_encoder_setting_set_double(p_schro_params->encoder,
00218 "interlaced_coding", 1);
00219
00220 schro_encoder_setting_set_double(p_schro_params->encoder, "open_gop",
00221 !(avccontext->flags & CODEC_FLAG_CLOSED_GOP));
00222
00223
00224
00225 schro_video_format_set_std_signal_range(p_schro_params->format,
00226 SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
00227
00228
00229 schro_encoder_set_video_format(p_schro_params->encoder,
00230 p_schro_params->format);
00231
00232
00233 schro_debug_set_level(avccontext->debug);
00234
00235 schro_encoder_start(p_schro_params->encoder);
00236
00237
00238 ff_schro_queue_init(&p_schro_params->enc_frame_queue);
00239 return 0;
00240 }
00241
00242 static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avccontext,
00243 const AVFrame *frame)
00244 {
00245 SchroEncoderParams *p_schro_params = avccontext->priv_data;
00246 SchroFrame *in_frame;
00247
00248
00249
00250 in_frame = ff_create_schro_frame(avccontext, p_schro_params->frame_format);
00251
00252 if (in_frame)
00253 avpicture_layout((const AVPicture *)frame, avccontext->pix_fmt,
00254 avccontext->width, avccontext->height,
00255 in_frame->components[0].data,
00256 p_schro_params->frame_size);
00257
00258 return in_frame;
00259 }
00260
00261 static void libschroedinger_free_frame(void *data)
00262 {
00263 FFSchroEncodedFrame *enc_frame = data;
00264
00265 av_freep(&enc_frame->p_encbuf);
00266 av_free(enc_frame);
00267 }
00268
00269 static int libschroedinger_encode_frame(AVCodecContext *avccontext, AVPacket *pkt,
00270 const AVFrame *frame, int *got_packet)
00271 {
00272 int enc_size = 0;
00273 SchroEncoderParams *p_schro_params = avccontext->priv_data;
00274 SchroEncoder *encoder = p_schro_params->encoder;
00275 struct FFSchroEncodedFrame *p_frame_output = NULL;
00276 int go = 1;
00277 SchroBuffer *enc_buf;
00278 int presentation_frame;
00279 int parse_code;
00280 int last_frame_in_sequence = 0;
00281 int pkt_size, ret;
00282
00283 if (!frame) {
00284
00285 if (!p_schro_params->eos_signalled) {
00286 schro_encoder_end_of_stream(encoder);
00287 p_schro_params->eos_signalled = 1;
00288 }
00289 } else {
00290
00291 SchroFrame *in_frame = libschroedinger_frame_from_data(avccontext,
00292 frame);
00293
00294 schro_encoder_push_frame(encoder, in_frame);
00295 }
00296
00297 if (p_schro_params->eos_pulled)
00298 go = 0;
00299
00300
00301 while (go) {
00302 SchroStateEnum state;
00303 state = schro_encoder_wait(encoder);
00304 switch (state) {
00305 case SCHRO_STATE_HAVE_BUFFER:
00306 case SCHRO_STATE_END_OF_STREAM:
00307 enc_buf = schro_encoder_pull(encoder, &presentation_frame);
00308 assert(enc_buf->length > 0);
00309 assert(enc_buf->length <= buf_size);
00310 parse_code = enc_buf->data[4];
00311
00312
00313
00314
00315
00316 p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf,
00317 p_schro_params->enc_buf_size + enc_buf->length);
00318
00319 memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
00320 enc_buf->data, enc_buf->length);
00321 p_schro_params->enc_buf_size += enc_buf->length;
00322
00323
00324 if (state == SCHRO_STATE_END_OF_STREAM) {
00325 p_schro_params->eos_pulled = 1;
00326 go = 0;
00327 }
00328
00329 if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
00330 schro_buffer_unref(enc_buf);
00331 break;
00332 }
00333
00334
00335 p_frame_output = av_mallocz(sizeof(FFSchroEncodedFrame));
00336
00337 p_frame_output->size = p_schro_params->enc_buf_size;
00338 p_frame_output->p_encbuf = p_schro_params->enc_buf;
00339 if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
00340 SCHRO_PARSE_CODE_IS_REFERENCE(parse_code))
00341 p_frame_output->key_frame = 1;
00342
00343
00344
00345 p_frame_output->frame_num = AV_RB32(enc_buf->data + 13);
00346
00347 ff_schro_queue_push_back(&p_schro_params->enc_frame_queue,
00348 p_frame_output);
00349 p_schro_params->enc_buf_size = 0;
00350 p_schro_params->enc_buf = NULL;
00351
00352 schro_buffer_unref(enc_buf);
00353
00354 break;
00355
00356 case SCHRO_STATE_NEED_FRAME:
00357 go = 0;
00358 break;
00359
00360 case SCHRO_STATE_AGAIN:
00361 break;
00362
00363 default:
00364 av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
00365 return -1;
00366 }
00367 }
00368
00369
00370
00371 if (p_schro_params->enc_frame_queue.size == 1 &&
00372 p_schro_params->eos_pulled)
00373 last_frame_in_sequence = 1;
00374
00375 p_frame_output = ff_schro_queue_pop(&p_schro_params->enc_frame_queue);
00376
00377 if (!p_frame_output)
00378 return 0;
00379
00380 pkt_size = p_frame_output->size;
00381 if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0)
00382 pkt_size += p_schro_params->enc_buf_size;
00383 if ((ret = ff_alloc_packet2(avccontext, pkt, pkt_size)) < 0)
00384 goto error;
00385
00386 memcpy(pkt->data, p_frame_output->p_encbuf, p_frame_output->size);
00387 avccontext->coded_frame->key_frame = p_frame_output->key_frame;
00388
00389
00390
00391 pkt->pts =
00392 avccontext->coded_frame->pts = p_frame_output->frame_num;
00393 pkt->dts = p_schro_params->dts++;
00394 enc_size = p_frame_output->size;
00395
00396
00397
00398 if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) {
00399 memcpy(pkt->data + enc_size, p_schro_params->enc_buf,
00400 p_schro_params->enc_buf_size);
00401 enc_size += p_schro_params->enc_buf_size;
00402 av_freep(&p_schro_params->enc_buf);
00403 p_schro_params->enc_buf_size = 0;
00404 }
00405
00406 if (p_frame_output->key_frame)
00407 pkt->flags |= AV_PKT_FLAG_KEY;
00408 *got_packet = 1;
00409
00410 error:
00411
00412 libschroedinger_free_frame(p_frame_output);
00413 return ret;
00414 }
00415
00416
00417 static int libschroedinger_encode_close(AVCodecContext *avccontext)
00418 {
00419 SchroEncoderParams *p_schro_params = avccontext->priv_data;
00420
00421
00422 schro_encoder_free(p_schro_params->encoder);
00423
00424
00425 ff_schro_queue_free(&p_schro_params->enc_frame_queue,
00426 libschroedinger_free_frame);
00427
00428
00429
00430 if (p_schro_params->enc_buf_size)
00431 av_freep(&p_schro_params->enc_buf);
00432
00433
00434 av_freep(&p_schro_params->format);
00435
00436 return 0;
00437 }
00438
00439
00440 AVCodec ff_libschroedinger_encoder = {
00441 .name = "libschroedinger",
00442 .type = AVMEDIA_TYPE_VIDEO,
00443 .id = CODEC_ID_DIRAC,
00444 .priv_data_size = sizeof(SchroEncoderParams),
00445 .init = libschroedinger_encode_init,
00446 .encode2 = libschroedinger_encode_frame,
00447 .close = libschroedinger_encode_close,
00448 .capabilities = CODEC_CAP_DELAY,
00449 .pix_fmts = (const enum PixelFormat[]){
00450 PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE
00451 },
00452 .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
00453 };