FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23 * @file
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).
28 */
29 
30 #include <schroedinger/schro.h>
31 #include <schroedinger/schrodebug.h>
32 #include <schroedinger/schrovideoformat.h>
33 
34 #include "libavutil/avassert.h"
35 #include "avcodec.h"
36 #include "internal.h"
37 #include "libschroedinger.h"
38 #include "bytestream.h"
39 
40 
41 /** libschroedinger encoder private data */
42 typedef struct SchroEncoderParams {
43  /** Schroedinger video format */
44  SchroVideoFormat *format;
45 
46  /** Schroedinger frame format */
47  SchroFrameFormat frame_format;
48 
49  /** frame being encoded */
51 
52  /** frame size */
54 
55  /** Schroedinger encoder handle*/
56  SchroEncoder* encoder;
57 
58  /** buffer to store encoder output before writing it to the frame queue*/
59  unsigned char *enc_buf;
60 
61  /** Size of encoder buffer*/
63 
64  /** queue storing encoded frames */
66 
67  /** end of sequence signalled */
69 
70  /** end of sequence pulled */
72 
73  /* counter for frames submitted to encoder, used as dts */
74  int64_t dts;
76 
77 /**
78 * Works out Schro-compatible chroma format.
79 */
80 static int set_chroma_format(AVCodecContext *avccontext)
81 {
82  int num_formats = sizeof(schro_pixel_format_map) /
83  sizeof(schro_pixel_format_map[0]);
84  int idx;
85 
86  SchroEncoderParams *p_schro_params = avccontext->priv_data;
87 
88  for (idx = 0; idx < num_formats; ++idx) {
90  avccontext->pix_fmt) {
91  p_schro_params->format->chroma_format =
92  schro_pixel_format_map[idx].schro_pix_fmt;
93  return 0;
94  }
95  }
96 
97  av_log(avccontext, AV_LOG_ERROR,
98  "This codec currently only supports planar YUV 4:2:0, 4:2:2"
99  " and 4:4:4 formats.\n");
100 
101  return -1;
102 }
103 
105 {
106  SchroEncoderParams *p_schro_params = avccontext->priv_data;
107  SchroVideoFormatEnum preset;
108 
109  /* Initialize the libraries that libschroedinger depends on. */
110  schro_init();
111 
112  /* Create an encoder object. */
113  p_schro_params->encoder = schro_encoder_new();
114 
115  if (!p_schro_params->encoder) {
116  av_log(avccontext, AV_LOG_ERROR,
117  "Unrecoverable Error: schro_encoder_new failed. ");
118  return -1;
119  }
120 
121  /* Initialize the format. */
122  preset = ff_get_schro_video_format_preset(avccontext);
123  p_schro_params->format =
124  schro_encoder_get_video_format(p_schro_params->encoder);
125  schro_video_format_set_std_video_format(p_schro_params->format, preset);
126  p_schro_params->format->width = avccontext->width;
127  p_schro_params->format->height = avccontext->height;
128 
129  if (set_chroma_format(avccontext) == -1)
130  return -1;
131 
132  if (avccontext->color_primaries == AVCOL_PRI_BT709) {
133  p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_HDTV;
134  } else if (avccontext->color_primaries == AVCOL_PRI_BT470BG) {
135  p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_625;
136  } else if (avccontext->color_primaries == AVCOL_PRI_SMPTE170M) {
137  p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_525;
138  }
139 
140  if (avccontext->colorspace == AVCOL_SPC_BT709) {
141  p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_HDTV;
142  } else if (avccontext->colorspace == AVCOL_SPC_BT470BG) {
143  p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_SDTV;
144  }
145 
146  if (avccontext->color_trc == AVCOL_TRC_BT709) {
147  p_schro_params->format->transfer_function = SCHRO_TRANSFER_CHAR_TV_GAMMA;
148  }
149 
150  if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
151  &p_schro_params->frame_format) == -1) {
152  av_log(avccontext, AV_LOG_ERROR,
153  "This codec currently supports only planar YUV 4:2:0, 4:2:2"
154  " and 4:4:4 formats.\n");
155  return -1;
156  }
157 
158  p_schro_params->format->frame_rate_numerator = avccontext->time_base.den;
159  p_schro_params->format->frame_rate_denominator = avccontext->time_base.num;
160 
161  p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
162  avccontext->width,
163  avccontext->height);
164 
165  avccontext->coded_frame = &p_schro_params->picture;
166 
167  if (!avccontext->gop_size) {
168  schro_encoder_setting_set_double(p_schro_params->encoder,
169  "gop_structure",
170  SCHRO_ENCODER_GOP_INTRA_ONLY);
171 
172  if (avccontext->coder_type == FF_CODER_TYPE_VLC)
173  schro_encoder_setting_set_double(p_schro_params->encoder,
174  "enable_noarith", 1);
175  } else {
176  schro_encoder_setting_set_double(p_schro_params->encoder,
177  "au_distance", avccontext->gop_size);
178  avccontext->has_b_frames = 1;
179  p_schro_params->dts = -1;
180  }
181 
182  /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
183  if (avccontext->flags & CODEC_FLAG_QSCALE) {
184  if (!avccontext->global_quality) {
185  /* lossless coding */
186  schro_encoder_setting_set_double(p_schro_params->encoder,
187  "rate_control",
188  SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
189  } else {
190  int quality;
191  schro_encoder_setting_set_double(p_schro_params->encoder,
192  "rate_control",
193  SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY);
194 
195  quality = avccontext->global_quality / FF_QP2LAMBDA;
196  if (quality > 10)
197  quality = 10;
198  schro_encoder_setting_set_double(p_schro_params->encoder,
199  "quality", quality);
200  }
201  } else {
202  schro_encoder_setting_set_double(p_schro_params->encoder,
203  "rate_control",
204  SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
205 
206  schro_encoder_setting_set_double(p_schro_params->encoder,
207  "bitrate",
208  avccontext->bit_rate);
209 
210  }
211 
212  if (avccontext->flags & CODEC_FLAG_INTERLACED_ME)
213  /* All material can be coded as interlaced or progressive
214  irrespective of the type of source material. */
215  schro_encoder_setting_set_double(p_schro_params->encoder,
216  "interlaced_coding", 1);
217 
218  schro_encoder_setting_set_double(p_schro_params->encoder, "open_gop",
219  !(avccontext->flags & CODEC_FLAG_CLOSED_GOP));
220 
221  /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
222  * and libdirac support other bit-depth data. */
223  schro_video_format_set_std_signal_range(p_schro_params->format,
224  SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
225 
226  /* Set the encoder format. */
227  schro_encoder_set_video_format(p_schro_params->encoder,
228  p_schro_params->format);
229 
230  /* Set the debug level. */
231  schro_debug_set_level(avccontext->debug);
232 
233  schro_encoder_start(p_schro_params->encoder);
234 
235  /* Initialize the encoded frame queue. */
236  ff_schro_queue_init(&p_schro_params->enc_frame_queue);
237  return 0;
238 }
239 
240 static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avccontext,
241  const AVFrame *frame)
242 {
243  SchroEncoderParams *p_schro_params = avccontext->priv_data;
244  SchroFrame *in_frame;
245  /* Input line size may differ from what the codec supports. Especially
246  * when transcoding from one format to another. So use avpicture_layout
247  * to copy the frame. */
248  in_frame = ff_create_schro_frame(avccontext, p_schro_params->frame_format);
249 
250  if (in_frame)
251  avpicture_layout((const AVPicture *)frame, avccontext->pix_fmt,
252  avccontext->width, avccontext->height,
253  in_frame->components[0].data,
254  p_schro_params->frame_size);
255 
256  return in_frame;
257 }
258 
260 {
261  FFSchroEncodedFrame *enc_frame = data;
262 
263  av_freep(&enc_frame->p_encbuf);
264  av_free(enc_frame);
265 }
266 
268  const AVFrame *frame, int *got_packet)
269 {
270  int enc_size = 0;
271  SchroEncoderParams *p_schro_params = avccontext->priv_data;
272  SchroEncoder *encoder = p_schro_params->encoder;
273  struct FFSchroEncodedFrame *p_frame_output = NULL;
274  int go = 1;
275  SchroBuffer *enc_buf;
276  int presentation_frame;
277  int parse_code;
278  int last_frame_in_sequence = 0;
279  int pkt_size, ret;
280 
281  if (!frame) {
282  /* Push end of sequence if not already signalled. */
283  if (!p_schro_params->eos_signalled) {
284  schro_encoder_end_of_stream(encoder);
285  p_schro_params->eos_signalled = 1;
286  }
287  } else {
288  /* Allocate frame data to schro input buffer. */
289  SchroFrame *in_frame = libschroedinger_frame_from_data(avccontext,
290  frame);
291  /* Load next frame. */
292  schro_encoder_push_frame(encoder, in_frame);
293  }
294 
295  if (p_schro_params->eos_pulled)
296  go = 0;
297 
298  /* Now check to see if we have any output from the encoder. */
299  while (go) {
300  SchroStateEnum state;
301  state = schro_encoder_wait(encoder);
302  switch (state) {
303  case SCHRO_STATE_HAVE_BUFFER:
304  case SCHRO_STATE_END_OF_STREAM:
305  enc_buf = schro_encoder_pull(encoder, &presentation_frame);
306  av_assert0(enc_buf->length > 0);
307  parse_code = enc_buf->data[4];
308 
309  /* All non-frame data is prepended to actual frame data to
310  * be able to set the pts correctly. So we don't write data
311  * to the frame output queue until we actually have a frame
312  */
313  p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf,
314  p_schro_params->enc_buf_size + enc_buf->length);
315 
316  memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
317  enc_buf->data, enc_buf->length);
318  p_schro_params->enc_buf_size += enc_buf->length;
319 
320 
321  if (state == SCHRO_STATE_END_OF_STREAM) {
322  p_schro_params->eos_pulled = 1;
323  go = 0;
324  }
325 
326  if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
327  schro_buffer_unref(enc_buf);
328  break;
329  }
330 
331  /* Create output frame. */
332  p_frame_output = av_mallocz(sizeof(FFSchroEncodedFrame));
333  /* Set output data. */
334  p_frame_output->size = p_schro_params->enc_buf_size;
335  p_frame_output->p_encbuf = p_schro_params->enc_buf;
336  if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
337  SCHRO_PARSE_CODE_IS_REFERENCE(parse_code))
338  p_frame_output->key_frame = 1;
339 
340  /* Parse the coded frame number from the bitstream. Bytes 14
341  * through 17 represesent the frame number. */
342  p_frame_output->frame_num = AV_RB32(enc_buf->data + 13);
343 
344  ff_schro_queue_push_back(&p_schro_params->enc_frame_queue,
345  p_frame_output);
346  p_schro_params->enc_buf_size = 0;
347  p_schro_params->enc_buf = NULL;
348 
349  schro_buffer_unref(enc_buf);
350 
351  break;
352 
353  case SCHRO_STATE_NEED_FRAME:
354  go = 0;
355  break;
356 
357  case SCHRO_STATE_AGAIN:
358  break;
359 
360  default:
361  av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
362  return -1;
363  }
364  }
365 
366  /* Copy 'next' frame in queue. */
367 
368  if (p_schro_params->enc_frame_queue.size == 1 &&
369  p_schro_params->eos_pulled)
370  last_frame_in_sequence = 1;
371 
372  p_frame_output = ff_schro_queue_pop(&p_schro_params->enc_frame_queue);
373 
374  if (!p_frame_output)
375  return 0;
376 
377  pkt_size = p_frame_output->size;
378  if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0)
379  pkt_size += p_schro_params->enc_buf_size;
380  if ((ret = ff_alloc_packet2(avccontext, pkt, pkt_size)) < 0)
381  goto error;
382 
383  memcpy(pkt->data, p_frame_output->p_encbuf, p_frame_output->size);
384  avccontext->coded_frame->key_frame = p_frame_output->key_frame;
385  /* Use the frame number of the encoded frame as the pts. It is OK to
386  * do so since Dirac is a constant frame rate codec. It expects input
387  * to be of constant frame rate. */
388  pkt->pts =
389  avccontext->coded_frame->pts = p_frame_output->frame_num;
390  pkt->dts = p_schro_params->dts++;
391  enc_size = p_frame_output->size;
392 
393  /* Append the end of sequence information to the last frame in the
394  * sequence. */
395  if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) {
396  memcpy(pkt->data + enc_size, p_schro_params->enc_buf,
397  p_schro_params->enc_buf_size);
398  enc_size += p_schro_params->enc_buf_size;
399  av_freep(&p_schro_params->enc_buf);
400  p_schro_params->enc_buf_size = 0;
401  }
402 
403  if (p_frame_output->key_frame)
404  pkt->flags |= AV_PKT_FLAG_KEY;
405  *got_packet = 1;
406 
407 error:
408  /* free frame */
409  libschroedinger_free_frame(p_frame_output);
410  return ret;
411 }
412 
413 
415 {
416  SchroEncoderParams *p_schro_params = avccontext->priv_data;
417 
418  /* Close the encoder. */
419  schro_encoder_free(p_schro_params->encoder);
420 
421  /* Free data in the output frame queue. */
422  ff_schro_queue_free(&p_schro_params->enc_frame_queue,
424 
425 
426  /* Free the encoder buffer. */
427  if (p_schro_params->enc_buf_size)
428  av_freep(&p_schro_params->enc_buf);
429 
430  /* Free the video format structure. */
431  av_freep(&p_schro_params->format);
432 
433  return 0;
434 }
435 
436 
438  .name = "libschroedinger",
439  .type = AVMEDIA_TYPE_VIDEO,
440  .id = AV_CODEC_ID_DIRAC,
441  .priv_data_size = sizeof(SchroEncoderParams),
443  .encode2 = libschroedinger_encode_frame,
445  .capabilities = CODEC_CAP_DELAY,
446  .pix_fmts = (const enum AVPixelFormat[]){
448  },
449  .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
450 };