libschroedingerenc.c
Go to the documentation of this file.
1 /*
2  * Dirac encoder support via Schroedinger libraries
3  * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 
30 #undef NDEBUG
31 #include <assert.h>
32 
33 #include <schroedinger/schro.h>
34 #include <schroedinger/schrodebug.h>
35 #include <schroedinger/schrovideoformat.h>
36 
37 #include "avcodec.h"
38 #include "internal.h"
39 #include "libschroedinger.h"
40 #include "bytestream.h"
41 
42 
44 typedef struct SchroEncoderParams {
46  SchroVideoFormat *format;
47 
49  SchroFrameFormat frame_format;
50 
53 
56 
58  SchroEncoder* encoder;
59 
61  unsigned char *enc_buf;
62 
65 
68 
71 
74 
75  /* counter for frames submitted to encoder, used as dts */
76  int64_t dts;
78 
82 static int set_chroma_format(AVCodecContext *avccontext)
83 {
84  int num_formats = sizeof(schro_pixel_format_map) /
85  sizeof(schro_pixel_format_map[0]);
86  int idx;
87 
88  SchroEncoderParams *p_schro_params = avccontext->priv_data;
89 
90  for (idx = 0; idx < num_formats; ++idx) {
92  avccontext->pix_fmt) {
93  p_schro_params->format->chroma_format =
94  schro_pixel_format_map[idx].schro_pix_fmt;
95  return 0;
96  }
97  }
98 
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");
102 
103  return -1;
104 }
105 
107 {
108  SchroEncoderParams *p_schro_params = avccontext->priv_data;
109  SchroVideoFormatEnum preset;
110 
111  /* Initialize the libraries that libschroedinger depends on. */
112  schro_init();
113 
114  /* Create an encoder object. */
115  p_schro_params->encoder = schro_encoder_new();
116 
117  if (!p_schro_params->encoder) {
118  av_log(avccontext, AV_LOG_ERROR,
119  "Unrecoverable Error: schro_encoder_new failed. ");
120  return -1;
121  }
122 
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;
130 
131  if (set_chroma_format(avccontext) == -1)
132  return -1;
133 
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;
140  }
141 
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;
146  }
147 
148  if (avccontext->color_trc == AVCOL_TRC_BT709) {
149  p_schro_params->format->transfer_function = SCHRO_TRANSFER_CHAR_TV_GAMMA;
150  }
151 
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");
157  return -1;
158  }
159 
160  p_schro_params->format->frame_rate_numerator = avccontext->time_base.den;
161  p_schro_params->format->frame_rate_denominator = avccontext->time_base.num;
162 
163  p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
164  avccontext->width,
165  avccontext->height);
166 
167  avccontext->coded_frame = &p_schro_params->picture;
168 
169  if (!avccontext->gop_size) {
170  schro_encoder_setting_set_double(p_schro_params->encoder,
171  "gop_structure",
172  SCHRO_ENCODER_GOP_INTRA_ONLY);
173 
174  if (avccontext->coder_type == FF_CODER_TYPE_VLC)
175  schro_encoder_setting_set_double(p_schro_params->encoder,
176  "enable_noarith", 1);
177  } else {
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;
182  }
183 
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,
189  "rate_control",
190  SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
191  } else {
192  int quality;
193  schro_encoder_setting_set_double(p_schro_params->encoder,
194  "rate_control",
195  SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY);
196 
197  quality = avccontext->global_quality / FF_QP2LAMBDA;
198  if (quality > 10)
199  quality = 10;
200  schro_encoder_setting_set_double(p_schro_params->encoder,
201  "quality", quality);
202  }
203  } else {
204  schro_encoder_setting_set_double(p_schro_params->encoder,
205  "rate_control",
206  SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
207 
208  schro_encoder_setting_set_double(p_schro_params->encoder,
209  "bitrate",
210  avccontext->bit_rate);
211 
212  }
213 
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);
219 
220  schro_encoder_setting_set_double(p_schro_params->encoder, "open_gop",
221  !(avccontext->flags & CODEC_FLAG_CLOSED_GOP));
222 
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);
227 
228  /* Set the encoder format. */
229  schro_encoder_set_video_format(p_schro_params->encoder,
230  p_schro_params->format);
231 
232  /* Set the debug level. */
233  schro_debug_set_level(avccontext->debug);
234 
235  schro_encoder_start(p_schro_params->encoder);
236 
237  /* Initialize the encoded frame queue. */
238  ff_schro_queue_init(&p_schro_params->enc_frame_queue);
239  return 0;
240 }
241 
242 static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avccontext,
243  const AVFrame *frame)
244 {
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);
251 
252  if (in_frame)
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);
257 
258  return in_frame;
259 }
260 
262 {
263  FFSchroEncodedFrame *enc_frame = data;
264 
265  av_freep(&enc_frame->p_encbuf);
266  av_free(enc_frame);
267 }
268 
270  const AVFrame *frame, int *got_packet)
271 {
272  int enc_size = 0;
273  SchroEncoderParams *p_schro_params = avccontext->priv_data;
274  SchroEncoder *encoder = p_schro_params->encoder;
275  struct FFSchroEncodedFrame *p_frame_output = NULL;
276  int go = 1;
277  SchroBuffer *enc_buf;
278  int presentation_frame;
279  int parse_code;
280  int last_frame_in_sequence = 0;
281  int pkt_size, ret;
282 
283  if (!frame) {
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;
288  }
289  } else {
290  /* Allocate frame data to schro input buffer. */
291  SchroFrame *in_frame = libschroedinger_frame_from_data(avccontext,
292  frame);
293  /* Load next frame. */
294  schro_encoder_push_frame(encoder, in_frame);
295  }
296 
297  if (p_schro_params->eos_pulled)
298  go = 0;
299 
300  /* Now check to see if we have any output from the encoder. */
301  while (go) {
302  SchroStateEnum state;
303  state = schro_encoder_wait(encoder);
304  switch (state) {
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];
311 
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
315  */
316  p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf,
317  p_schro_params->enc_buf_size + enc_buf->length);
318 
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;
322 
323 
324  if (state == SCHRO_STATE_END_OF_STREAM) {
325  p_schro_params->eos_pulled = 1;
326  go = 0;
327  }
328 
329  if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
330  schro_buffer_unref(enc_buf);
331  break;
332  }
333 
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;
342 
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);
346 
347  ff_schro_queue_push_back(&p_schro_params->enc_frame_queue,
348  p_frame_output);
349  p_schro_params->enc_buf_size = 0;
350  p_schro_params->enc_buf = NULL;
351 
352  schro_buffer_unref(enc_buf);
353 
354  break;
355 
356  case SCHRO_STATE_NEED_FRAME:
357  go = 0;
358  break;
359 
360  case SCHRO_STATE_AGAIN:
361  break;
362 
363  default:
364  av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
365  return -1;
366  }
367  }
368 
369  /* Copy 'next' frame in queue. */
370 
371  if (p_schro_params->enc_frame_queue.size == 1 &&
372  p_schro_params->eos_pulled)
373  last_frame_in_sequence = 1;
374 
375  p_frame_output = ff_schro_queue_pop(&p_schro_params->enc_frame_queue);
376 
377  if (!p_frame_output)
378  return 0;
379 
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);
385  goto error;
386  }
387 
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. */
393  pkt->pts =
394  avccontext->coded_frame->pts = p_frame_output->frame_num;
395  pkt->dts = p_schro_params->dts++;
396  enc_size = p_frame_output->size;
397 
398  /* Append the end of sequence information to the last frame in the
399  * sequence. */
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;
406  }
407 
408  if (p_frame_output->key_frame)
409  pkt->flags |= AV_PKT_FLAG_KEY;
410  *got_packet = 1;
411 
412 error:
413  /* free frame */
414  libschroedinger_free_frame(p_frame_output);
415  return ret;
416 }
417 
418 
420 {
421  SchroEncoderParams *p_schro_params = avccontext->priv_data;
422 
423  /* Close the encoder. */
424  schro_encoder_free(p_schro_params->encoder);
425 
426  /* Free data in the output frame queue. */
427  ff_schro_queue_free(&p_schro_params->enc_frame_queue,
429 
430 
431  /* Free the encoder buffer. */
432  if (p_schro_params->enc_buf_size)
433  av_freep(&p_schro_params->enc_buf);
434 
435  /* Free the video format structure. */
436  av_freep(&p_schro_params->format);
437 
438  return 0;
439 }
440 
441 
443  .name = "libschroedinger",
444  .type = AVMEDIA_TYPE_VIDEO,
445  .id = AV_CODEC_ID_DIRAC,
446  .priv_data_size = sizeof(SchroEncoderParams),
448  .encode2 = libschroedinger_encode_frame,
450  .capabilities = CODEC_CAP_DELAY,
451  .pix_fmts = (const enum AVPixelFormat[]){
453  },
454  .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
455 };
AVFrame picture
frame being encoded
static int libschroedinger_encode_init(AVCodecContext *avccontext)
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
uint16_t key_frame
key frame flag.
int num
numerator
Definition: rational.h:44
static const struct @34 schro_pixel_format_map[]
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
int avpicture_layout(const AVPicture *src, enum AVPixelFormat pix_fmt, int width, int height, unsigned char *dest, int dest_size)
Copy pixel data from an AVPicture into a buffer.
Definition: avpicture.c:49
libschroedinger encoder private data
int frame_size
frame size
four components are given, that's all.
Definition: avcodec.h:3153
data structures common to libschroedinger decoder and encoder
AVCodec.
Definition: avcodec.h:2960
enum AVPixelFormat ff_pix_fmt
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1465
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:151
static void libschroedinger_free_frame(void *data)
SchroFrame * ff_create_schro_frame(AVCodecContext *avccontext, SchroFrameFormat schro_frame_fmt)
Create a Schro frame based on the dimensions and frame format passed.
#define AV_RB32
Definition: intreadwrite.h:130
uint32_t frame_num
encoded frame number.
int64_t pts
presentation timestamp in time_base units (time when frame should be shown to user) If AV_NOPTS_VALUE...
Definition: avcodec.h:1088
const char data[16]
Definition: mxf.c:66
int coder_type
coder type
Definition: avcodec.h:2388
uint8_t * data
Definition: avcodec.h:915
contains a single encoded frame returned from Dirac or Schroedinger
int size
Queue size.
uint8_t * p_encbuf
encoded frame data
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1634
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
SchroEncoder * encoder
Schroedinger encoder handle.
SchroFrameFormat frame_format
Schroedinger frame format.
void * ff_schro_queue_pop(FFSchroQueue *queue)
Return the first element in the queue.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void ff_schro_queue_init(FFSchroQueue *queue)
Initialise the queue.
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
FFSchroQueue enc_frame_queue
queue storing encoded frames
static int libschroedinger_encode_frame(AVCodecContext *avccontext, AVPacket *pkt, const AVFrame *frame, int *got_packet)
SchroVideoFormat * format
Schroedinger video format.
static int libschroedinger_encode_close(AVCodecContext *avccontext)
static SchroFrame * libschroedinger_frame_from_data(AVCodecContext *avccontext, const AVFrame *frame)
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
int ff_get_schro_frame_format(SchroChromaFormat schro_pix_fmt, SchroFrameFormat *schro_frame_fmt)
Sets the Schroedinger frame format corresponding to the Schro chroma format passed.
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
int enc_buf_size
Size of encoder buffer.
also ITU-R BT1361
Definition: avcodec.h:551
int bit_rate
the average bitrate
Definition: avcodec.h:1404
int ff_schro_queue_push_back(FFSchroQueue *queue, void *p_data)
Add an element to the end of the queue.
void ff_schro_queue_free(FFSchroQueue *queue, void(*free_func)(void *))
Free the queue resources.
A simple queue implementation used in libschroedinger.
int width
picture width / height.
Definition: avcodec.h:1508
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2058
SchroVideoFormatEnum ff_get_schro_video_format_preset(AVCodecContext *avccontext)
Returns the video format preset matching the input video dimensions and time base.
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:878
int eos_signalled
end of sequence signalled
NULL
Definition: eval.c:52
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: avcodec.h:561
external API header
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: avcodec.h:543
static int set_chroma_format(AVCodecContext *avccontext)
Works out Schro-compatible chroma format.
int debug
debug
Definition: avcodec.h:2568
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
unsigned char * enc_buf
buffer to store encoder output before writing it to the frame queue
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2072
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2065
int eos_pulled
end of sequence pulled
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1420
static uint32_t state
Definition: trasher.c:27
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: avcodec.h:544
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1524
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
common internal api header.
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:116
AVCodec ff_libschroedinger_encoder
int den
denominator
Definition: rational.h:45
void * priv_data
Definition: avcodec.h:1382
uint32_t size
encoded frame size
int avpicture_get_size(enum AVPixelFormat 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: avpicture.c:85
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:914
struct SchroEncoderParams SchroEncoderParams
libschroedinger encoder private data
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: avcodec.h:540
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: avcodec.h:564
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898
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:158
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:908