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.

310 lines
9.9KB

  1. /*
  2. * Dirac decoder support via Schroedinger libraries
  3. * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libschroedingerdec.c
  23. * Dirac decoder support via libschroedinger-1.0 libraries. More details about
  24. * the Schroedinger project can be found at http://www.diracvideo.org/.
  25. * The library implements Dirac Specification Version 2.2.
  26. * (http://dirac.sourceforge.net/specification.html).
  27. */
  28. #include "avcodec.h"
  29. #include "libdirac_libschro.h"
  30. #include "libschroedinger.h"
  31. #undef NDEBUG
  32. #include <assert.h>
  33. #include <schroedinger/schro.h>
  34. #include <schroedinger/schrodebug.h>
  35. #include <schroedinger/schrovideoformat.h>
  36. /** libschroedinger decoder private data */
  37. typedef struct FfmpegSchroDecoderParams
  38. {
  39. /** Schroedinger video format */
  40. SchroVideoFormat *format;
  41. /** Schroedinger frame format */
  42. SchroFrameFormat frame_format;
  43. /** decoder handle */
  44. SchroDecoder* decoder;
  45. /** queue storing decoded frames */
  46. FfmpegDiracSchroQueue dec_frame_queue;
  47. /** end of sequence signalled */
  48. int eos_signalled;
  49. /** end of sequence pulled */
  50. int eos_pulled;
  51. /** decoded picture */
  52. AVPicture dec_pic;
  53. } FfmpegSchroDecoderParams;
  54. /**
  55. * Returns FFmpeg chroma format.
  56. */
  57. static enum PixelFormat GetFfmpegChromaFormat(SchroChromaFormat schro_pix_fmt)
  58. {
  59. int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
  60. sizeof(ffmpeg_schro_pixel_format_map[0]);
  61. int idx;
  62. for (idx = 0; idx < num_formats; ++idx) {
  63. if (ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) {
  64. return ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt;
  65. }
  66. }
  67. return PIX_FMT_NONE;
  68. }
  69. static int libschroedinger_decode_init(AVCodecContext *avccontext)
  70. {
  71. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data ;
  72. /* First of all, initialize our supporting libraries. */
  73. schro_init();
  74. schro_debug_set_level(avccontext->debug);
  75. p_schro_params->decoder = schro_decoder_new();
  76. schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
  77. if (!p_schro_params->decoder)
  78. return -1;
  79. /* Initialize the decoded frame queue. */
  80. ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
  81. return 0 ;
  82. }
  83. static void libschroedinger_decode_buffer_free (SchroBuffer *schro_buf,
  84. void *priv)
  85. {
  86. av_freep(&priv);
  87. }
  88. static void libschroedinger_decode_frame_free (void *frame)
  89. {
  90. schro_frame_unref(frame);
  91. }
  92. static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
  93. {
  94. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
  95. SchroDecoder *decoder = p_schro_params->decoder;
  96. p_schro_params->format = schro_decoder_get_video_format (decoder);
  97. /* Tell FFmpeg about sequence details. */
  98. if(avcodec_check_dimensions(avccontext, p_schro_params->format->width,
  99. p_schro_params->format->height) < 0) {
  100. av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
  101. p_schro_params->format->width, p_schro_params->format->height);
  102. avccontext->height = avccontext->width = 0;
  103. return;
  104. }
  105. avccontext->height = p_schro_params->format->height;
  106. avccontext->width = p_schro_params->format->width;
  107. avccontext->pix_fmt =
  108. GetFfmpegChromaFormat(p_schro_params->format->chroma_format);
  109. if (ff_get_schro_frame_format( p_schro_params->format->chroma_format,
  110. &p_schro_params->frame_format) == -1) {
  111. av_log (avccontext, AV_LOG_ERROR,
  112. "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
  113. "and 4:4:4 formats.\n");
  114. return;
  115. }
  116. avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
  117. avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
  118. if (p_schro_params->dec_pic.data[0] == NULL)
  119. {
  120. avpicture_alloc(&p_schro_params->dec_pic,
  121. avccontext->pix_fmt,
  122. avccontext->width,
  123. avccontext->height);
  124. }
  125. }
  126. static int libschroedinger_decode_frame(AVCodecContext *avccontext,
  127. void *data, int *data_size,
  128. const uint8_t *buf, int buf_size)
  129. {
  130. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
  131. SchroDecoder *decoder = p_schro_params->decoder;
  132. SchroVideoFormat *format;
  133. AVPicture *picture = data;
  134. SchroBuffer *enc_buf;
  135. SchroFrame* frame;
  136. int state;
  137. int go = 1;
  138. *data_size = 0;
  139. if (buf_size>0) {
  140. unsigned char *in_buf = av_malloc(buf_size);
  141. memcpy (in_buf, buf, buf_size);
  142. enc_buf = schro_buffer_new_with_data (in_buf, buf_size);
  143. enc_buf->free = libschroedinger_decode_buffer_free;
  144. enc_buf->priv = in_buf;
  145. /* Push buffer into decoder. */
  146. state = schro_decoder_push (decoder, enc_buf);
  147. if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
  148. libschroedinger_handle_first_access_unit(avccontext);
  149. } else {
  150. if (!p_schro_params->eos_signalled) {
  151. state = schro_decoder_push_end_of_stream(decoder);
  152. p_schro_params->eos_signalled = 1;
  153. }
  154. }
  155. format = p_schro_params->format;
  156. while (go) {
  157. /* Parse data and process result. */
  158. state = schro_decoder_wait (decoder);
  159. switch (state)
  160. {
  161. case SCHRO_DECODER_FIRST_ACCESS_UNIT:
  162. libschroedinger_handle_first_access_unit (avccontext);
  163. break;
  164. case SCHRO_DECODER_NEED_BITS:
  165. /* Need more input data - stop iterating over what we have. */
  166. go = 0;
  167. break;
  168. case SCHRO_DECODER_NEED_FRAME:
  169. /* Decoder needs a frame - create one and push it in. */
  170. frame = schro_frame_new_and_alloc(NULL,
  171. p_schro_params->frame_format,
  172. format->width,
  173. format->height);
  174. schro_decoder_add_output_picture (decoder, frame);
  175. break;
  176. case SCHRO_DECODER_OK:
  177. /* Pull a frame out of the decoder. */
  178. frame = schro_decoder_pull (decoder);
  179. if (frame) {
  180. ff_dirac_schro_queue_push_back(
  181. &p_schro_params->dec_frame_queue,
  182. frame);
  183. }
  184. break;
  185. case SCHRO_DECODER_EOS:
  186. go = 0;
  187. p_schro_params->eos_pulled = 1;
  188. schro_decoder_reset (decoder);
  189. break;
  190. case SCHRO_DECODER_ERROR:
  191. return -1;
  192. break;
  193. }
  194. }
  195. /* Grab next frame to be returned from the top of the queue. */
  196. frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue);
  197. if (frame != NULL) {
  198. memcpy (p_schro_params->dec_pic.data[0],
  199. frame->components[0].data,
  200. frame->components[0].length);
  201. memcpy (p_schro_params->dec_pic.data[1],
  202. frame->components[1].data,
  203. frame->components[1].length);
  204. memcpy (p_schro_params->dec_pic.data[2],
  205. frame->components[2].data,
  206. frame->components[2].length);
  207. /* Fill picture with current buffer data from Schroedinger. */
  208. avpicture_fill(picture, p_schro_params->dec_pic.data[0],
  209. avccontext->pix_fmt,
  210. avccontext->width, avccontext->height);
  211. *data_size = sizeof(AVPicture);
  212. /* Now free the frame resources. */
  213. libschroedinger_decode_frame_free (frame);
  214. }
  215. return buf_size;
  216. }
  217. static int libschroedinger_decode_close(AVCodecContext *avccontext)
  218. {
  219. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
  220. /* Free the decoder. */
  221. schro_decoder_free (p_schro_params->decoder);
  222. av_freep(&p_schro_params->format);
  223. avpicture_free (&p_schro_params->dec_pic);
  224. /* Free data in the output frame queue. */
  225. ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
  226. libschroedinger_decode_frame_free);
  227. return 0 ;
  228. }
  229. static void libschroedinger_flush (AVCodecContext *avccontext)
  230. {
  231. /* Got a seek request. Free the decoded frames queue and then reset
  232. * the decoder */
  233. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
  234. /* Free data in the output frame queue. */
  235. ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
  236. libschroedinger_decode_frame_free);
  237. ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
  238. schro_decoder_reset(p_schro_params->decoder);
  239. p_schro_params->eos_pulled = 0;
  240. p_schro_params->eos_signalled = 0;
  241. }
  242. AVCodec libschroedinger_decoder = {
  243. "libschroedinger",
  244. CODEC_TYPE_VIDEO,
  245. CODEC_ID_DIRAC,
  246. sizeof(FfmpegSchroDecoderParams),
  247. libschroedinger_decode_init,
  248. NULL,
  249. libschroedinger_decode_close,
  250. libschroedinger_decode_frame,
  251. CODEC_CAP_DELAY,
  252. .flush = libschroedinger_flush,
  253. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  254. };