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