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 Libav.
7 * Libav 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 * Libav 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 Libav; 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).
33 #include <schroedinger/schro.h>
34 #include <schroedinger/schrodebug.h>
35 #include <schroedinger/schrovideoformat.h>
39 #include "libschroedinger.h"
40 #include "bytestream.h"
43 /** libschroedinger encoder private data */
44 typedef struct SchroEncoderParams
{
45 /** Schroedinger video format */
46 SchroVideoFormat
*format
;
48 /** Schroedinger frame format */
49 SchroFrameFormat frame_format
;
51 /** frame being encoded */
57 /** Schroedinger encoder handle*/
58 SchroEncoder
* encoder
;
60 /** buffer to store encoder output before writing it to the frame queue*/
61 unsigned char *enc_buf
;
63 /** Size of encoder buffer*/
66 /** queue storing encoded frames */
67 FFSchroQueue enc_frame_queue
;
69 /** end of sequence signalled */
72 /** end of sequence pulled */
75 /* counter for frames submitted to encoder, used as dts */
80 * Works out Schro-compatible chroma format.
82 static int set_chroma_format(AVCodecContext
*avccontext
)
84 int num_formats
= sizeof(schro_pixel_format_map
) /
85 sizeof(schro_pixel_format_map
[0]);
88 SchroEncoderParams
*p_schro_params
= avccontext
->priv_data
;
90 for (idx
= 0; idx
< num_formats
; ++idx
) {
91 if (schro_pixel_format_map
[idx
].ff_pix_fmt
==
92 avccontext
->pix_fmt
) {
93 p_schro_params
->format
->chroma_format
=
94 schro_pixel_format_map
[idx
].schro_pix_fmt
;
99 av_log(avccontext
, AV_LOG_ERROR
,
100 "This codec currently only supports planar YUV 4:2:0, 4:2:2"
101 " and 4:4:4 formats.\n");
106 static int libschroedinger_encode_init(AVCodecContext
*avccontext
)
108 SchroEncoderParams
*p_schro_params
= avccontext
->priv_data
;
109 SchroVideoFormatEnum preset
;
111 /* Initialize the libraries that libschroedinger depends on. */
114 /* Create an encoder object. */
115 p_schro_params
->encoder
= schro_encoder_new();
117 if (!p_schro_params
->encoder
) {
118 av_log(avccontext
, AV_LOG_ERROR
,
119 "Unrecoverable Error: schro_encoder_new failed. ");
123 /* Initialize the format. */
124 preset
= ff_get_schro_video_format_preset(avccontext
);
125 p_schro_params
->format
=
126 schro_encoder_get_video_format(p_schro_params
->encoder
);
127 schro_video_format_set_std_video_format(p_schro_params
->format
, preset
);
128 p_schro_params
->format
->width
= avccontext
->width
;
129 p_schro_params
->format
->height
= avccontext
->height
;
131 if (set_chroma_format(avccontext
) == -1)
134 if (avccontext
->color_primaries
== AVCOL_PRI_BT709
) {
135 p_schro_params
->format
->colour_primaries
= SCHRO_COLOUR_PRIMARY_HDTV
;
136 } else if (avccontext
->color_primaries
== AVCOL_PRI_BT470BG
) {
137 p_schro_params
->format
->colour_primaries
= SCHRO_COLOUR_PRIMARY_SDTV_625
;
138 } else if (avccontext
->color_primaries
== AVCOL_PRI_SMPTE170M
) {
139 p_schro_params
->format
->colour_primaries
= SCHRO_COLOUR_PRIMARY_SDTV_525
;
142 if (avccontext
->colorspace
== AVCOL_SPC_BT709
) {
143 p_schro_params
->format
->colour_matrix
= SCHRO_COLOUR_MATRIX_HDTV
;
144 } else if (avccontext
->colorspace
== AVCOL_SPC_BT470BG
) {
145 p_schro_params
->format
->colour_matrix
= SCHRO_COLOUR_MATRIX_SDTV
;
148 if (avccontext
->color_trc
== AVCOL_TRC_BT709
) {
149 p_schro_params
->format
->transfer_function
= SCHRO_TRANSFER_CHAR_TV_GAMMA
;
152 if (ff_get_schro_frame_format(p_schro_params
->format
->chroma_format
,
153 &p_schro_params
->frame_format
) == -1) {
154 av_log(avccontext
, AV_LOG_ERROR
,
155 "This codec currently supports only planar YUV 4:2:0, 4:2:2"
156 " and 4:4:4 formats.\n");
160 p_schro_params
->format
->frame_rate_numerator
= avccontext
->time_base
.den
;
161 p_schro_params
->format
->frame_rate_denominator
= avccontext
->time_base
.num
;
163 p_schro_params
->frame_size
= avpicture_get_size(avccontext
->pix_fmt
,
167 avccontext
->coded_frame
= &p_schro_params
->picture
;
169 if (!avccontext
->gop_size
) {
170 schro_encoder_setting_set_double(p_schro_params
->encoder
,
172 SCHRO_ENCODER_GOP_INTRA_ONLY
);
174 if (avccontext
->coder_type
== FF_CODER_TYPE_VLC
)
175 schro_encoder_setting_set_double(p_schro_params
->encoder
,
176 "enable_noarith", 1);
178 schro_encoder_setting_set_double(p_schro_params
->encoder
,
179 "au_distance", avccontext
->gop_size
);
180 avccontext
->has_b_frames
= 1;
181 p_schro_params
->dts
= -1;
184 /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
185 if (avccontext
->flags
& CODEC_FLAG_QSCALE
) {
186 if (!avccontext
->global_quality
) {
187 /* lossless coding */
188 schro_encoder_setting_set_double(p_schro_params
->encoder
,
190 SCHRO_ENCODER_RATE_CONTROL_LOSSLESS
);
193 schro_encoder_setting_set_double(p_schro_params
->encoder
,
195 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY
);
197 quality
= avccontext
->global_quality
/ FF_QP2LAMBDA
;
200 schro_encoder_setting_set_double(p_schro_params
->encoder
,
204 schro_encoder_setting_set_double(p_schro_params
->encoder
,
206 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE
);
208 schro_encoder_setting_set_double(p_schro_params
->encoder
,
210 avccontext
->bit_rate
);
214 if (avccontext
->flags
& CODEC_FLAG_INTERLACED_ME
)
215 /* All material can be coded as interlaced or progressive
216 irrespective of the type of source material. */
217 schro_encoder_setting_set_double(p_schro_params
->encoder
,
218 "interlaced_coding", 1);
220 schro_encoder_setting_set_double(p_schro_params
->encoder
, "open_gop",
221 !(avccontext
->flags
& CODEC_FLAG_CLOSED_GOP
));
223 /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
224 * and libdirac support other bit-depth data. */
225 schro_video_format_set_std_signal_range(p_schro_params
->format
,
226 SCHRO_SIGNAL_RANGE_8BIT_VIDEO
);
228 /* Set the encoder format. */
229 schro_encoder_set_video_format(p_schro_params
->encoder
,
230 p_schro_params
->format
);
232 /* Set the debug level. */
233 schro_debug_set_level(avccontext
->debug
);
235 schro_encoder_start(p_schro_params
->encoder
);
237 /* Initialize the encoded frame queue. */
238 ff_schro_queue_init(&p_schro_params
->enc_frame_queue
);
242 static SchroFrame
*libschroedinger_frame_from_data(AVCodecContext
*avccontext
,
243 const AVFrame
*frame
)
245 SchroEncoderParams
*p_schro_params
= avccontext
->priv_data
;
246 SchroFrame
*in_frame
;
247 /* Input line size may differ from what the codec supports. Especially
248 * when transcoding from one format to another. So use avpicture_layout
249 * to copy the frame. */
250 in_frame
= ff_create_schro_frame(avccontext
, p_schro_params
->frame_format
);
253 avpicture_layout((const AVPicture
*)frame
, avccontext
->pix_fmt
,
254 avccontext
->width
, avccontext
->height
,
255 in_frame
->components
[0].data
,
256 p_schro_params
->frame_size
);
261 static void libschroedinger_free_frame(void *data
)
263 FFSchroEncodedFrame
*enc_frame
= data
;
265 av_freep(&enc_frame
->p_encbuf
);
269 static int libschroedinger_encode_frame(AVCodecContext
*avccontext
, AVPacket
*pkt
,
270 const AVFrame
*frame
, int *got_packet
)
273 SchroEncoderParams
*p_schro_params
= avccontext
->priv_data
;
274 SchroEncoder
*encoder
= p_schro_params
->encoder
;
275 struct FFSchroEncodedFrame
*p_frame_output
= NULL
;
277 SchroBuffer
*enc_buf
;
278 int presentation_frame
;
280 int last_frame_in_sequence
= 0;
284 /* Push end of sequence if not already signalled. */
285 if (!p_schro_params
->eos_signalled
) {
286 schro_encoder_end_of_stream(encoder
);
287 p_schro_params
->eos_signalled
= 1;
290 /* Allocate frame data to schro input buffer. */
291 SchroFrame
*in_frame
= libschroedinger_frame_from_data(avccontext
,
293 /* Load next frame. */
294 schro_encoder_push_frame(encoder
, in_frame
);
297 if (p_schro_params
->eos_pulled
)
300 /* Now check to see if we have any output from the encoder. */
302 SchroStateEnum state
;
303 state
= schro_encoder_wait(encoder
);
305 case SCHRO_STATE_HAVE_BUFFER
:
306 case SCHRO_STATE_END_OF_STREAM
:
307 enc_buf
= schro_encoder_pull(encoder
, &presentation_frame
);
308 assert(enc_buf
->length
> 0);
309 assert(enc_buf
->length
<= buf_size
);
310 parse_code
= enc_buf
->data
[4];
312 /* All non-frame data is prepended to actual frame data to
313 * be able to set the pts correctly. So we don't write data
314 * to the frame output queue until we actually have a frame
316 p_schro_params
->enc_buf
= av_realloc(p_schro_params
->enc_buf
,
317 p_schro_params
->enc_buf_size
+ enc_buf
->length
);
319 memcpy(p_schro_params
->enc_buf
+ p_schro_params
->enc_buf_size
,
320 enc_buf
->data
, enc_buf
->length
);
321 p_schro_params
->enc_buf_size
+= enc_buf
->length
;
324 if (state
== SCHRO_STATE_END_OF_STREAM
) {
325 p_schro_params
->eos_pulled
= 1;
329 if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code
)) {
330 schro_buffer_unref(enc_buf
);
334 /* Create output frame. */
335 p_frame_output
= av_mallocz(sizeof(FFSchroEncodedFrame
));
336 /* Set output data. */
337 p_frame_output
->size
= p_schro_params
->enc_buf_size
;
338 p_frame_output
->p_encbuf
= p_schro_params
->enc_buf
;
339 if (SCHRO_PARSE_CODE_IS_INTRA(parse_code
) &&
340 SCHRO_PARSE_CODE_IS_REFERENCE(parse_code
))
341 p_frame_output
->key_frame
= 1;
343 /* Parse the coded frame number from the bitstream. Bytes 14
344 * through 17 represesent the frame number. */
345 p_frame_output
->frame_num
= AV_RB32(enc_buf
->data
+ 13);
347 ff_schro_queue_push_back(&p_schro_params
->enc_frame_queue
,
349 p_schro_params
->enc_buf_size
= 0;
350 p_schro_params
->enc_buf
= NULL
;
352 schro_buffer_unref(enc_buf
);
356 case SCHRO_STATE_NEED_FRAME
:
360 case SCHRO_STATE_AGAIN
:
364 av_log(avccontext
, AV_LOG_ERROR
, "Unknown Schro Encoder state\n");
369 /* Copy 'next' frame in queue. */
371 if (p_schro_params
->enc_frame_queue
.size
== 1 &&
372 p_schro_params
->eos_pulled
)
373 last_frame_in_sequence
= 1;
375 p_frame_output
= ff_schro_queue_pop(&p_schro_params
->enc_frame_queue
);
380 pkt_size
= p_frame_output
->size
;
381 if (last_frame_in_sequence
&& p_schro_params
->enc_buf_size
> 0)
382 pkt_size
+= p_schro_params
->enc_buf_size
;
383 if ((ret
= ff_alloc_packet(pkt
, pkt_size
)) < 0) {
384 av_log(avccontext
, AV_LOG_ERROR
, "Error getting output packet of size %d.\n", pkt_size
);
388 memcpy(pkt
->data
, p_frame_output
->p_encbuf
, p_frame_output
->size
);
389 avccontext
->coded_frame
->key_frame
= p_frame_output
->key_frame
;
390 /* Use the frame number of the encoded frame as the pts. It is OK to
391 * do so since Dirac is a constant frame rate codec. It expects input
392 * to be of constant frame rate. */
394 avccontext
->coded_frame
->pts
= p_frame_output
->frame_num
;
395 pkt
->dts
= p_schro_params
->dts
++;
396 enc_size
= p_frame_output
->size
;
398 /* Append the end of sequence information to the last frame in the
400 if (last_frame_in_sequence
&& p_schro_params
->enc_buf_size
> 0) {
401 memcpy(pkt
->data
+ enc_size
, p_schro_params
->enc_buf
,
402 p_schro_params
->enc_buf_size
);
403 enc_size
+= p_schro_params
->enc_buf_size
;
404 av_freep(&p_schro_params
->enc_buf
);
405 p_schro_params
->enc_buf_size
= 0;
408 if (p_frame_output
->key_frame
)
409 pkt
->flags
|= AV_PKT_FLAG_KEY
;
414 libschroedinger_free_frame(p_frame_output
);
419 static int libschroedinger_encode_close(AVCodecContext
*avccontext
)
421 SchroEncoderParams
*p_schro_params
= avccontext
->priv_data
;
423 /* Close the encoder. */
424 schro_encoder_free(p_schro_params
->encoder
);
426 /* Free data in the output frame queue. */
427 ff_schro_queue_free(&p_schro_params
->enc_frame_queue
,
428 libschroedinger_free_frame
);
431 /* Free the encoder buffer. */
432 if (p_schro_params
->enc_buf_size
)
433 av_freep(&p_schro_params
->enc_buf
);
435 /* Free the video format structure. */
436 av_freep(&p_schro_params
->format
);
442 AVCodec ff_libschroedinger_encoder
= {
443 .name
= "libschroedinger",
444 .type
= AVMEDIA_TYPE_VIDEO
,
445 .id
= AV_CODEC_ID_DIRAC
,
446 .priv_data_size
= sizeof(SchroEncoderParams
),
447 .init
= libschroedinger_encode_init
,
448 .encode2
= libschroedinger_encode_frame
,
449 .close
= libschroedinger_encode_close
,
450 .capabilities
= CODEC_CAP_DELAY
,
451 .pix_fmts
= (const enum AVPixelFormat
[]){
452 AV_PIX_FMT_YUV420P
, AV_PIX_FMT_YUV422P
, AV_PIX_FMT_YUV444P
, AV_PIX_FMT_NONE
454 .long_name
= NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),