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.

215 lines
6.2KB

  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. * 'ULH0' (YCbCr 4:2:0 BT.709), 'ULH2' (YCbCr 4:2:2 BT.709)
  25. */
  26. extern "C" {
  27. #include "avcodec.h"
  28. }
  29. #include "libutvideo.h"
  30. #include "get_bits.h"
  31. static av_cold int utvideo_decode_init(AVCodecContext *avctx)
  32. {
  33. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  34. UtVideoExtra info;
  35. int format;
  36. int begin_ret;
  37. if (avctx->extradata_size != 4*4) {
  38. av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
  39. return -1;
  40. }
  41. /* Read extradata */
  42. info.version = AV_RL32(avctx->extradata);
  43. info.original_format = AV_RL32(avctx->extradata + 4);
  44. info.frameinfo_size = AV_RL32(avctx->extradata + 8);
  45. info.flags = AV_RL32(avctx->extradata + 12);
  46. /* Pick format based on FOURCC */
  47. switch (avctx->codec_tag) {
  48. #ifdef UTV_BT709
  49. case MKTAG('U', 'L', 'H', '0'):
  50. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  51. avctx->color_primaries = AVCOL_PRI_BT709;
  52. avctx->colorspace = AVCOL_SPC_BT709;
  53. format = UTVF_YV12;
  54. break;
  55. case MKTAG('U', 'L', 'H', '2'):
  56. avctx->pix_fmt = AV_PIX_FMT_YUYV422;
  57. avctx->color_primaries = AVCOL_PRI_BT709;
  58. avctx->colorspace = AVCOL_SPC_BT709;
  59. format = UTVF_YUY2;
  60. break;
  61. #endif
  62. case MKTAG('U', 'L', 'Y', '0'):
  63. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  64. format = UTVF_YV12;
  65. break;
  66. case MKTAG('U', 'L', 'Y', '2'):
  67. avctx->pix_fmt = AV_PIX_FMT_YUYV422;
  68. format = UTVF_YUY2;
  69. break;
  70. case MKTAG('U', 'L', 'R', 'G'):
  71. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  72. format = UTVF_NFCC_BGR_BU;
  73. break;
  74. case MKTAG('U', 'L', 'R', 'A'):
  75. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  76. format = UTVF_NFCC_BGRA_BU;
  77. break;
  78. default:
  79. av_log(avctx, AV_LOG_ERROR,
  80. "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
  81. return -1;
  82. }
  83. /* Only allocate the buffer once */
  84. utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  85. utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
  86. if (utv->buffer == NULL) {
  87. av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
  88. return -1;
  89. }
  90. /* Allocate the output frame */
  91. avctx->coded_frame = avcodec_alloc_frame();
  92. /* Ut Video only supports 8-bit */
  93. avctx->bits_per_raw_sample = 8;
  94. /* Is it interlaced? */
  95. avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
  96. /* Apparently Ut Video doesn't store this info... */
  97. avctx->coded_frame->top_field_first = 1;
  98. /*
  99. * Create a Ut Video instance. Since the function wants
  100. * an "interface name" string, pass it the name of the lib.
  101. */
  102. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  103. /* Initialize Decoding */
  104. begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
  105. CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
  106. /* Check to see if the decoder initlized properly */
  107. if (begin_ret != 0) {
  108. av_log(avctx, AV_LOG_ERROR,
  109. "Could not initialize decoder: %d\n", begin_ret);
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
  115. int *got_frame, AVPacket *avpkt)
  116. {
  117. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  118. AVFrame *pic = avctx->coded_frame;
  119. int w = avctx->width, h = avctx->height;
  120. /* Set flags */
  121. pic->reference = 0;
  122. pic->pict_type = AV_PICTURE_TYPE_I;
  123. pic->key_frame = 1;
  124. /* Decode the frame */
  125. utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
  126. /* Set the output data depending on the colorspace */
  127. switch (avctx->pix_fmt) {
  128. case AV_PIX_FMT_YUV420P:
  129. pic->linesize[0] = w;
  130. pic->linesize[1] = pic->linesize[2] = w / 2;
  131. pic->data[0] = utv->buffer;
  132. pic->data[2] = utv->buffer + (w * h);
  133. pic->data[1] = pic->data[2] + (w * h / 4);
  134. break;
  135. case AV_PIX_FMT_YUYV422:
  136. pic->linesize[0] = w * 2;
  137. pic->data[0] = utv->buffer;
  138. break;
  139. case AV_PIX_FMT_BGR24:
  140. case AV_PIX_FMT_RGB32:
  141. /* Make the linesize negative, since Ut Video uses bottom-up BGR */
  142. pic->linesize[0] = -1 * w * (avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4);
  143. pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
  144. break;
  145. }
  146. *got_frame = 1;
  147. *(AVFrame *)data = *pic;
  148. return avpkt->size;
  149. }
  150. static av_cold int utvideo_decode_close(AVCodecContext *avctx)
  151. {
  152. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  153. /* Free output */
  154. av_freep(&avctx->coded_frame);
  155. av_freep(&utv->buffer);
  156. /* Finish decoding and clean up the instance */
  157. utv->codec->DecodeEnd();
  158. CCodec::DeleteInstance(utv->codec);
  159. return 0;
  160. }
  161. AVCodec ff_libutvideo_decoder = {
  162. "libutvideo",
  163. NULL_IF_CONFIG_SMALL("Ut Video"),
  164. AVMEDIA_TYPE_VIDEO,
  165. AV_CODEC_ID_UTVIDEO,
  166. 0, //capabilities
  167. NULL, //supported_framerates
  168. NULL, //pix_fmts
  169. NULL, //supported_samplerates
  170. NULL, //sample_fmts
  171. NULL, //channel_layouts
  172. 0, //max_lowres
  173. NULL, //priv_class
  174. NULL, //profiles
  175. sizeof(UtVideoContext),
  176. NULL, //next
  177. NULL, //init_thread_copy
  178. NULL, //update_thread_context
  179. NULL, //defaults
  180. NULL, //init_static_data
  181. utvideo_decode_init,
  182. NULL, //encode
  183. NULL, //encode2
  184. utvideo_decode_frame,
  185. utvideo_decode_close,
  186. };