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.

200 lines
5.7KB

  1. /*
  2. * Copyright (c) 2011 Derek Buitenhuis
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation;
  9. * version 2 of the License.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Known FOURCCs:
  23. * 'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA)
  24. */
  25. extern "C" {
  26. #include "avcodec.h"
  27. }
  28. #include "libutvideo.h"
  29. #include "get_bits.h"
  30. static av_cold int utvideo_decode_init(AVCodecContext *avctx)
  31. {
  32. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  33. UtVideoExtra info;
  34. int format;
  35. int begin_ret;
  36. if (avctx->extradata_size != 4*4) {
  37. av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
  38. return -1;
  39. }
  40. /* Read extradata */
  41. info.version = AV_RL32(avctx->extradata);
  42. info.original_format = AV_RL32(avctx->extradata + 4);
  43. info.frameinfo_size = AV_RL32(avctx->extradata + 8);
  44. info.flags = AV_RL32(avctx->extradata + 12);
  45. /* Pick format based on FOURCC */
  46. switch (avctx->codec_tag) {
  47. case MKTAG('U', 'L', 'Y', '0'):
  48. avctx->pix_fmt = PIX_FMT_YUV420P;
  49. format = UTVF_YV12;
  50. break;
  51. case MKTAG('U', 'L', 'Y', '2'):
  52. avctx->pix_fmt = PIX_FMT_YUYV422;
  53. format = UTVF_YUY2;
  54. break;
  55. case MKTAG('U', 'L', 'R', 'G'):
  56. avctx->pix_fmt = PIX_FMT_BGR24;
  57. format = UTVF_RGB24_WIN;
  58. break;
  59. case MKTAG('U', 'L', 'R', 'A'):
  60. avctx->pix_fmt = PIX_FMT_RGB32;
  61. format = UTVF_RGB32_WIN;
  62. break;
  63. default:
  64. av_log(avctx, AV_LOG_ERROR,
  65. "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
  66. return -1;
  67. }
  68. /* Only allocate the buffer once */
  69. utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  70. utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
  71. if (utv->buffer == NULL) {
  72. av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
  73. return -1;
  74. }
  75. /* Allocate the output frame */
  76. avctx->coded_frame = avcodec_alloc_frame();
  77. /* Ut Video only supports 8-bit */
  78. avctx->bits_per_raw_sample = 8;
  79. /* Is it interlaced? */
  80. avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
  81. /* Apparently Ut Video doesn't store this info... */
  82. avctx->coded_frame->top_field_first = 1;
  83. /*
  84. * Create a Ut Video instance. Since the function wants
  85. * an "interface name" string, pass it the name of the lib.
  86. */
  87. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  88. /* Initialize Decoding */
  89. begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
  90. CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
  91. /* Check to see if the decoder initlized properly */
  92. if (begin_ret != 0) {
  93. av_log(avctx, AV_LOG_ERROR,
  94. "Could not initialize decoder: %d\n", begin_ret);
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
  100. int *data_size, AVPacket *avpkt)
  101. {
  102. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  103. AVFrame *pic = avctx->coded_frame;
  104. int w = avctx->width, h = avctx->height;
  105. /* Set flags */
  106. pic->reference = 0;
  107. pic->pict_type = AV_PICTURE_TYPE_I;
  108. pic->key_frame = 1;
  109. /* Decode the frame */
  110. utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
  111. /* Set the output data depending on the colorspace */
  112. switch (avctx->pix_fmt) {
  113. case PIX_FMT_YUV420P:
  114. pic->linesize[0] = w;
  115. pic->linesize[1] = pic->linesize[2] = w / 2;
  116. pic->data[0] = utv->buffer;
  117. pic->data[2] = utv->buffer + (w * h);
  118. pic->data[1] = pic->data[2] + (w * h / 4);
  119. break;
  120. case PIX_FMT_YUYV422:
  121. pic->linesize[0] = w * 2;
  122. pic->data[0] = utv->buffer;
  123. break;
  124. case PIX_FMT_BGR24:
  125. case PIX_FMT_RGB32:
  126. /* Make the linesize negative, since Ut Video uses bottom-up BGR */
  127. pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
  128. pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
  129. break;
  130. }
  131. *data_size = sizeof(AVFrame);
  132. *(AVFrame *)data = *pic;
  133. return avpkt->size;
  134. }
  135. static av_cold int utvideo_decode_close(AVCodecContext *avctx)
  136. {
  137. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  138. /* Free output */
  139. av_freep(&avctx->coded_frame);
  140. av_freep(&utv->buffer);
  141. /* Finish decoding and clean up the instance */
  142. utv->codec->DecodeEnd();
  143. CCodec::DeleteInstance(utv->codec);
  144. return 0;
  145. }
  146. AVCodec ff_libutvideo_decoder = {
  147. "libutvideo",
  148. NULL_IF_CONFIG_SMALL("Ut Video"),
  149. AVMEDIA_TYPE_VIDEO,
  150. AV_CODEC_ID_UTVIDEO,
  151. 0, //capabilities
  152. NULL, //supported_framerates
  153. NULL, //pix_fmts
  154. NULL, //supported_samplerates
  155. NULL, //sample_fmts
  156. NULL, //channel_layouts
  157. 0, //max_lowres
  158. NULL, //priv_class
  159. NULL, //profiles
  160. sizeof(UtVideoContext),
  161. NULL, //next
  162. NULL, //init_thread_copy
  163. NULL, //update_thread_context
  164. NULL, //defaults
  165. NULL, //init_static_data
  166. utvideo_decode_init,
  167. NULL, //encode
  168. NULL, //encode2
  169. utvideo_decode_frame,
  170. utvideo_decode_close,
  171. };