2 * Dirac encoder support via Schroedinger libraries
3 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Dirac encoder support via libschroedinger-1.0 libraries. More details about
25 * the Schroedinger project can be found at http://www.diracvideo.org/.
26 * The library implements Dirac Specification Version 2.2
27 * (http://dirac.sourceforge.net/specification.html).
30 #include <schroedinger/schro.h>
31 #include <schroedinger/schrodebug.h>
32 #include <schroedinger/schrovideoformat.h>
34 #include "libavutil/attributes.h"
35 #include "libavutil/avassert.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/opt.h"
41 #include "libschroedinger.h"
42 #include "bytestream.h"
45 /** libschroedinger encoder private data */
46 typedef struct SchroEncoderParams {
49 /** Schroedinger video format */
50 SchroVideoFormat *format;
52 /** Schroedinger frame format */
53 SchroFrameFormat frame_format;
58 /** Schroedinger encoder handle*/
59 SchroEncoder* encoder;
61 /** buffer to store encoder output before writing it to the frame queue*/
62 unsigned char *enc_buf;
64 /** Size of encoder buffer*/
67 /** queue storing encoded frames */
68 FFSchroQueue enc_frame_queue;
70 /** end of sequence signalled */
73 /** end of sequence pulled */
76 /* counter for frames submitted to encoder, used as dts */
84 * Works out Schro-compatible chroma format.
86 static int set_chroma_format(AVCodecContext *avctx)
88 int num_formats = sizeof(schro_pixel_format_map) /
89 sizeof(schro_pixel_format_map[0]);
92 SchroEncoderParams *p_schro_params = avctx->priv_data;
94 for (idx = 0; idx < num_formats; ++idx) {
95 if (schro_pixel_format_map[idx].ff_pix_fmt == avctx->pix_fmt) {
96 p_schro_params->format->chroma_format =
97 schro_pixel_format_map[idx].schro_pix_fmt;
102 av_log(avctx, AV_LOG_ERROR,
103 "This codec currently only supports planar YUV 4:2:0, 4:2:2"
104 " and 4:4:4 formats.\n");
109 static av_cold int libschroedinger_encode_init(AVCodecContext *avctx)
111 SchroEncoderParams *p_schro_params = avctx->priv_data;
112 SchroVideoFormatEnum preset;
114 /* Initialize the libraries that libschroedinger depends on. */
117 /* Create an encoder object. */
118 p_schro_params->encoder = schro_encoder_new();
120 if (!p_schro_params->encoder) {
121 av_log(avctx, AV_LOG_ERROR,
122 "Unrecoverable Error: schro_encoder_new failed. ");
126 /* Initialize the format. */
127 preset = ff_get_schro_video_format_preset(avctx);
128 p_schro_params->format =
129 schro_encoder_get_video_format(p_schro_params->encoder);
130 schro_video_format_set_std_video_format(p_schro_params->format, preset);
131 p_schro_params->format->width = avctx->width;
132 p_schro_params->format->height = avctx->height;
134 if (set_chroma_format(avctx) == -1)
137 if (avctx->color_primaries == AVCOL_PRI_BT709) {
138 p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_HDTV;
139 } else if (avctx->color_primaries == AVCOL_PRI_BT470BG) {
140 p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_625;
141 } else if (avctx->color_primaries == AVCOL_PRI_SMPTE170M) {
142 p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_525;
145 if (avctx->colorspace == AVCOL_SPC_BT709) {
146 p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_HDTV;
147 } else if (avctx->colorspace == AVCOL_SPC_BT470BG) {
148 p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_SDTV;
151 if (avctx->color_trc == AVCOL_TRC_BT709) {
152 p_schro_params->format->transfer_function = SCHRO_TRANSFER_CHAR_TV_GAMMA;
155 if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
156 &p_schro_params->frame_format) == -1) {
157 av_log(avctx, AV_LOG_ERROR,
158 "This codec currently supports only planar YUV 4:2:0, 4:2:2"
159 " and 4:4:4 formats.\n");
163 p_schro_params->format->frame_rate_numerator = avctx->time_base.den;
164 p_schro_params->format->frame_rate_denominator = avctx->time_base.num;
166 p_schro_params->frame_size = av_image_get_buffer_size(avctx->pix_fmt,
170 if (!avctx->gop_size) {
171 schro_encoder_setting_set_double(p_schro_params->encoder,
173 SCHRO_ENCODER_GOP_INTRA_ONLY);
175 #if FF_API_CODER_TYPE
176 FF_DISABLE_DEPRECATION_WARNINGS
177 if (avctx->coder_type != FF_CODER_TYPE_VLC)
178 p_schro_params->noarith = 0;
179 FF_ENABLE_DEPRECATION_WARNINGS
181 schro_encoder_setting_set_double(p_schro_params->encoder,
183 p_schro_params->noarith);
185 schro_encoder_setting_set_double(p_schro_params->encoder,
186 "au_distance", avctx->gop_size);
187 avctx->has_b_frames = 1;
188 p_schro_params->dts = -1;
191 /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
192 if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
193 if (!avctx->global_quality) {
194 /* lossless coding */
195 schro_encoder_setting_set_double(p_schro_params->encoder,
197 SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
200 schro_encoder_setting_set_double(p_schro_params->encoder,
202 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY);
204 quality = avctx->global_quality / FF_QP2LAMBDA;
207 schro_encoder_setting_set_double(p_schro_params->encoder,
211 schro_encoder_setting_set_double(p_schro_params->encoder,
213 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
215 schro_encoder_setting_set_double(p_schro_params->encoder,
216 "bitrate", avctx->bit_rate);
219 if (avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
220 /* All material can be coded as interlaced or progressive
221 irrespective of the type of source material. */
222 schro_encoder_setting_set_double(p_schro_params->encoder,
223 "interlaced_coding", 1);
225 schro_encoder_setting_set_double(p_schro_params->encoder, "open_gop",
226 !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
228 /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
229 * and libdirac support other bit-depth data. */
230 schro_video_format_set_std_signal_range(p_schro_params->format,
231 SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
233 /* Set the encoder format. */
234 schro_encoder_set_video_format(p_schro_params->encoder,
235 p_schro_params->format);
237 /* Set the debug level. */
238 schro_debug_set_level(avctx->debug);
240 schro_encoder_start(p_schro_params->encoder);
242 /* Initialize the encoded frame queue. */
243 ff_schro_queue_init(&p_schro_params->enc_frame_queue);
247 static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avctx,
248 const AVFrame *frame)
250 SchroEncoderParams *p_schro_params = avctx->priv_data;
251 SchroFrame *in_frame = ff_create_schro_frame(avctx,
252 p_schro_params->frame_format);
255 /* Copy input data to SchroFrame buffers (they match the ones
256 * referenced by the AVFrame stored in priv) */
257 if (av_frame_copy(in_frame->priv, frame) < 0) {
258 av_log(avctx, AV_LOG_ERROR, "Failed to copy input data\n");
266 static void libschroedinger_free_frame(void *data)
268 FFSchroEncodedFrame *enc_frame = data;
270 av_freep(&enc_frame->p_encbuf);
274 static int libschroedinger_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
275 const AVFrame *frame, int *got_packet)
278 SchroEncoderParams *p_schro_params = avctx->priv_data;
279 SchroEncoder *encoder = p_schro_params->encoder;
280 struct FFSchroEncodedFrame *p_frame_output = NULL;
282 SchroBuffer *enc_buf;
283 int presentation_frame;
285 int last_frame_in_sequence = 0;
289 /* Push end of sequence if not already signalled. */
290 if (!p_schro_params->eos_signalled) {
291 schro_encoder_end_of_stream(encoder);
292 p_schro_params->eos_signalled = 1;
295 /* Allocate frame data to schro input buffer. */
296 SchroFrame *in_frame = libschroedinger_frame_from_data(avctx, frame);
298 return AVERROR(ENOMEM);
299 /* Load next frame. */
300 schro_encoder_push_frame(encoder, in_frame);
303 if (p_schro_params->eos_pulled)
306 /* Now check to see if we have any output from the encoder. */
309 SchroStateEnum state;
310 state = schro_encoder_wait(encoder);
312 case SCHRO_STATE_HAVE_BUFFER:
313 case SCHRO_STATE_END_OF_STREAM:
314 enc_buf = schro_encoder_pull(encoder, &presentation_frame);
315 if (enc_buf->length <= 0)
317 parse_code = enc_buf->data[4];
319 /* All non-frame data is prepended to actual frame data to
320 * be able to set the pts correctly. So we don't write data
321 * to the frame output queue until we actually have a frame
323 if ((err = av_reallocp(&p_schro_params->enc_buf,
324 p_schro_params->enc_buf_size +
325 enc_buf->length)) < 0) {
326 p_schro_params->enc_buf_size = 0;
330 memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
331 enc_buf->data, enc_buf->length);
332 p_schro_params->enc_buf_size += enc_buf->length;
335 if (state == SCHRO_STATE_END_OF_STREAM) {
336 p_schro_params->eos_pulled = 1;
340 if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
341 schro_buffer_unref(enc_buf);
345 /* Create output frame. */
346 p_frame_output = av_mallocz(sizeof(FFSchroEncodedFrame));
348 return AVERROR(ENOMEM);
349 /* Set output data. */
350 p_frame_output->size = p_schro_params->enc_buf_size;
351 p_frame_output->p_encbuf = p_schro_params->enc_buf;
352 if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
353 SCHRO_PARSE_CODE_IS_REFERENCE(parse_code))
354 p_frame_output->key_frame = 1;
356 /* Parse the coded frame number from the bitstream. Bytes 14
357 * through 17 represesent the frame number. */
358 p_frame_output->frame_num = AV_RB32(enc_buf->data + 13);
360 ff_schro_queue_push_back(&p_schro_params->enc_frame_queue,
362 p_schro_params->enc_buf_size = 0;
363 p_schro_params->enc_buf = NULL;
365 schro_buffer_unref(enc_buf);
369 case SCHRO_STATE_NEED_FRAME:
373 case SCHRO_STATE_AGAIN:
377 av_log(avctx, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
382 /* Copy 'next' frame in queue. */
384 if (p_schro_params->enc_frame_queue.size == 1 &&
385 p_schro_params->eos_pulled)
386 last_frame_in_sequence = 1;
388 p_frame_output = ff_schro_queue_pop(&p_schro_params->enc_frame_queue);
393 pkt_size = p_frame_output->size;
394 if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0)
395 pkt_size += p_schro_params->enc_buf_size;
396 if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
399 memcpy(pkt->data, p_frame_output->p_encbuf, p_frame_output->size);
400 #if FF_API_CODED_FRAME
401 FF_DISABLE_DEPRECATION_WARNINGS
402 avctx->coded_frame->key_frame = p_frame_output->key_frame;
403 avctx->coded_frame->pts = p_frame_output->frame_num;
404 FF_ENABLE_DEPRECATION_WARNINGS
406 /* Use the frame number of the encoded frame as the pts. It is OK to
407 * do so since Dirac is a constant frame rate codec. It expects input
408 * to be of constant frame rate. */
409 pkt->pts = p_frame_output->frame_num;
410 pkt->dts = p_schro_params->dts++;
411 enc_size = p_frame_output->size;
413 /* Append the end of sequence information to the last frame in the
415 if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) {
416 memcpy(pkt->data + enc_size, p_schro_params->enc_buf,
417 p_schro_params->enc_buf_size);
418 enc_size += p_schro_params->enc_buf_size;
419 av_freep(&p_schro_params->enc_buf);
420 p_schro_params->enc_buf_size = 0;
423 if (p_frame_output->key_frame)
424 pkt->flags |= AV_PKT_FLAG_KEY;
429 libschroedinger_free_frame(p_frame_output);
434 static int libschroedinger_encode_close(AVCodecContext *avctx)
436 SchroEncoderParams *p_schro_params = avctx->priv_data;
438 /* Close the encoder. */
439 schro_encoder_free(p_schro_params->encoder);
441 /* Free data in the output frame queue. */
442 ff_schro_queue_free(&p_schro_params->enc_frame_queue,
443 libschroedinger_free_frame);
446 /* Free the encoder buffer. */
447 if (p_schro_params->enc_buf_size)
448 av_freep(&p_schro_params->enc_buf);
450 /* Free the video format structure. */
451 av_freep(&p_schro_params->format);
456 #define OFFSET(x) offsetof(SchroEncoderParams, x)
457 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
458 static const AVOption options[] = {
459 { "noarith", "Enable noarith", OFFSET(noarith), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
464 static const AVClass libschroedinger_class = {
465 .class_name = "libschroedinger",
466 .item_name = av_default_item_name,
468 .version = LIBAVUTIL_VERSION_INT,
471 AVCodec ff_libschroedinger_encoder = {
472 .name = "libschroedinger",
473 .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
474 .type = AVMEDIA_TYPE_VIDEO,
475 .id = AV_CODEC_ID_DIRAC,
476 .priv_data_size = sizeof(SchroEncoderParams),
477 .priv_class = &libschroedinger_class,
478 .init = libschroedinger_encode_init,
479 .encode2 = libschroedinger_encode_frame,
480 .close = libschroedinger_encode_close,
481 .capabilities = AV_CODEC_CAP_DELAY,
482 .pix_fmts = (const enum AVPixelFormat[]){
483 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_NONE