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.

393 lines
13KB

  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
  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 "libavutil/imgutils.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "avcodec.h"
  31. #include "libschroedinger.h"
  32. #include <schroedinger/schro.h>
  33. #include <schroedinger/schrodebug.h>
  34. #include <schroedinger/schrovideoformat.h>
  35. /** SchroFrame and Pts relation */
  36. typedef struct LibSchroFrameContext {
  37. SchroFrame *frame;
  38. int64_t pts;
  39. } LibSchroFrameContext;
  40. /** libschroedinger decoder private data */
  41. typedef struct SchroDecoderParams {
  42. /** Schroedinger video format */
  43. SchroVideoFormat *format;
  44. /** Schroedinger frame format */
  45. SchroFrameFormat frame_format;
  46. /** decoder handle */
  47. SchroDecoder* decoder;
  48. /** queue storing decoded frames */
  49. FFSchroQueue dec_frame_queue;
  50. /** end of sequence signalled */
  51. int eos_signalled;
  52. /** end of sequence pulled */
  53. int eos_pulled;
  54. /** decoded picture */
  55. AVFrame dec_frame;
  56. } SchroDecoderParams;
  57. typedef struct SchroParseUnitContext {
  58. const uint8_t *buf;
  59. int buf_size;
  60. } SchroParseUnitContext;
  61. static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
  62. void *priv)
  63. {
  64. av_freep(&priv);
  65. }
  66. static void parse_context_init(SchroParseUnitContext *parse_ctx,
  67. const uint8_t *buf, int buf_size)
  68. {
  69. parse_ctx->buf = buf;
  70. parse_ctx->buf_size = buf_size;
  71. }
  72. static SchroBuffer *find_next_parse_unit(SchroParseUnitContext *parse_ctx)
  73. {
  74. SchroBuffer *enc_buf = NULL;
  75. int next_pu_offset = 0;
  76. unsigned char *in_buf;
  77. if (parse_ctx->buf_size < 13 ||
  78. parse_ctx->buf[0] != 'B' ||
  79. parse_ctx->buf[1] != 'B' ||
  80. parse_ctx->buf[2] != 'C' ||
  81. parse_ctx->buf[3] != 'D')
  82. return NULL;
  83. next_pu_offset = (parse_ctx->buf[5] << 24) +
  84. (parse_ctx->buf[6] << 16) +
  85. (parse_ctx->buf[7] << 8) +
  86. parse_ctx->buf[8];
  87. if (next_pu_offset == 0 &&
  88. SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
  89. next_pu_offset = 13;
  90. if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
  91. return NULL;
  92. in_buf = av_malloc(next_pu_offset);
  93. if (!in_buf) {
  94. av_log(parse_ctx, AV_LOG_ERROR, "Unable to allocate input buffer\n");
  95. return NULL;
  96. }
  97. memcpy(in_buf, parse_ctx->buf, next_pu_offset);
  98. enc_buf = schro_buffer_new_with_data(in_buf, next_pu_offset);
  99. enc_buf->free = libschroedinger_decode_buffer_free;
  100. enc_buf->priv = in_buf;
  101. parse_ctx->buf += next_pu_offset;
  102. parse_ctx->buf_size -= next_pu_offset;
  103. return enc_buf;
  104. }
  105. /**
  106. * Returns FFmpeg chroma format.
  107. */
  108. static enum PixelFormat get_chroma_format(SchroChromaFormat schro_pix_fmt)
  109. {
  110. int num_formats = sizeof(schro_pixel_format_map) /
  111. sizeof(schro_pixel_format_map[0]);
  112. int idx;
  113. for (idx = 0; idx < num_formats; ++idx)
  114. if (schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt)
  115. return schro_pixel_format_map[idx].ff_pix_fmt;
  116. return PIX_FMT_NONE;
  117. }
  118. static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext)
  119. {
  120. SchroDecoderParams *p_schro_params = avccontext->priv_data;
  121. /* First of all, initialize our supporting libraries. */
  122. schro_init();
  123. schro_debug_set_level(avccontext->debug);
  124. p_schro_params->decoder = schro_decoder_new();
  125. schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
  126. if (!p_schro_params->decoder)
  127. return -1;
  128. /* Initialize the decoded frame queue. */
  129. ff_schro_queue_init(&p_schro_params->dec_frame_queue);
  130. return 0;
  131. }
  132. static void libschroedinger_decode_frame_free(void *frame)
  133. {
  134. schro_frame_unref(frame);
  135. }
  136. static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
  137. {
  138. SchroDecoderParams *p_schro_params = avccontext->priv_data;
  139. SchroDecoder *decoder = p_schro_params->decoder;
  140. p_schro_params->format = schro_decoder_get_video_format(decoder);
  141. /* Tell FFmpeg about sequence details. */
  142. if (av_image_check_size(p_schro_params->format->width,
  143. p_schro_params->format->height, 0, avccontext) < 0) {
  144. av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
  145. p_schro_params->format->width, p_schro_params->format->height);
  146. avccontext->height = avccontext->width = 0;
  147. return;
  148. }
  149. avccontext->height = p_schro_params->format->height;
  150. avccontext->width = p_schro_params->format->width;
  151. avccontext->pix_fmt = get_chroma_format(p_schro_params->format->chroma_format);
  152. if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
  153. &p_schro_params->frame_format) == -1) {
  154. av_log(avccontext, AV_LOG_ERROR,
  155. "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
  156. "and 4:4:4 formats.\n");
  157. return;
  158. }
  159. avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
  160. avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
  161. }
  162. static int libschroedinger_decode_frame(AVCodecContext *avccontext,
  163. void *data, int *data_size,
  164. AVPacket *avpkt)
  165. {
  166. const uint8_t *buf = avpkt->data;
  167. int buf_size = avpkt->size;
  168. int64_t pts = avpkt->pts;
  169. SchroTag *tag;
  170. SchroDecoderParams *p_schro_params = avccontext->priv_data;
  171. SchroDecoder *decoder = p_schro_params->decoder;
  172. SchroBuffer *enc_buf;
  173. SchroFrame* frame;
  174. int state;
  175. int go = 1;
  176. int outer = 1;
  177. SchroParseUnitContext parse_ctx;
  178. LibSchroFrameContext *framewithpts = NULL;
  179. *data_size = 0;
  180. parse_context_init(&parse_ctx, buf, buf_size);
  181. if (!buf_size) {
  182. if (!p_schro_params->eos_signalled) {
  183. state = schro_decoder_push_end_of_stream(decoder);
  184. p_schro_params->eos_signalled = 1;
  185. }
  186. }
  187. /* Loop through all the individual parse units in the input buffer */
  188. do {
  189. if ((enc_buf = find_next_parse_unit(&parse_ctx))) {
  190. /* Set Schrotag with the pts to be recovered after decoding*/
  191. enc_buf->tag = schro_tag_new(av_malloc(sizeof(int64_t)), av_free);
  192. if (!enc_buf->tag->value) {
  193. av_log(avccontext, AV_LOG_ERROR, "Unable to allocate SchroTag\n");
  194. return AVERROR(ENOMEM);
  195. }
  196. AV_WN(64, enc_buf->tag->value, pts);
  197. /* Push buffer into decoder. */
  198. if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) &&
  199. SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0)
  200. avccontext->has_b_frames = 1;
  201. state = schro_decoder_push(decoder, enc_buf);
  202. if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
  203. libschroedinger_handle_first_access_unit(avccontext);
  204. go = 1;
  205. } else
  206. outer = 0;
  207. while (go) {
  208. /* Parse data and process result. */
  209. state = schro_decoder_wait(decoder);
  210. switch (state) {
  211. case SCHRO_DECODER_FIRST_ACCESS_UNIT:
  212. libschroedinger_handle_first_access_unit(avccontext);
  213. break;
  214. case SCHRO_DECODER_NEED_BITS:
  215. /* Need more input data - stop iterating over what we have. */
  216. go = 0;
  217. break;
  218. case SCHRO_DECODER_NEED_FRAME:
  219. /* Decoder needs a frame - create one and push it in. */
  220. frame = ff_create_schro_frame(avccontext,
  221. p_schro_params->frame_format);
  222. schro_decoder_add_output_picture(decoder, frame);
  223. break;
  224. case SCHRO_DECODER_OK:
  225. /* Pull a frame out of the decoder. */
  226. tag = schro_decoder_get_picture_tag(decoder);
  227. frame = schro_decoder_pull(decoder);
  228. if (frame) {
  229. /* Add relation between schroframe and pts. */
  230. framewithpts = av_malloc(sizeof(LibSchroFrameContext));
  231. if (!framewithpts) {
  232. av_log(avccontext, AV_LOG_ERROR, "Unable to allocate FrameWithPts\n");
  233. return AVERROR(ENOMEM);
  234. }
  235. framewithpts->frame = frame;
  236. framewithpts->pts = AV_RN64(tag->value);
  237. ff_schro_queue_push_back(&p_schro_params->dec_frame_queue,
  238. framewithpts);
  239. }
  240. break;
  241. case SCHRO_DECODER_EOS:
  242. go = 0;
  243. p_schro_params->eos_pulled = 1;
  244. schro_decoder_reset(decoder);
  245. outer = 0;
  246. break;
  247. case SCHRO_DECODER_ERROR:
  248. return -1;
  249. break;
  250. }
  251. }
  252. } while (outer);
  253. /* Grab next frame to be returned from the top of the queue. */
  254. framewithpts = ff_schro_queue_pop(&p_schro_params->dec_frame_queue);
  255. if (framewithpts && framewithpts->frame) {
  256. if (p_schro_params->dec_frame.data[0])
  257. avccontext->release_buffer(avccontext, &p_schro_params->dec_frame);
  258. if (avccontext->get_buffer(avccontext, &p_schro_params->dec_frame) < 0) {
  259. av_log(avccontext, AV_LOG_ERROR, "Unable to allocate buffer\n");
  260. return AVERROR(ENOMEM);
  261. }
  262. memcpy(p_schro_params->dec_frame.data[0],
  263. framewithpts->frame->components[0].data,
  264. framewithpts->frame->components[0].length);
  265. memcpy(p_schro_params->dec_frame.data[1],
  266. framewithpts->frame->components[1].data,
  267. framewithpts->frame->components[1].length);
  268. memcpy(p_schro_params->dec_frame.data[2],
  269. framewithpts->frame->components[2].data,
  270. framewithpts->frame->components[2].length);
  271. /* Fill frame with current buffer data from Schroedinger. */
  272. p_schro_params->dec_frame.format = -1; /* Unknown -1 */
  273. p_schro_params->dec_frame.width = framewithpts->frame->width;
  274. p_schro_params->dec_frame.height = framewithpts->frame->height;
  275. p_schro_params->dec_frame.pkt_pts = framewithpts->pts;
  276. p_schro_params->dec_frame.linesize[0] = framewithpts->frame->components[0].stride;
  277. p_schro_params->dec_frame.linesize[1] = framewithpts->frame->components[1].stride;
  278. p_schro_params->dec_frame.linesize[2] = framewithpts->frame->components[2].stride;
  279. *(AVFrame*)data = p_schro_params->dec_frame;
  280. *data_size = sizeof(AVFrame);
  281. /* Now free the frame resources. */
  282. libschroedinger_decode_frame_free(framewithpts->frame);
  283. av_free(framewithpts);
  284. } else {
  285. data = NULL;
  286. *data_size = 0;
  287. }
  288. return buf_size;
  289. }
  290. static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext)
  291. {
  292. SchroDecoderParams *p_schro_params = avccontext->priv_data;
  293. /* Free the decoder. */
  294. schro_decoder_free(p_schro_params->decoder);
  295. av_freep(&p_schro_params->format);
  296. if (p_schro_params->dec_frame.data[0])
  297. avccontext->release_buffer(avccontext, &p_schro_params->dec_frame);
  298. /* Free data in the output frame queue. */
  299. ff_schro_queue_free(&p_schro_params->dec_frame_queue,
  300. libschroedinger_decode_frame_free);
  301. return 0;
  302. }
  303. static void libschroedinger_flush(AVCodecContext *avccontext)
  304. {
  305. /* Got a seek request. Free the decoded frames queue and then reset
  306. * the decoder */
  307. SchroDecoderParams *p_schro_params = avccontext->priv_data;
  308. /* Free data in the output frame queue. */
  309. ff_schro_queue_free(&p_schro_params->dec_frame_queue,
  310. libschroedinger_decode_frame_free);
  311. ff_schro_queue_init(&p_schro_params->dec_frame_queue);
  312. schro_decoder_reset(p_schro_params->decoder);
  313. p_schro_params->eos_pulled = 0;
  314. p_schro_params->eos_signalled = 0;
  315. }
  316. AVCodec ff_libschroedinger_decoder = {
  317. .name = "libschroedinger",
  318. .type = AVMEDIA_TYPE_VIDEO,
  319. .id = CODEC_ID_DIRAC,
  320. .priv_data_size = sizeof(SchroDecoderParams),
  321. .init = libschroedinger_decode_init,
  322. .close = libschroedinger_decode_close,
  323. .decode = libschroedinger_decode_frame,
  324. .capabilities = CODEC_CAP_DELAY,
  325. .flush = libschroedinger_flush,
  326. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  327. };