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.

209 lines
6.7KB

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