2 * Dirac encoding support via libdirac library
3 * Copyright (c) 2005 BBC, Andrew Kennedy <dirac at rd dot bbc dot co dot uk>
4 * Copyright (c) 2006-2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * @file libavcodec/libdiracenc.c
25 * Dirac encoding support via libdirac library; more details about the
26 * Dirac project can be found at http://dirac.sourceforge.net/.
27 * The libdirac_encoder library implements Dirac specification version 2.2
28 * (http://dirac.sourceforge.net/specification.html).
31 #include "libdirac_libschro.h"
38 #include <libdirac_encoder/dirac_encoder.h>
40 /** Dirac encoder private data */
41 typedef struct FfmpegDiracEncoderParams
43 /** Dirac encoder context */
44 dirac_encoder_context_t enc_ctx
;
46 /** frame being encoded */
52 /** Dirac encoder handle */
53 dirac_encoder_t
* p_encoder
;
55 /** input frame buffer */
56 unsigned char *p_in_frame_buf
;
58 /** buffer to store encoder output before writing it to the frame queue */
59 unsigned char *enc_buf
;
61 /** size of encoder buffer */
64 /** queue storing encoded frames */
65 FfmpegDiracSchroQueue enc_frame_queue
;
67 /** end of sequence signalled by user, 0 - false, 1 - true */
70 /** end of sequence returned by encoder, 0 - false, 1 - true */
72 } FfmpegDiracEncoderParams
;
75 * Works out Dirac-compatible chroma format.
77 static dirac_chroma_t
GetDiracChromaFormat(enum PixelFormat ff_pix_fmt
)
79 int num_formats
= sizeof(ffmpeg_dirac_pixel_format_map
) /
80 sizeof(ffmpeg_dirac_pixel_format_map
[0]);
83 for (idx
= 0; idx
< num_formats
; ++idx
) {
84 if (ffmpeg_dirac_pixel_format_map
[idx
].ff_pix_fmt
== ff_pix_fmt
) {
85 return ffmpeg_dirac_pixel_format_map
[idx
].dirac_pix_fmt
;
92 * Dirac video preset table. Ensure that this tables matches up correctly
93 * with the ff_dirac_schro_video_format_info table in libdirac_libschro.c.
95 static const VideoFormat ff_dirac_video_formats
[]={
97 VIDEO_FORMAT_QSIF525
,
101 VIDEO_FORMAT_4SIF525
,
103 VIDEO_FORMAT_SD_480I60
,
104 VIDEO_FORMAT_SD_576I50
,
105 VIDEO_FORMAT_HD_720P60
,
106 VIDEO_FORMAT_HD_720P50
,
107 VIDEO_FORMAT_HD_1080I60
,
108 VIDEO_FORMAT_HD_1080I50
,
109 VIDEO_FORMAT_HD_1080P60
,
110 VIDEO_FORMAT_HD_1080P50
,
111 VIDEO_FORMAT_DIGI_CINEMA_2K24
,
112 VIDEO_FORMAT_DIGI_CINEMA_4K24
,
116 * Returns the video format preset matching the input video dimensions and
119 static VideoFormat
GetDiracVideoFormatPreset (AVCodecContext
*avccontext
)
121 unsigned int num_formats
= sizeof(ff_dirac_video_formats
) /
122 sizeof(ff_dirac_video_formats
[0]);
124 unsigned int idx
= ff_dirac_schro_get_video_format_idx (avccontext
);
126 return (idx
< num_formats
) ?
127 ff_dirac_video_formats
[idx
] : VIDEO_FORMAT_CUSTOM
;
130 static av_cold
int libdirac_encode_init(AVCodecContext
*avccontext
)
133 FfmpegDiracEncoderParams
* p_dirac_params
= avccontext
->priv_data
;
135 int verbose
= avccontext
->debug
;
138 /* get Dirac preset */
139 preset
= GetDiracVideoFormatPreset(avccontext
);
141 /* initialize the encoder context */
142 dirac_encoder_context_init (&(p_dirac_params
->enc_ctx
), preset
);
144 p_dirac_params
->enc_ctx
.src_params
.chroma
=
145 GetDiracChromaFormat(avccontext
->pix_fmt
);
147 if (p_dirac_params
->enc_ctx
.src_params
.chroma
== formatNK
) {
148 av_log (avccontext
, AV_LOG_ERROR
,
149 "Unsupported pixel format %d. This codec supports only "
150 "Planar YUV formats (yuv420p, yuv422p, yuv444p\n",
151 avccontext
->pix_fmt
);
155 p_dirac_params
->enc_ctx
.src_params
.frame_rate
.numerator
=
156 avccontext
->time_base
.den
;
157 p_dirac_params
->enc_ctx
.src_params
.frame_rate
.denominator
=
158 avccontext
->time_base
.num
;
160 p_dirac_params
->enc_ctx
.src_params
.width
= avccontext
->width
;
161 p_dirac_params
->enc_ctx
.src_params
.height
= avccontext
->height
;
163 p_dirac_params
->frame_size
= avpicture_get_size(avccontext
->pix_fmt
,
167 avccontext
->coded_frame
= &p_dirac_params
->picture
;
170 p_dirac_params
->enc_ctx
.decode_flag
= 0;
171 p_dirac_params
->enc_ctx
.instr_flag
= 0;
173 p_dirac_params
->enc_ctx
.decode_flag
= 1;
174 p_dirac_params
->enc_ctx
.instr_flag
= 1;
177 /* Intra-only sequence */
178 if (avccontext
->gop_size
== 0 ) {
179 p_dirac_params
->enc_ctx
.enc_params
.num_L1
= 0;
180 if (avccontext
->coder_type
== FF_CODER_TYPE_VLC
)
181 p_dirac_params
->enc_ctx
.enc_params
.using_ac
= 0;
183 avccontext
->has_b_frames
= 1;
185 if (avccontext
->flags
& CODEC_FLAG_QSCALE
) {
186 if (avccontext
->global_quality
!= 0) {
187 p_dirac_params
->enc_ctx
.enc_params
.qf
=
188 avccontext
->global_quality
/ (FF_QP2LAMBDA
*10.0);
189 /* if it is not default bitrate then send target rate. */
190 if (avccontext
->bit_rate
>= 1000 &&
191 avccontext
->bit_rate
!= 200000) {
192 p_dirac_params
->enc_ctx
.enc_params
.trate
=
193 avccontext
->bit_rate
/ 1000;
196 p_dirac_params
->enc_ctx
.enc_params
.lossless
= 1;
197 } else if (avccontext
->bit_rate
>= 1000)
198 p_dirac_params
->enc_ctx
.enc_params
.trate
= avccontext
->bit_rate
/ 1000;
200 if ((preset
> VIDEO_FORMAT_QCIF
|| preset
< VIDEO_FORMAT_QSIF525
) &&
201 avccontext
->bit_rate
== 200000) {
202 p_dirac_params
->enc_ctx
.enc_params
.trate
= 0;
205 if (avccontext
->flags
& CODEC_FLAG_INTERLACED_ME
) {
206 /* all material can be coded as interlaced or progressive
207 * irrespective of the type of source material */
208 p_dirac_params
->enc_ctx
.enc_params
.picture_coding_mode
= 1;
211 p_dirac_params
->p_encoder
= dirac_encoder_init (&(p_dirac_params
->enc_ctx
),
214 if (!p_dirac_params
->p_encoder
) {
215 av_log(avccontext
, AV_LOG_ERROR
,
216 "Unrecoverable Error: dirac_encoder_init failed. ");
220 /* allocate enough memory for the incoming data */
221 p_dirac_params
->p_in_frame_buf
= av_malloc(p_dirac_params
->frame_size
);
223 /* initialize the encoded frame queue */
224 ff_dirac_schro_queue_init(&p_dirac_params
->enc_frame_queue
);
229 static void DiracFreeFrame (void *data
)
231 FfmpegDiracSchroEncodedFrame
*enc_frame
= data
;
233 av_freep (&(enc_frame
->p_encbuf
));
237 static int libdirac_encode_frame(AVCodecContext
*avccontext
,
238 unsigned char *frame
,
239 int buf_size
, void *data
)
242 dirac_encoder_state_t state
;
243 FfmpegDiracEncoderParams
* p_dirac_params
= avccontext
->priv_data
;
244 FfmpegDiracSchroEncodedFrame
* p_frame_output
= NULL
;
245 FfmpegDiracSchroEncodedFrame
* p_next_output_frame
= NULL
;
247 int last_frame_in_sequence
= 0;
250 /* push end of sequence if not already signalled */
251 if (!p_dirac_params
->eos_signalled
) {
252 dirac_encoder_end_sequence( p_dirac_params
->p_encoder
);
253 p_dirac_params
->eos_signalled
= 1;
257 /* Allocate frame data to Dirac input buffer.
258 * Input line size may differ from what the codec supports,
259 * especially when transcoding from one format to another.
260 * So use avpicture_layout to copy the frame. */
261 avpicture_layout ((AVPicture
*)data
, avccontext
->pix_fmt
,
262 avccontext
->width
, avccontext
->height
,
263 p_dirac_params
->p_in_frame_buf
,
264 p_dirac_params
->frame_size
);
266 /* load next frame */
267 if (dirac_encoder_load (p_dirac_params
->p_encoder
,
268 p_dirac_params
->p_in_frame_buf
,
269 p_dirac_params
->frame_size
) < 0) {
270 av_log(avccontext
, AV_LOG_ERROR
, "Unrecoverable Encoder Error."
271 " dirac_encoder_load failed...\n");
276 if (p_dirac_params
->eos_pulled
)
280 p_dirac_params
->p_encoder
->enc_buf
.buffer
= frame
;
281 p_dirac_params
->p_encoder
->enc_buf
.size
= buf_size
;
283 state
= dirac_encoder_output ( p_dirac_params
->p_encoder
);
287 case ENC_STATE_AVAIL
:
289 assert (p_dirac_params
->p_encoder
->enc_buf
.size
> 0);
291 /* All non-frame data is prepended to actual frame data to
292 * be able to set the pts correctly. So we don't write data
293 * to the frame output queue until we actually have a frame
296 p_dirac_params
->enc_buf
= av_realloc (
297 p_dirac_params
->enc_buf
,
298 p_dirac_params
->enc_buf_size
+
299 p_dirac_params
->p_encoder
->enc_buf
.size
301 memcpy(p_dirac_params
->enc_buf
+ p_dirac_params
->enc_buf_size
,
302 p_dirac_params
->p_encoder
->enc_buf
.buffer
,
303 p_dirac_params
->p_encoder
->enc_buf
.size
);
305 p_dirac_params
->enc_buf_size
+=
306 p_dirac_params
->p_encoder
->enc_buf
.size
;
308 if (state
== ENC_STATE_EOS
) {
309 p_dirac_params
->eos_pulled
= 1;
313 /* If non-frame data, don't output it until it we get an
314 * encoded frame back from the encoder. */
315 if (p_dirac_params
->p_encoder
->enc_pparams
.pnum
== -1)
318 /* create output frame */
319 p_frame_output
= av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame
));
320 /* set output data */
321 p_frame_output
->size
= p_dirac_params
->enc_buf_size
;
322 p_frame_output
->p_encbuf
= p_dirac_params
->enc_buf
;
323 p_frame_output
->frame_num
=
324 p_dirac_params
->p_encoder
->enc_pparams
.pnum
;
326 if (p_dirac_params
->p_encoder
->enc_pparams
.ptype
== INTRA_PICTURE
&&
327 p_dirac_params
->p_encoder
->enc_pparams
.rtype
== REFERENCE_PICTURE
)
328 p_frame_output
->key_frame
= 1;
330 ff_dirac_schro_queue_push_back (&p_dirac_params
->enc_frame_queue
,
333 p_dirac_params
->enc_buf_size
= 0;
334 p_dirac_params
->enc_buf
= NULL
;
337 case ENC_STATE_BUFFER
:
341 case ENC_STATE_INVALID
:
342 av_log(avccontext
, AV_LOG_ERROR
,
343 "Unrecoverable Dirac Encoder Error. Quitting...\n");
347 av_log(avccontext
, AV_LOG_ERROR
, "Unknown Dirac Encoder state\n");
352 /* copy 'next' frame in queue */
354 if (p_dirac_params
->enc_frame_queue
.size
== 1 &&
355 p_dirac_params
->eos_pulled
)
356 last_frame_in_sequence
= 1;
358 p_next_output_frame
=
359 ff_dirac_schro_queue_pop(&p_dirac_params
->enc_frame_queue
);
361 if (p_next_output_frame
== NULL
)
364 memcpy(frame
, p_next_output_frame
->p_encbuf
, p_next_output_frame
->size
);
365 avccontext
->coded_frame
->key_frame
= p_next_output_frame
->key_frame
;
366 /* Use the frame number of the encoded frame as the pts. It is OK to do
367 * so since Dirac is a constant framerate codec. It expects input to be
368 * of constant framerate. */
369 avccontext
->coded_frame
->pts
= p_next_output_frame
->frame_num
;
370 enc_size
= p_next_output_frame
->size
;
372 /* Append the end of sequence information to the last frame in the
374 if (last_frame_in_sequence
&& p_dirac_params
->enc_buf_size
> 0)
376 memcpy (frame
+ enc_size
, p_dirac_params
->enc_buf
,
377 p_dirac_params
->enc_buf_size
);
378 enc_size
+= p_dirac_params
->enc_buf_size
;
379 av_freep (&p_dirac_params
->enc_buf
);
380 p_dirac_params
->enc_buf_size
= 0;
384 DiracFreeFrame(p_next_output_frame
);
389 static av_cold
int libdirac_encode_close(AVCodecContext
*avccontext
)
391 FfmpegDiracEncoderParams
* p_dirac_params
= avccontext
->priv_data
;
393 /* close the encoder */
394 dirac_encoder_close(p_dirac_params
->p_encoder
);
396 /* free data in the output frame queue */
397 ff_dirac_schro_queue_free(&p_dirac_params
->enc_frame_queue
,
400 /* free the encoder buffer */
401 if (p_dirac_params
->enc_buf_size
)
402 av_freep(&p_dirac_params
->enc_buf
);
404 /* free the input frame buffer */
405 av_freep(&p_dirac_params
->p_in_frame_buf
);
411 AVCodec libdirac_encoder
= {
415 sizeof(FfmpegDiracEncoderParams
),
416 libdirac_encode_init
,
417 libdirac_encode_frame
,
418 libdirac_encode_close
,
419 .capabilities
= CODEC_CAP_DELAY
,
420 .pix_fmts
= (enum PixelFormat
[]){PIX_FMT_YUV420P
, PIX_FMT_YUV422P
, PIX_FMT_YUV444P
, PIX_FMT_NONE
},
421 .long_name
= NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),