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.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
  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 "libavutil/imgutils.h"
  30. #include "libdirac.h"
  31. #undef NDEBUG
  32. #include <assert.h>
  33. #include <libdirac_decoder/dirac_parser.h>
  34. /** contains a single frame returned from Dirac */
  35. typedef struct FfmpegDiracDecoderParams {
  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. return PIX_FMT_NONE;
  53. }
  54. static av_cold int libdirac_decode_init(AVCodecContext *avccontext)
  55. {
  56. FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
  57. p_dirac_params->p_decoder = dirac_decoder_init(avccontext->debug);
  58. if (!p_dirac_params->p_decoder)
  59. return -1;
  60. return 0;
  61. }
  62. static int libdirac_decode_frame(AVCodecContext *avccontext,
  63. void *data, int *data_size,
  64. AVPacket *avpkt)
  65. {
  66. const uint8_t *buf = avpkt->data;
  67. int buf_size = avpkt->size;
  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. case STATE_BUFFER:
  85. return buf_size;
  86. case STATE_SEQUENCE:
  87. {
  88. /* tell FFmpeg about sequence details */
  89. dirac_sourceparams_t *src_params = &p_dirac_params->p_decoder->src_params;
  90. if (av_image_check_size(src_params->width, src_params->height,
  91. 0, avccontext) < 0) {
  92. av_log(avccontext, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n",
  93. src_params->width, src_params->height);
  94. avccontext->height = avccontext->width = 0;
  95. return -1;
  96. }
  97. avccontext->height = src_params->height;
  98. avccontext->width = src_params->width;
  99. avccontext->pix_fmt = GetFfmpegChromaFormat(src_params->chroma);
  100. if (avccontext->pix_fmt == PIX_FMT_NONE) {
  101. av_log(avccontext, AV_LOG_ERROR,
  102. "Dirac chroma format %d not supported currently\n",
  103. src_params->chroma);
  104. return -1;
  105. }
  106. avccontext->time_base.den = src_params->frame_rate.numerator;
  107. avccontext->time_base.num = src_params->frame_rate.denominator;
  108. /* calculate output dimensions */
  109. avpicture_fill(&pic, NULL, avccontext->pix_fmt,
  110. avccontext->width, avccontext->height);
  111. pict_size = avpicture_get_size(avccontext->pix_fmt,
  112. avccontext->width,
  113. avccontext->height);
  114. /* allocate output buffer */
  115. if (!p_dirac_params->p_out_frame_buf)
  116. p_dirac_params->p_out_frame_buf = av_malloc(pict_size);
  117. buffer[0] = p_dirac_params->p_out_frame_buf;
  118. buffer[1] = p_dirac_params->p_out_frame_buf +
  119. pic.linesize[0] * avccontext->height;
  120. buffer[2] = buffer[1] +
  121. pic.linesize[1] * src_params->chroma_height;
  122. /* tell Dirac about output destination */
  123. dirac_set_buf(p_dirac_params->p_decoder, buffer, NULL);
  124. break;
  125. }
  126. case STATE_SEQUENCE_END:
  127. break;
  128. case STATE_PICTURE_AVAIL:
  129. /* fill picture with current buffer data from Dirac */
  130. avpicture_fill(picture, p_dirac_params->p_out_frame_buf,
  131. avccontext->pix_fmt,
  132. avccontext->width, avccontext->height);
  133. *data_size = sizeof(AVPicture);
  134. return buf_size;
  135. case STATE_INVALID:
  136. return -1;
  137. default:
  138. break;
  139. }
  140. }
  141. return buf_size;
  142. }
  143. static av_cold int libdirac_decode_close(AVCodecContext *avccontext)
  144. {
  145. FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
  146. dirac_decoder_close(p_dirac_params->p_decoder);
  147. av_freep(&p_dirac_params->p_out_frame_buf);
  148. return 0;
  149. }
  150. static void libdirac_flush(AVCodecContext *avccontext)
  151. {
  152. /* Got a seek request. We will need free memory held in the private
  153. * context and free the current Dirac decoder handle and then open
  154. * a new decoder handle. */
  155. libdirac_decode_close(avccontext);
  156. libdirac_decode_init(avccontext);
  157. return;
  158. }
  159. AVCodec ff_libdirac_decoder = {
  160. .name = "libdirac",
  161. .type = AVMEDIA_TYPE_VIDEO,
  162. .id = CODEC_ID_DIRAC,
  163. .priv_data_size = sizeof(FfmpegDiracDecoderParams),
  164. .init = libdirac_decode_init,
  165. .close = libdirac_decode_close,
  166. .decode = libdirac_decode_frame,
  167. .capabilities = CODEC_CAP_DELAY,
  168. .flush = libdirac_flush,
  169. .long_name = NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),
  170. };