libdiracenc.c
Go to the documentation of this file.
1 /*
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 >
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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.
12  *
13  * Libav 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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
31 #include "libdirac_libschro.h"
32 #include "libdirac.h"
33 
34 #undef NDEBUG
35 #include <assert.h>
36 
37 
38 #include <libdirac_encoder/dirac_encoder.h>
39 
41 typedef struct DiracEncoderParams {
43  dirac_encoder_context_t enc_ctx;
44 
47 
50 
52  dirac_encoder_t* p_encoder;
53 
55  unsigned char *p_in_frame_buf;
56 
58  unsigned char *enc_buf;
59 
62 
65 
68 
72 
76 static dirac_chroma_t GetDiracChromaFormat(enum PixelFormat ff_pix_fmt)
77 {
78  int num_formats = sizeof(dirac_pixel_format_map) /
79  sizeof(dirac_pixel_format_map[0]);
80  int idx;
81 
82  for (idx = 0; idx < num_formats; ++idx)
83  if (dirac_pixel_format_map[idx].ff_pix_fmt == ff_pix_fmt)
84  return dirac_pixel_format_map[idx].dirac_pix_fmt;
85  return formatNK;
86 }
87 
92 static const VideoFormat ff_dirac_video_formats[]={
93  VIDEO_FORMAT_CUSTOM ,
94  VIDEO_FORMAT_QSIF525 ,
95  VIDEO_FORMAT_QCIF ,
96  VIDEO_FORMAT_SIF525 ,
97  VIDEO_FORMAT_CIF ,
98  VIDEO_FORMAT_4SIF525 ,
99  VIDEO_FORMAT_4CIF ,
100  VIDEO_FORMAT_SD_480I60 ,
101  VIDEO_FORMAT_SD_576I50 ,
102  VIDEO_FORMAT_HD_720P60 ,
103  VIDEO_FORMAT_HD_720P50 ,
104  VIDEO_FORMAT_HD_1080I60 ,
105  VIDEO_FORMAT_HD_1080I50 ,
106  VIDEO_FORMAT_HD_1080P60 ,
107  VIDEO_FORMAT_HD_1080P50 ,
108  VIDEO_FORMAT_DIGI_CINEMA_2K24 ,
109  VIDEO_FORMAT_DIGI_CINEMA_4K24 ,
110 };
111 
116 static VideoFormat GetDiracVideoFormatPreset(AVCodecContext *avccontext)
117 {
118  unsigned int num_formats = sizeof(ff_dirac_video_formats) /
119  sizeof(ff_dirac_video_formats[0]);
120 
121  unsigned int idx = ff_dirac_schro_get_video_format_idx(avccontext);
122 
123  return (idx < num_formats) ?
124  ff_dirac_video_formats[idx] : VIDEO_FORMAT_CUSTOM;
125 }
126 
128 {
129 
130  DiracEncoderParams* p_dirac_params = avccontext->priv_data;
131  int no_local = 1;
132  int verbose = avccontext->debug;
133  VideoFormat preset;
134 
135  /* get Dirac preset */
136  preset = GetDiracVideoFormatPreset(avccontext);
137 
138  /* initialize the encoder context */
139  dirac_encoder_context_init(&p_dirac_params->enc_ctx, preset);
140 
141  p_dirac_params->enc_ctx.src_params.chroma = GetDiracChromaFormat(avccontext->pix_fmt);
142 
143  if (p_dirac_params->enc_ctx.src_params.chroma == formatNK) {
144  av_log(avccontext, AV_LOG_ERROR,
145  "Unsupported pixel format %d. This codec supports only "
146  "Planar YUV formats (yuv420p, yuv422p, yuv444p\n",
147  avccontext->pix_fmt);
148  return -1;
149  }
150 
151  p_dirac_params->enc_ctx.src_params.frame_rate.numerator = avccontext->time_base.den;
152  p_dirac_params->enc_ctx.src_params.frame_rate.denominator = avccontext->time_base.num;
153 
154  p_dirac_params->enc_ctx.src_params.width = avccontext->width;
155  p_dirac_params->enc_ctx.src_params.height = avccontext->height;
156 
157  p_dirac_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
158  avccontext->width,
159  avccontext->height);
160 
161  avccontext->coded_frame = &p_dirac_params->picture;
162 
163  if (no_local) {
164  p_dirac_params->enc_ctx.decode_flag = 0;
165  p_dirac_params->enc_ctx.instr_flag = 0;
166  } else {
167  p_dirac_params->enc_ctx.decode_flag = 1;
168  p_dirac_params->enc_ctx.instr_flag = 1;
169  }
170 
171  /* Intra-only sequence */
172  if (!avccontext->gop_size) {
173  p_dirac_params->enc_ctx.enc_params.num_L1 = 0;
174  if (avccontext->coder_type == FF_CODER_TYPE_VLC)
175  p_dirac_params->enc_ctx.enc_params.using_ac = 0;
176  } else
177  avccontext->has_b_frames = 1;
178 
179  if (avccontext->flags & CODEC_FLAG_QSCALE) {
180  if (avccontext->global_quality) {
181  p_dirac_params->enc_ctx.enc_params.qf = avccontext->global_quality
182  / (FF_QP2LAMBDA * 10.0);
183  /* if it is not default bitrate then send target rate. */
184  if (avccontext->bit_rate >= 1000 &&
185  avccontext->bit_rate != 200000)
186  p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate
187  / 1000;
188  } else
189  p_dirac_params->enc_ctx.enc_params.lossless = 1;
190  } else if (avccontext->bit_rate >= 1000)
191  p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate / 1000;
192 
193  if ((preset > VIDEO_FORMAT_QCIF || preset < VIDEO_FORMAT_QSIF525) &&
194  avccontext->bit_rate == 200000)
195  p_dirac_params->enc_ctx.enc_params.trate = 0;
196 
197  if (avccontext->flags & CODEC_FLAG_INTERLACED_ME)
198  /* all material can be coded as interlaced or progressive
199  * irrespective of the type of source material */
200  p_dirac_params->enc_ctx.enc_params.picture_coding_mode = 1;
201 
202  p_dirac_params->p_encoder = dirac_encoder_init(&p_dirac_params->enc_ctx,
203  verbose);
204 
205  if (!p_dirac_params->p_encoder) {
206  av_log(avccontext, AV_LOG_ERROR,
207  "Unrecoverable Error: dirac_encoder_init failed. ");
208  return EXIT_FAILURE;
209  }
210 
211  /* allocate enough memory for the incoming data */
212  p_dirac_params->p_in_frame_buf = av_malloc(p_dirac_params->frame_size);
213 
214  /* initialize the encoded frame queue */
215  ff_dirac_schro_queue_init(&p_dirac_params->enc_frame_queue);
216 
217  return 0;
218 }
219 
220 static void DiracFreeFrame(void *data)
221 {
222  DiracSchroEncodedFrame *enc_frame = data;
223 
224  av_freep(&enc_frame->p_encbuf);
225  av_free(enc_frame);
226 }
227 
228 static int libdirac_encode_frame(AVCodecContext *avccontext,
229  unsigned char *frame,
230  int buf_size, void *data)
231 {
232  int enc_size = 0;
233  dirac_encoder_state_t state;
234  DiracEncoderParams *p_dirac_params = avccontext->priv_data;
235  DiracSchroEncodedFrame *p_frame_output = NULL;
236  DiracSchroEncodedFrame *p_next_output_frame = NULL;
237  int go = 1;
238  int last_frame_in_sequence = 0;
239 
240  if (!data) {
241  /* push end of sequence if not already signalled */
242  if (!p_dirac_params->eos_signalled) {
243  dirac_encoder_end_sequence(p_dirac_params->p_encoder);
244  p_dirac_params->eos_signalled = 1;
245  }
246  } else {
247 
248  /* Allocate frame data to Dirac input buffer.
249  * Input line size may differ from what the codec supports,
250  * especially when transcoding from one format to another.
251  * So use avpicture_layout to copy the frame. */
252  avpicture_layout((AVPicture *)data, avccontext->pix_fmt,
253  avccontext->width, avccontext->height,
254  p_dirac_params->p_in_frame_buf,
255  p_dirac_params->frame_size);
256 
257  /* load next frame */
258  if (dirac_encoder_load(p_dirac_params->p_encoder,
259  p_dirac_params->p_in_frame_buf,
260  p_dirac_params->frame_size) < 0) {
261  av_log(avccontext, AV_LOG_ERROR, "Unrecoverable Encoder Error."
262  " dirac_encoder_load failed...\n");
263  return -1;
264  }
265  }
266 
267  if (p_dirac_params->eos_pulled)
268  go = 0;
269 
270  while (go) {
271  p_dirac_params->p_encoder->enc_buf.buffer = frame;
272  p_dirac_params->p_encoder->enc_buf.size = buf_size;
273  /* process frame */
274  state = dirac_encoder_output(p_dirac_params->p_encoder);
275 
276  switch (state) {
277  case ENC_STATE_AVAIL:
278  case ENC_STATE_EOS:
279  assert(p_dirac_params->p_encoder->enc_buf.size > 0);
280 
281  /* All non-frame data is prepended to actual frame data to
282  * be able to set the pts correctly. So we don't write data
283  * to the frame output queue until we actually have a frame
284  */
285 
286  p_dirac_params->enc_buf = av_realloc(p_dirac_params->enc_buf,
287  p_dirac_params->enc_buf_size +
288  p_dirac_params->p_encoder->enc_buf.size);
289  memcpy(p_dirac_params->enc_buf + p_dirac_params->enc_buf_size,
290  p_dirac_params->p_encoder->enc_buf.buffer,
291  p_dirac_params->p_encoder->enc_buf.size);
292 
293  p_dirac_params->enc_buf_size += p_dirac_params->p_encoder->enc_buf.size;
294 
295  if (state == ENC_STATE_EOS) {
296  p_dirac_params->eos_pulled = 1;
297  go = 0;
298  }
299 
300  /* If non-frame data, don't output it until it we get an
301  * encoded frame back from the encoder. */
302  if (p_dirac_params->p_encoder->enc_pparams.pnum == -1)
303  break;
304 
305  /* create output frame */
306  p_frame_output = av_mallocz(sizeof(DiracSchroEncodedFrame));
307  /* set output data */
308  p_frame_output->size = p_dirac_params->enc_buf_size;
309  p_frame_output->p_encbuf = p_dirac_params->enc_buf;
310  p_frame_output->frame_num = p_dirac_params->p_encoder->enc_pparams.pnum;
311 
312  if (p_dirac_params->p_encoder->enc_pparams.ptype == INTRA_PICTURE &&
313  p_dirac_params->p_encoder->enc_pparams.rtype == REFERENCE_PICTURE)
314  p_frame_output->key_frame = 1;
315 
317  p_frame_output);
318 
319  p_dirac_params->enc_buf_size = 0;
320  p_dirac_params->enc_buf = NULL;
321  break;
322 
323  case ENC_STATE_BUFFER:
324  go = 0;
325  break;
326 
327  case ENC_STATE_INVALID:
328  av_log(avccontext, AV_LOG_ERROR,
329  "Unrecoverable Dirac Encoder Error. Quitting...\n");
330  return -1;
331 
332  default:
333  av_log(avccontext, AV_LOG_ERROR, "Unknown Dirac Encoder state\n");
334  return -1;
335  }
336  }
337 
338  /* copy 'next' frame in queue */
339 
340  if (p_dirac_params->enc_frame_queue.size == 1 && p_dirac_params->eos_pulled)
341  last_frame_in_sequence = 1;
342 
343  p_next_output_frame = ff_dirac_schro_queue_pop(&p_dirac_params->enc_frame_queue);
344 
345  if (!p_next_output_frame)
346  return 0;
347 
348  memcpy(frame, p_next_output_frame->p_encbuf, p_next_output_frame->size);
349  avccontext->coded_frame->key_frame = p_next_output_frame->key_frame;
350  /* Use the frame number of the encoded frame as the pts. It is OK to do
351  * so since Dirac is a constant framerate codec. It expects input to be
352  * of constant framerate. */
353  avccontext->coded_frame->pts = p_next_output_frame->frame_num;
354  enc_size = p_next_output_frame->size;
355 
356  /* Append the end of sequence information to the last frame in the
357  * sequence. */
358  if (last_frame_in_sequence && p_dirac_params->enc_buf_size > 0) {
359  memcpy(frame + enc_size, p_dirac_params->enc_buf,
360  p_dirac_params->enc_buf_size);
361  enc_size += p_dirac_params->enc_buf_size;
362  av_freep(&p_dirac_params->enc_buf);
363  p_dirac_params->enc_buf_size = 0;
364  }
365 
366  /* free frame */
367  DiracFreeFrame(p_next_output_frame);
368 
369  return enc_size;
370 }
371 
373 {
374  DiracEncoderParams *p_dirac_params = avccontext->priv_data;
375 
376  /* close the encoder */
377  dirac_encoder_close(p_dirac_params->p_encoder);
378 
379  /* free data in the output frame queue */
382 
383  /* free the encoder buffer */
384  if (p_dirac_params->enc_buf_size)
385  av_freep(&p_dirac_params->enc_buf);
386 
387  /* free the input frame buffer */
388  av_freep(&p_dirac_params->p_in_frame_buf);
389 
390  return 0;
391 }
392 
393 
395  .name = "libdirac",
396  .type = AVMEDIA_TYPE_VIDEO,
397  .id = CODEC_ID_DIRAC,
398  .priv_data_size = sizeof(DiracEncoderParams),
400  .encode = libdirac_encode_frame,
402  .capabilities = CODEC_CAP_DELAY,
404  .long_name = NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),
405 };
uint32_t size
encoded frame size
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:68
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:154
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
Audio Video Frame.
Definition: avcodec.h:985
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
int num
numerator
Definition: rational.h:44
struct DiracEncoderParams DiracEncoderParams
Dirac encoder private data.
int eos_signalled
end of sequence signalled by user, 0 - false, 1 - true
Definition: libdiracenc.c:67
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
uint8_t * p_encbuf
encoded frame data
four components are given, that's all.
Definition: avcodec.h:3367
AVCodec.
Definition: avcodec.h:3189
static dirac_chroma_t GetDiracChromaFormat(enum PixelFormat ff_pix_fmt)
Works out Dirac-compatible chroma format.
Definition: libdiracenc.c:76
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1398
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:147
uint32_t frame_num
encoded frame number.
uint16_t key_frame
key frame flag.
static const VideoFormat ff_dirac_video_formats[]
Dirac video preset table.
Definition: libdiracenc.c:92
unsigned char * p_in_frame_buf
input frame buffer
Definition: libdiracenc.c:55
DiracSchroQueue enc_frame_queue
queue storing encoded frames
Definition: libdiracenc.c:64
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:64
#define av_cold
Definition: attributes.h:71
int64_t pts
presentation timestamp in time_base units (time when frame should be shown to user) If AV_NOPTS_VALUE...
Definition: avcodec.h:1037
contains a single encoded frame returned from Dirac or Schroedinger
const char data[16]
Definition: mxf.c:60
int coder_type
coder type
Definition: avcodec.h:2220
int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: imgconvert.c:476
int eos_pulled
end of sequence returned by encoder, 0 - false, 1 - true
Definition: libdiracenc.c:70
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
int enc_buf_size
size of encoder buffer
Definition: libdiracenc.c:61
static int verbose
Definition: ffmpeg.c:213
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1745
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:137
static void DiracFreeFrame(void *data)
Definition: libdiracenc.c:220
data structures common to libdirac encoder and decoder
void * ff_dirac_schro_queue_pop(DiracSchroQueue *queue)
Return the first element in the queue.
A simple queue implementation used in libdirac and libschroedinger.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
int size
Queue size.
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1355
static const struct @27 dirac_pixel_format_map[]
Table providing a Dirac chroma format to Libav pixel format mapping.
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:69
Dirac encoder private data.
Definition: libdiracenc.c:41
AVFrame picture
frame being encoded
Definition: libdiracenc.c:46
int bit_rate
the average bitrate
Definition: avcodec.h:1340
unsigned char * enc_buf
buffer to store encoder output before writing it to the frame queue
Definition: libdiracenc.c:58
enum PixelFormat ff_pix_fmt
Definition: libdirac.h:36
int width
picture width / height.
Definition: avcodec.h:1408
dirac_encoder_t * p_encoder
Dirac encoder handle.
Definition: libdiracenc.c:52
static av_cold int libdirac_encode_close(AVCodecContext *avccontext)
Definition: libdiracenc.c:372
static VideoFormat GetDiracVideoFormatPreset(AVCodecContext *avccontext)
Returns the video format preset matching the input video dimensions and time base.
Definition: libdiracenc.c:116
NULL
Definition: eval.c:50
int ff_dirac_schro_queue_push_back(DiracSchroQueue *queue, void *p_data)
Add an element to the end of the queue.
int debug
debug
Definition: avcodec.h:2007
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:627
static av_cold int libdirac_encode_init(AVCodecContext *avccontext)
Definition: libdiracenc.c:127
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:64
dirac_encoder_context_t enc_ctx
Dirac encoder context.
Definition: libdiracenc.c:43
#define FF_CODER_TYPE_VLC
Definition: avcodec.h:2210
int frame_size
frame size
Definition: libdiracenc.c:49
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:2208
static uint32_t state
Definition: trasher.c:25
#define CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:655
static int libdirac_encode_frame(AVCodecContext *avccontext, unsigned char *frame, int buf_size, void *data)
Definition: libdiracenc.c:228
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1417
PixelFormat
Pixel format.
Definition: pixfmt.h:62
int den
denominator
Definition: rational.h:45
AVCodec ff_libdirac_encoder
Definition: libdiracenc.c:394
void * priv_data
Definition: avcodec.h:1531
data structures common to libdirac and libschroedinger
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1022
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:250
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:750
void ff_dirac_schro_queue_init(DiracSchroQueue *queue)
Initialise the queue.
int avpicture_layout(const AVPicture *src, enum PixelFormat pix_fmt, int width, int height, unsigned char *dest, int dest_size)
Copy pixel data from an AVPicture into a buffer.
Definition: imgconvert.c:443
unsigned int ff_dirac_schro_get_video_format_idx(AVCodecContext *avccontext)
Returns the index into the Dirac Schro common video format info table.
void ff_dirac_schro_queue_free(DiracSchroQueue *queue, void(*free_func)(void *))
Free the queue resources.