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