Libav
|
00001 /* 00002 * Dirac decoder support via Schroedinger libraries 00003 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com > 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00030 #include "avcodec.h" 00031 #include "libdirac_libschro.h" 00032 #include "libschroedinger.h" 00033 00034 #undef NDEBUG 00035 #include <assert.h> 00036 00037 00038 #include <schroedinger/schro.h> 00039 #include <schroedinger/schrodebug.h> 00040 #include <schroedinger/schrovideoformat.h> 00041 00043 typedef struct FfmpegSchroDecoderParams { 00045 SchroVideoFormat *format; 00046 00048 SchroFrameFormat frame_format; 00049 00051 SchroDecoder* decoder; 00052 00054 FfmpegDiracSchroQueue dec_frame_queue; 00055 00057 int eos_signalled; 00058 00060 int eos_pulled; 00061 00063 AVPicture dec_pic; 00064 } FfmpegSchroDecoderParams; 00065 00066 typedef struct FfmpegSchroParseUnitContext { 00067 const uint8_t *buf; 00068 int buf_size; 00069 } FfmpegSchroParseUnitContext; 00070 00071 00072 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf, 00073 void *priv); 00074 00075 static void FfmpegSchroParseContextInit(FfmpegSchroParseUnitContext *parse_ctx, 00076 const uint8_t *buf, int buf_size) 00077 { 00078 parse_ctx->buf = buf; 00079 parse_ctx->buf_size = buf_size; 00080 } 00081 00082 static SchroBuffer* FfmpegFindNextSchroParseUnit(FfmpegSchroParseUnitContext *parse_ctx) 00083 { 00084 SchroBuffer *enc_buf = NULL; 00085 int next_pu_offset = 0; 00086 unsigned char *in_buf; 00087 00088 if (parse_ctx->buf_size < 13 || 00089 parse_ctx->buf[0] != 'B' || 00090 parse_ctx->buf[1] != 'B' || 00091 parse_ctx->buf[2] != 'C' || 00092 parse_ctx->buf[3] != 'D') 00093 return NULL; 00094 00095 next_pu_offset = (parse_ctx->buf[5] << 24) + 00096 (parse_ctx->buf[6] << 16) + 00097 (parse_ctx->buf[7] << 8) + 00098 parse_ctx->buf[8]; 00099 00100 if (next_pu_offset == 0 && 00101 SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4])) 00102 next_pu_offset = 13; 00103 00104 if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset) 00105 return NULL; 00106 00107 in_buf = av_malloc(next_pu_offset); 00108 memcpy(in_buf, parse_ctx->buf, next_pu_offset); 00109 enc_buf = schro_buffer_new_with_data(in_buf, next_pu_offset); 00110 enc_buf->free = libschroedinger_decode_buffer_free; 00111 enc_buf->priv = in_buf; 00112 00113 parse_ctx->buf += next_pu_offset; 00114 parse_ctx->buf_size -= next_pu_offset; 00115 00116 return enc_buf; 00117 } 00118 00122 static enum PixelFormat GetFfmpegChromaFormat(SchroChromaFormat schro_pix_fmt) 00123 { 00124 int num_formats = sizeof(ffmpeg_schro_pixel_format_map) / 00125 sizeof(ffmpeg_schro_pixel_format_map[0]); 00126 int idx; 00127 00128 for (idx = 0; idx < num_formats; ++idx) 00129 if (ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) 00130 return ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt; 00131 return PIX_FMT_NONE; 00132 } 00133 00134 static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext) 00135 { 00136 00137 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; 00138 /* First of all, initialize our supporting libraries. */ 00139 schro_init(); 00140 00141 schro_debug_set_level(avccontext->debug); 00142 p_schro_params->decoder = schro_decoder_new(); 00143 schro_decoder_set_skip_ratio(p_schro_params->decoder, 1); 00144 00145 if (!p_schro_params->decoder) 00146 return -1; 00147 00148 /* Initialize the decoded frame queue. */ 00149 ff_dirac_schro_queue_init(&p_schro_params->dec_frame_queue); 00150 return 0; 00151 } 00152 00153 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf, 00154 void *priv) 00155 { 00156 av_freep(&priv); 00157 } 00158 00159 static void libschroedinger_decode_frame_free(void *frame) 00160 { 00161 schro_frame_unref(frame); 00162 } 00163 00164 static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext) 00165 { 00166 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; 00167 SchroDecoder *decoder = p_schro_params->decoder; 00168 00169 p_schro_params->format = schro_decoder_get_video_format(decoder); 00170 00171 /* Tell FFmpeg about sequence details. */ 00172 if (avcodec_check_dimensions(avccontext, p_schro_params->format->width, 00173 p_schro_params->format->height) < 0) { 00174 av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n", 00175 p_schro_params->format->width, p_schro_params->format->height); 00176 avccontext->height = avccontext->width = 0; 00177 return; 00178 } 00179 avccontext->height = p_schro_params->format->height; 00180 avccontext->width = p_schro_params->format->width; 00181 avccontext->pix_fmt = GetFfmpegChromaFormat(p_schro_params->format->chroma_format); 00182 00183 if (ff_get_schro_frame_format(p_schro_params->format->chroma_format, 00184 &p_schro_params->frame_format) == -1) { 00185 av_log(avccontext, AV_LOG_ERROR, 00186 "This codec currently only supports planar YUV 4:2:0, 4:2:2 " 00187 "and 4:4:4 formats.\n"); 00188 return; 00189 } 00190 00191 avccontext->time_base.den = p_schro_params->format->frame_rate_numerator; 00192 avccontext->time_base.num = p_schro_params->format->frame_rate_denominator; 00193 00194 if (!p_schro_params->dec_pic.data[0]) 00195 avpicture_alloc(&p_schro_params->dec_pic, 00196 avccontext->pix_fmt, 00197 avccontext->width, 00198 avccontext->height); 00199 } 00200 00201 static int libschroedinger_decode_frame(AVCodecContext *avccontext, 00202 void *data, int *data_size, 00203 AVPacket *avpkt) 00204 { 00205 const uint8_t *buf = avpkt->data; 00206 int buf_size = avpkt->size; 00207 00208 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; 00209 SchroDecoder *decoder = p_schro_params->decoder; 00210 SchroVideoFormat *format; 00211 AVPicture *picture = data; 00212 SchroBuffer *enc_buf; 00213 SchroFrame* frame; 00214 int state; 00215 int go = 1; 00216 int outer = 1; 00217 FfmpegSchroParseUnitContext parse_ctx; 00218 00219 *data_size = 0; 00220 00221 FfmpegSchroParseContextInit(&parse_ctx, buf, buf_size); 00222 if (!buf_size) { 00223 if (!p_schro_params->eos_signalled) { 00224 state = schro_decoder_push_end_of_stream(decoder); 00225 p_schro_params->eos_signalled = 1; 00226 } 00227 } 00228 00229 /* Loop through all the individual parse units in the input buffer */ 00230 do { 00231 if ((enc_buf = FfmpegFindNextSchroParseUnit(&parse_ctx))) { 00232 /* Push buffer into decoder. */ 00233 if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) && 00234 SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0) 00235 avccontext->has_b_frames = 1; 00236 state = schro_decoder_push(decoder, enc_buf); 00237 if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT) 00238 libschroedinger_handle_first_access_unit(avccontext); 00239 go = 1; 00240 } else 00241 outer = 0; 00242 format = p_schro_params->format; 00243 00244 while (go) { 00245 /* Parse data and process result. */ 00246 state = schro_decoder_wait(decoder); 00247 switch (state) { 00248 case SCHRO_DECODER_FIRST_ACCESS_UNIT: 00249 libschroedinger_handle_first_access_unit(avccontext); 00250 break; 00251 00252 case SCHRO_DECODER_NEED_BITS: 00253 /* Need more input data - stop iterating over what we have. */ 00254 go = 0; 00255 break; 00256 00257 case SCHRO_DECODER_NEED_FRAME: 00258 /* Decoder needs a frame - create one and push it in. */ 00259 frame = ff_create_schro_frame(avccontext, 00260 p_schro_params->frame_format); 00261 schro_decoder_add_output_picture(decoder, frame); 00262 break; 00263 00264 case SCHRO_DECODER_OK: 00265 /* Pull a frame out of the decoder. */ 00266 frame = schro_decoder_pull(decoder); 00267 00268 if (frame) 00269 ff_dirac_schro_queue_push_back(&p_schro_params->dec_frame_queue, 00270 frame); 00271 break; 00272 case SCHRO_DECODER_EOS: 00273 go = 0; 00274 p_schro_params->eos_pulled = 1; 00275 schro_decoder_reset(decoder); 00276 outer = 0; 00277 break; 00278 00279 case SCHRO_DECODER_ERROR: 00280 return -1; 00281 break; 00282 } 00283 } 00284 } while (outer); 00285 00286 /* Grab next frame to be returned from the top of the queue. */ 00287 frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue); 00288 00289 if (frame) { 00290 memcpy(p_schro_params->dec_pic.data[0], 00291 frame->components[0].data, 00292 frame->components[0].length); 00293 00294 memcpy(p_schro_params->dec_pic.data[1], 00295 frame->components[1].data, 00296 frame->components[1].length); 00297 00298 memcpy(p_schro_params->dec_pic.data[2], 00299 frame->components[2].data, 00300 frame->components[2].length); 00301 00302 /* Fill picture with current buffer data from Schroedinger. */ 00303 avpicture_fill(picture, p_schro_params->dec_pic.data[0], 00304 avccontext->pix_fmt, 00305 avccontext->width, avccontext->height); 00306 00307 *data_size = sizeof(AVPicture); 00308 00309 /* Now free the frame resources. */ 00310 libschroedinger_decode_frame_free(frame); 00311 } 00312 return buf_size; 00313 } 00314 00315 00316 static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext) 00317 { 00318 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; 00319 /* Free the decoder. */ 00320 schro_decoder_free(p_schro_params->decoder); 00321 av_freep(&p_schro_params->format); 00322 00323 avpicture_free(&p_schro_params->dec_pic); 00324 00325 /* Free data in the output frame queue. */ 00326 ff_dirac_schro_queue_free(&p_schro_params->dec_frame_queue, 00327 libschroedinger_decode_frame_free); 00328 00329 return 0; 00330 } 00331 00332 static void libschroedinger_flush(AVCodecContext *avccontext) 00333 { 00334 /* Got a seek request. Free the decoded frames queue and then reset 00335 * the decoder */ 00336 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; 00337 00338 /* Free data in the output frame queue. */ 00339 ff_dirac_schro_queue_free(&p_schro_params->dec_frame_queue, 00340 libschroedinger_decode_frame_free); 00341 00342 ff_dirac_schro_queue_init(&p_schro_params->dec_frame_queue); 00343 schro_decoder_reset(p_schro_params->decoder); 00344 p_schro_params->eos_pulled = 0; 00345 p_schro_params->eos_signalled = 0; 00346 } 00347 00348 AVCodec libschroedinger_decoder = { 00349 "libschroedinger", 00350 AVMEDIA_TYPE_VIDEO, 00351 CODEC_ID_DIRAC, 00352 sizeof(FfmpegSchroDecoderParams), 00353 libschroedinger_decode_init, 00354 NULL, 00355 libschroedinger_decode_close, 00356 libschroedinger_decode_frame, 00357 CODEC_CAP_DELAY, 00358 .flush = libschroedinger_flush, 00359 .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"), 00360 };