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.

397 lines
14KB

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