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
23 * @file libschroedingerenc.c
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>
38 #include "libdirac_libschro.h"
39 #include "libschroedinger.h"
42 /** libschroedinger encoder private data */
43 typedef struct FfmpegSchroEncoderParams
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 /** queue storing encoded frames */
61 FfmpegDiracSchroQueue enc_frame_queue
;
63 /** end of sequence signalled */
66 /** end of sequence pulled */
68 } FfmpegSchroEncoderParams
;
71 * Works out Schro-compatible chroma format.
73 static int SetSchroChromaFormat(AVCodecContext
*avccontext
)
75 int num_formats
= sizeof(ffmpeg_schro_pixel_format_map
) /
76 sizeof(ffmpeg_schro_pixel_format_map
[0]);
79 FfmpegSchroEncoderParams
* p_schro_params
= avccontext
->priv_data
;
81 for (idx
= 0; idx
< num_formats
; ++idx
) {
82 if (ffmpeg_schro_pixel_format_map
[idx
].ff_pix_fmt
==
83 avccontext
->pix_fmt
) {
84 p_schro_params
->format
->chroma_format
=
85 ffmpeg_schro_pixel_format_map
[idx
].schro_pix_fmt
;
90 av_log (avccontext
, AV_LOG_ERROR
,
91 "This codec currently only supports planar YUV 4:2:0, 4:2:2"
92 " and 4:4:4 formats.\n");
97 static int libschroedinger_encode_init(AVCodecContext
*avccontext
)
99 FfmpegSchroEncoderParams
* p_schro_params
= avccontext
->priv_data
;
100 SchroVideoFormatEnum preset
;
102 /* Initialize the libraries that libschroedinger depends on. */
105 /* Create an encoder object. */
106 p_schro_params
->encoder
= schro_encoder_new();
108 if (!p_schro_params
->encoder
) {
109 av_log(avccontext
, AV_LOG_ERROR
,
110 "Unrecoverable Error: schro_encoder_new failed. ");
114 /* Initialize the format. */
115 preset
= ff_get_schro_video_format_preset(avccontext
);
116 p_schro_params
->format
=
117 schro_encoder_get_video_format(p_schro_params
->encoder
);
118 schro_video_format_set_std_video_format (p_schro_params
->format
, preset
);
119 p_schro_params
->format
->width
= avccontext
->width
;
120 p_schro_params
->format
->height
= avccontext
->height
;
122 if (SetSchroChromaFormat(avccontext
) == -1)
125 if (ff_get_schro_frame_format(p_schro_params
->format
->chroma_format
,
126 &p_schro_params
->frame_format
) == -1) {
127 av_log (avccontext
, AV_LOG_ERROR
,
128 "This codec currently supports only planar YUV 4:2:0, 4:2:2"
129 " and 4:4:4 formats.\n");
133 p_schro_params
->format
->frame_rate_numerator
= avccontext
->time_base
.den
;
134 p_schro_params
->format
->frame_rate_denominator
= avccontext
->time_base
.num
;
136 p_schro_params
->frame_size
= avpicture_get_size(avccontext
->pix_fmt
,
140 avccontext
->coded_frame
= &p_schro_params
->picture
;
142 if (avccontext
->gop_size
== 0){
143 schro_encoder_setting_set_double (p_schro_params
->encoder
,
145 SCHRO_ENCODER_GOP_INTRA_ONLY
);
148 schro_encoder_setting_set_double (p_schro_params
->encoder
,
150 SCHRO_ENCODER_GOP_BIREF
);
151 avccontext
->has_b_frames
= 1;
154 /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
155 if (avccontext
->flags
& CODEC_FLAG_QSCALE
) {
156 if (avccontext
->global_quality
== 0) {
157 /* lossless coding */
158 schro_encoder_setting_set_double (p_schro_params
->encoder
,
160 SCHRO_ENCODER_RATE_CONTROL_LOSSLESS
);
163 schro_encoder_setting_set_double (p_schro_params
->encoder
,
165 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_NOISE_THRESHOLD
);
167 noise_threshold
= avccontext
->global_quality
/FF_QP2LAMBDA
;
168 if (noise_threshold
> 100)
169 noise_threshold
= 100;
170 schro_encoder_setting_set_double (p_schro_params
->encoder
,
176 schro_encoder_setting_set_double ( p_schro_params
->encoder
,
178 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE
);
180 schro_encoder_setting_set_double (p_schro_params
->encoder
,
182 avccontext
->bit_rate
);
186 if (avccontext
->flags
& CODEC_FLAG_INTERLACED_ME
) {
187 /* All material can be coded as interlaced or progressive
188 irrespective of the type of source material. */
189 schro_encoder_setting_set_double (p_schro_params
->encoder
,
190 "interlaced_coding", 1);
193 /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
194 * and libdirac support other bit-depth data. */
195 schro_video_format_set_std_signal_range(p_schro_params
->format
,
196 SCHRO_SIGNAL_RANGE_8BIT_VIDEO
);
199 /* Hardcode motion vector precision to quarter pixel. */
200 schro_encoder_setting_set_double (p_schro_params
->encoder
,
203 /* Set the encoder format. */
204 schro_encoder_set_video_format(p_schro_params
->encoder
,
205 p_schro_params
->format
);
207 /* Set the debug level. */
208 schro_debug_set_level (avccontext
->debug
);
210 schro_encoder_start (p_schro_params
->encoder
);
212 /* Initialize the encoded frame queue. */
213 ff_dirac_schro_queue_init (&p_schro_params
->enc_frame_queue
);
217 static SchroFrame
*libschroedinger_frame_from_data (AVCodecContext
*avccontext
,
220 FfmpegSchroEncoderParams
* p_schro_params
= avccontext
->priv_data
;
221 SchroFrame
*in_frame
;
222 /* Input line size may differ from what the codec supports. Especially
223 * when transcoding from one format to another. So use avpicture_layout
224 * to copy the frame. */
225 in_frame
= schro_frame_new_and_alloc (NULL
,
226 p_schro_params
->frame_format
,
227 p_schro_params
->format
->width
,
228 p_schro_params
->format
->height
);
230 avpicture_layout ((AVPicture
*)in_data
, avccontext
->pix_fmt
,
231 avccontext
->width
, avccontext
->height
,
232 in_frame
->components
[0].data
,
233 p_schro_params
->frame_size
);
238 static void SchroedingerFreeFrame(void *data
)
240 FfmpegDiracSchroEncodedFrame
*enc_frame
= data
;
242 av_freep (&(enc_frame
->p_encbuf
));
246 static int libschroedinger_encode_frame(AVCodecContext
*avccontext
,
247 unsigned char *frame
,
248 int buf_size
, void *data
)
251 FfmpegSchroEncoderParams
* p_schro_params
= avccontext
->priv_data
;
252 SchroEncoder
*encoder
= p_schro_params
->encoder
;
253 struct FfmpegDiracSchroEncodedFrame
* p_frame_output
= NULL
;
255 SchroBuffer
*enc_buf
;
256 int presentation_frame
;
260 /* Push end of sequence if not already signalled. */
261 if (!p_schro_params
->eos_signalled
) {
262 schro_encoder_end_of_stream(encoder
);
263 p_schro_params
->eos_signalled
= 1;
266 /* Allocate frame data to schro input buffer. */
267 SchroFrame
*in_frame
= libschroedinger_frame_from_data (avccontext
,
269 /* Load next frame. */
270 schro_encoder_push_frame(encoder
, in_frame
);
273 if (p_schro_params
->eos_pulled
)
276 /* Now check to see if we have any output from the encoder. */
278 SchroStateEnum state
;
279 state
= schro_encoder_wait(encoder
);
282 case SCHRO_STATE_HAVE_BUFFER
:
283 case SCHRO_STATE_END_OF_STREAM
:
284 enc_buf
= schro_encoder_pull (encoder
,
285 &presentation_frame
);
286 assert (enc_buf
->length
> 0);
287 assert (enc_buf
->length
<= buf_size
);
289 /* Create output frame. */
290 p_frame_output
= av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame
));
291 /* Set output data. */
292 p_frame_output
->size
= enc_buf
->length
;
293 p_frame_output
->p_encbuf
= av_malloc(enc_buf
->length
);
294 memcpy(p_frame_output
->p_encbuf
, enc_buf
->data
, enc_buf
->length
);
296 parse_code
= enc_buf
->data
[4];
297 if (SCHRO_PARSE_CODE_IS_INTRA(parse_code
) &&
298 SCHRO_PARSE_CODE_IS_REFERENCE(parse_code
)) {
299 p_frame_output
->key_frame
= 1;
302 /* Parse the coded frame number from the bitstream. Bytes 14
303 * through 17 represesent the frame number. */
304 if (SCHRO_PARSE_CODE_IS_PICTURE(parse_code
))
306 assert (enc_buf
->length
>= 17);
307 p_frame_output
->frame_num
= (enc_buf
->data
[13] << 24) +
308 (enc_buf
->data
[14] << 16) +
309 (enc_buf
->data
[15] << 8) +
313 ff_dirac_schro_queue_push_back (&p_schro_params
->enc_frame_queue
,
315 schro_buffer_unref (enc_buf
);
317 if (state
== SCHRO_STATE_END_OF_STREAM
) {
318 p_schro_params
->eos_pulled
= 1;
323 case SCHRO_STATE_NEED_FRAME
:
327 case SCHRO_STATE_AGAIN
:
331 av_log(avccontext
, AV_LOG_ERROR
, "Unknown Schro Encoder state\n");
336 /* Copy 'next' frame in queue. */
338 ff_dirac_schro_queue_pop (&p_schro_params
->enc_frame_queue
);
340 if (p_frame_output
== NULL
)
343 memcpy(frame
, p_frame_output
->p_encbuf
, p_frame_output
->size
);
344 avccontext
->coded_frame
->key_frame
= p_frame_output
->key_frame
;
345 /* Use the frame number of the encoded frame as the pts. It is OK to
346 * do so since Dirac is a constant frame rate codec. It expects input
347 * to be of constant frame rate. */
348 avccontext
->coded_frame
->pts
= p_frame_output
->frame_num
;
349 enc_size
= p_frame_output
->size
;
352 SchroedingerFreeFrame (p_frame_output
);
358 static int libschroedinger_encode_close(AVCodecContext
*avccontext
)
361 FfmpegSchroEncoderParams
* p_schro_params
= avccontext
->priv_data
;
363 /* Close the encoder. */
364 schro_encoder_free(p_schro_params
->encoder
);
366 /* Free data in the output frame queue. */
367 ff_dirac_schro_queue_free (&p_schro_params
->enc_frame_queue
,
368 SchroedingerFreeFrame
);
370 /* Free the video format structure. */
371 av_freep(&p_schro_params
->format
);
377 AVCodec libschroedinger_encoder
= {
381 sizeof(FfmpegSchroEncoderParams
),
382 libschroedinger_encode_init
,
383 libschroedinger_encode_frame
,
384 libschroedinger_encode_close
,
385 .capabilities
= CODEC_CAP_DELAY
,
386 .pix_fmts
= (enum PixelFormat
[]){PIX_FMT_YUV420P
, PIX_FMT_YUV422P
, PIX_FMT_YUV444P
, PIX_FMT_NONE
},
387 .long_name
= NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),