You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
6.8KB

  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. AVPacket *avpkt)
  67. {
  68. const uint8_t *buf = avpkt->data;
  69. int buf_size = avpkt->size;
  70. FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
  71. AVPicture *picture = data;
  72. AVPicture pic;
  73. int pict_size;
  74. unsigned char *buffer[3];
  75. *data_size = 0;
  76. if (buf_size>0) {
  77. /* set data to decode into buffer */
  78. dirac_buffer (p_dirac_params->p_decoder, buf, buf+buf_size);
  79. if ((buf[4] &0x08) == 0x08 && (buf[4] & 0x03))
  80. avccontext->has_b_frames = 1;
  81. }
  82. while (1) {
  83. /* parse data and process result */
  84. DecoderState state = dirac_parse (p_dirac_params->p_decoder);
  85. switch (state)
  86. {
  87. case STATE_BUFFER:
  88. return buf_size;
  89. case STATE_SEQUENCE:
  90. {
  91. /* tell FFmpeg about sequence details */
  92. dirac_sourceparams_t *src_params =
  93. &p_dirac_params->p_decoder->src_params;
  94. if (avcodec_check_dimensions(avccontext, src_params->width,
  95. src_params->height) < 0) {
  96. av_log(avccontext, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n",
  97. src_params->width, src_params->height);
  98. avccontext->height = avccontext->width = 0;
  99. return -1;
  100. }
  101. avccontext->height = src_params->height;
  102. avccontext->width = src_params->width;
  103. avccontext->pix_fmt = GetFfmpegChromaFormat(src_params->chroma);
  104. if (avccontext->pix_fmt == PIX_FMT_NONE) {
  105. av_log (avccontext, AV_LOG_ERROR,
  106. "Dirac chroma format %d not supported currently\n",
  107. src_params->chroma);
  108. return -1;
  109. }
  110. avccontext->time_base.den = src_params->frame_rate.numerator;
  111. avccontext->time_base.num = src_params->frame_rate.denominator;
  112. /* calculate output dimensions */
  113. avpicture_fill(&pic, NULL, avccontext->pix_fmt,
  114. avccontext->width, avccontext->height);
  115. pict_size = avpicture_get_size(avccontext->pix_fmt,
  116. avccontext->width,
  117. avccontext->height);
  118. /* allocate output buffer */
  119. if (p_dirac_params->p_out_frame_buf == NULL)
  120. p_dirac_params->p_out_frame_buf = av_malloc (pict_size);
  121. buffer[0] = p_dirac_params->p_out_frame_buf;
  122. buffer[1] = p_dirac_params->p_out_frame_buf +
  123. pic.linesize[0] * avccontext->height;
  124. buffer[2] = buffer[1] +
  125. pic.linesize[1] * src_params->chroma_height;
  126. /* tell Dirac about output destination */
  127. dirac_set_buf(p_dirac_params->p_decoder, buffer, NULL);
  128. break;
  129. }
  130. case STATE_SEQUENCE_END:
  131. break;
  132. case STATE_PICTURE_AVAIL:
  133. /* fill picture with current buffer data from Dirac */
  134. avpicture_fill(picture, p_dirac_params->p_out_frame_buf,
  135. avccontext->pix_fmt,
  136. avccontext->width, avccontext->height);
  137. *data_size = sizeof(AVPicture);
  138. return buf_size;
  139. case STATE_INVALID:
  140. return -1;
  141. default:
  142. break;
  143. }
  144. }
  145. return buf_size;
  146. }
  147. static av_cold int libdirac_decode_close(AVCodecContext *avccontext)
  148. {
  149. FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
  150. dirac_decoder_close (p_dirac_params->p_decoder);
  151. av_freep(&p_dirac_params->p_out_frame_buf);
  152. return 0 ;
  153. }
  154. static void libdirac_flush (AVCodecContext *avccontext)
  155. {
  156. /* Got a seek request. We will need free memory held in the private
  157. * context and free the current Dirac decoder handle and then open
  158. * a new decoder handle. */
  159. libdirac_decode_close (avccontext);
  160. libdirac_decode_init (avccontext);
  161. return;
  162. }
  163. AVCodec libdirac_decoder = {
  164. "libdirac",
  165. CODEC_TYPE_VIDEO,
  166. CODEC_ID_DIRAC,
  167. sizeof(FfmpegDiracDecoderParams),
  168. libdirac_decode_init,
  169. NULL,
  170. libdirac_decode_close,
  171. libdirac_decode_frame,
  172. CODEC_CAP_DELAY,
  173. .flush = libdirac_flush,
  174. .long_name = NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),
  175. } ;