]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/libdiracdec.c
frsh: Export information about the last RTP contract and VRES
[frescor/ffmpeg.git] / libavcodec / libdiracdec.c
1 /*
2  * Dirac decoder 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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24 * @file libavcodec/libdiracdec.c
25 * Dirac decoder support via libdirac library; more details about the Dirac
26 * project can be found at http://dirac.sourceforge.net/.
27 * The libdirac_decoder library implements Dirac specification version 2.2
28 * (http://dirac.sourceforge.net/specification.html).
29 */
30
31 #include "libdirac.h"
32
33 #undef NDEBUG
34 #include <assert.h>
35
36 #include <libdirac_decoder/dirac_parser.h>
37
38 /** contains a single frame returned from Dirac */
39 typedef struct FfmpegDiracDecoderParams
40 {
41     /** decoder handle */
42     dirac_decoder_t* p_decoder;
43
44     /** buffer to hold decoded frame */
45     unsigned char* p_out_frame_buf;
46 } FfmpegDiracDecoderParams;
47
48
49 /**
50 * returns FFmpeg chroma format
51 */
52 static enum PixelFormat GetFfmpegChromaFormat(dirac_chroma_t dirac_pix_fmt)
53 {
54     int num_formats = sizeof(ffmpeg_dirac_pixel_format_map) /
55                       sizeof(ffmpeg_dirac_pixel_format_map[0]);
56     int idx;
57
58     for (idx = 0; idx < num_formats; ++idx) {
59         if (ffmpeg_dirac_pixel_format_map[idx].dirac_pix_fmt == dirac_pix_fmt) {
60             return ffmpeg_dirac_pixel_format_map[idx].ff_pix_fmt;
61         }
62     }
63     return PIX_FMT_NONE;
64 }
65
66 static av_cold int libdirac_decode_init(AVCodecContext *avccontext)
67 {
68
69     FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data ;
70     p_dirac_params->p_decoder =  dirac_decoder_init(avccontext->debug);
71
72     if (!p_dirac_params->p_decoder)
73         return -1;
74
75     return 0 ;
76 }
77
78 static int libdirac_decode_frame(AVCodecContext *avccontext,
79                                  void *data, int *data_size,
80                                  AVPacket *avpkt)
81 {
82     const uint8_t *buf = avpkt->data;
83     int buf_size = avpkt->size;
84
85     FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
86     AVPicture *picture = data;
87     AVPicture pic;
88     int pict_size;
89     unsigned char *buffer[3];
90
91     *data_size = 0;
92
93     if (buf_size>0) {
94         /* set data to decode into buffer */
95         dirac_buffer (p_dirac_params->p_decoder, buf, buf+buf_size);
96         if ((buf[4] &0x08) == 0x08 && (buf[4] & 0x03))
97             avccontext->has_b_frames = 1;
98     }
99     while (1) {
100          /* parse data and process result */
101         DecoderState state = dirac_parse (p_dirac_params->p_decoder);
102         switch (state)
103         {
104         case STATE_BUFFER:
105             return buf_size;
106
107         case STATE_SEQUENCE:
108         {
109             /* tell FFmpeg about sequence details */
110             dirac_sourceparams_t *src_params =
111                                   &p_dirac_params->p_decoder->src_params;
112
113             if (avcodec_check_dimensions(avccontext, src_params->width,
114                                          src_params->height) < 0) {
115                 av_log(avccontext, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n",
116                        src_params->width, src_params->height);
117                 avccontext->height = avccontext->width = 0;
118                 return -1;
119             }
120
121             avccontext->height = src_params->height;
122             avccontext->width  = src_params->width;
123
124             avccontext->pix_fmt = GetFfmpegChromaFormat(src_params->chroma);
125             if (avccontext->pix_fmt == PIX_FMT_NONE) {
126                 av_log (avccontext, AV_LOG_ERROR,
127                         "Dirac chroma format %d not supported currently\n",
128                         src_params->chroma);
129                 return -1;
130             }
131
132             avccontext->time_base.den = src_params->frame_rate.numerator;
133             avccontext->time_base.num = src_params->frame_rate.denominator;
134
135             /* calculate output dimensions */
136             avpicture_fill(&pic, NULL, avccontext->pix_fmt,
137                            avccontext->width, avccontext->height);
138
139             pict_size = avpicture_get_size(avccontext->pix_fmt,
140                                            avccontext->width,
141                                            avccontext->height);
142
143             /* allocate output buffer */
144             if (p_dirac_params->p_out_frame_buf == NULL)
145                 p_dirac_params->p_out_frame_buf = av_malloc (pict_size);
146             buffer[0] = p_dirac_params->p_out_frame_buf;
147             buffer[1] = p_dirac_params->p_out_frame_buf +
148                         pic.linesize[0] * avccontext->height;
149             buffer[2] = buffer[1] +
150                         pic.linesize[1] * src_params->chroma_height;
151
152             /* tell Dirac about output destination */
153             dirac_set_buf(p_dirac_params->p_decoder, buffer, NULL);
154             break;
155         }
156         case STATE_SEQUENCE_END:
157             break;
158
159         case STATE_PICTURE_AVAIL:
160             /* fill picture with current buffer data from Dirac */
161             avpicture_fill(picture, p_dirac_params->p_out_frame_buf,
162                            avccontext->pix_fmt,
163                            avccontext->width, avccontext->height);
164             *data_size = sizeof(AVPicture);
165             return buf_size;
166
167         case STATE_INVALID:
168             return -1;
169
170         default:
171             break;
172         }
173     }
174
175     return buf_size;
176 }
177
178
179 static av_cold int libdirac_decode_close(AVCodecContext *avccontext)
180 {
181     FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
182     dirac_decoder_close (p_dirac_params->p_decoder);
183
184     av_freep(&p_dirac_params->p_out_frame_buf);
185
186     return 0 ;
187 }
188
189 static void libdirac_flush (AVCodecContext *avccontext)
190 {
191     /* Got a seek request. We will need free memory held in the private
192      * context and free the current Dirac decoder handle and then open
193      * a new decoder handle. */
194     libdirac_decode_close (avccontext);
195     libdirac_decode_init (avccontext);
196     return;
197 }
198
199
200
201 AVCodec libdirac_decoder = {
202     "libdirac",
203     CODEC_TYPE_VIDEO,
204     CODEC_ID_DIRAC,
205     sizeof(FfmpegDiracDecoderParams),
206     libdirac_decode_init,
207     NULL,
208     libdirac_decode_close,
209     libdirac_decode_frame,
210     CODEC_CAP_DELAY,
211     .flush = libdirac_flush,
212     .long_name = NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),
213 } ;