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