libdiracdec.c 6.7 KB

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