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.

273 lines
8.0KB

  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 != 16 && avctx->extradata_size != 8 ) {
  38. av_log(avctx, AV_LOG_ERROR, "Extradata size (%d) mismatch.\n", avctx->extradata_size);
  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->colorspace = AVCOL_SPC_BT709;
  52. format = UTVF_YV12;
  53. break;
  54. case MKTAG('U', 'L', 'H', '2'):
  55. avctx->pix_fmt = AV_PIX_FMT_YUYV422;
  56. avctx->colorspace = AVCOL_SPC_BT709;
  57. format = UTVF_YUY2;
  58. break;
  59. #endif
  60. case MKTAG('U', 'L', 'Y', '0'):
  61. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  62. format = UTVF_YV12;
  63. break;
  64. case MKTAG('U', 'L', 'Y', '2'):
  65. avctx->pix_fmt = AV_PIX_FMT_YUYV422;
  66. format = UTVF_YUY2;
  67. break;
  68. case MKTAG('U', 'L', 'R', 'G'):
  69. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  70. format = UTVF_NFCC_BGR_BU;
  71. break;
  72. case MKTAG('U', 'L', 'R', 'A'):
  73. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  74. format = UTVF_NFCC_BGRA_BU;
  75. break;
  76. #ifdef UTVF_UQY2
  77. case MKTAG('U', 'Q', 'Y', '2'):
  78. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  79. format = UTVF_v210;
  80. break;
  81. #endif
  82. default:
  83. av_log(avctx, AV_LOG_ERROR,
  84. "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
  85. return -1;
  86. }
  87. /* Only allocate the buffer once */
  88. utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  89. #ifdef UTVF_UQY2
  90. if (format == UTVF_v210)
  91. utv->buf_size += avctx->height * ((avctx->width + 47) / 48) * 128; // the linesize used by the decoder, this does not seem to be exported
  92. #endif
  93. utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
  94. if (utv->buffer == NULL) {
  95. av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
  96. return -1;
  97. }
  98. /* Allocate the output frame */
  99. avctx->coded_frame = av_frame_alloc();
  100. /* Ut Video only supports 8-bit */
  101. avctx->bits_per_raw_sample = 8;
  102. /* Is it interlaced? */
  103. avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
  104. /* Apparently Ut Video doesn't store this info... */
  105. avctx->coded_frame->top_field_first = 1;
  106. /*
  107. * Create a Ut Video instance. Since the function wants
  108. * an "interface name" string, pass it the name of the lib.
  109. */
  110. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  111. /* Initialize Decoding */
  112. begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
  113. CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
  114. /* Check to see if the decoder initlized properly */
  115. if (begin_ret != 0) {
  116. av_log(avctx, AV_LOG_ERROR,
  117. "Could not initialize decoder: %d\n", begin_ret);
  118. return -1;
  119. }
  120. return 0;
  121. }
  122. static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
  123. int *got_frame, AVPacket *avpkt)
  124. {
  125. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  126. AVFrame *pic = avctx->coded_frame;
  127. int w = avctx->width, h = avctx->height;
  128. /* Set flags */
  129. pic->reference = 0;
  130. pic->pict_type = AV_PICTURE_TYPE_I;
  131. pic->key_frame = 1;
  132. /* Decode the frame */
  133. utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
  134. /* Set the output data depending on the colorspace */
  135. switch (avctx->pix_fmt) {
  136. case AV_PIX_FMT_YUV420P:
  137. pic->linesize[0] = w;
  138. pic->linesize[1] = pic->linesize[2] = w / 2;
  139. pic->data[0] = utv->buffer;
  140. pic->data[2] = utv->buffer + (w * h);
  141. pic->data[1] = pic->data[2] + (w * h / 4);
  142. break;
  143. case AV_PIX_FMT_YUYV422:
  144. pic->linesize[0] = w * 2;
  145. pic->data[0] = utv->buffer;
  146. break;
  147. case AV_PIX_FMT_YUV422P10: {
  148. uint16_t *y, *u, *v;
  149. int i,j;
  150. int linesize = ((w + 47) / 48) * 128;
  151. pic->linesize[0] = w * 2;
  152. pic->linesize[1] =
  153. pic->linesize[2] = w;
  154. pic->data[0] = utv->buffer + linesize * h;
  155. pic->data[1] = pic->data[0] + h*pic->linesize[0];
  156. pic->data[2] = pic->data[1] + h*pic->linesize[1];
  157. y = (uint16_t*)pic->data[0];
  158. u = (uint16_t*)pic->data[1];
  159. v = (uint16_t*)pic->data[2];
  160. for (j = 0; j < h; j++) {
  161. const uint8_t *in = utv->buffer + j * linesize;
  162. for (i = 0; i + 1 < w; i += 6, in += 4) {
  163. unsigned a,b;
  164. a = AV_RL32(in);
  165. in += 4;
  166. b = AV_RL32(in);
  167. *u++ = (a ) & 0x3FF;
  168. *y++ = (a>>10) & 0x3FF;
  169. *v++ = (a>>20) & 0x3FF;
  170. *y++ = (b ) & 0x3FF;
  171. if (i + 3 >= w)
  172. break;
  173. in += 4;
  174. a = AV_RL32(in);
  175. *u++ = (b>>10) & 0x3FF;
  176. *y++ = (b>>20) & 0x3FF;
  177. *v++ = (a ) & 0x3FF;
  178. *y++ = (a>>10) & 0x3FF;
  179. if (i + 5 >= w)
  180. break;
  181. in += 4;
  182. b = AV_RL32(in);
  183. *u++ = (a>>20) & 0x3FF;
  184. *y++ = (b ) & 0x3FF;
  185. *v++ = (b>>10) & 0x3FF;
  186. *y++ = (b>>20) & 0x3FF;
  187. }
  188. }
  189. break;
  190. }
  191. case AV_PIX_FMT_BGR24:
  192. case AV_PIX_FMT_RGB32:
  193. /* Make the linesize negative, since Ut Video uses bottom-up BGR */
  194. pic->linesize[0] = -1 * w * (avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4);
  195. pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
  196. break;
  197. }
  198. *got_frame = 1;
  199. av_frame_move_ref((AVFrame*)data, pic);
  200. return avpkt->size;
  201. }
  202. static av_cold int utvideo_decode_close(AVCodecContext *avctx)
  203. {
  204. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  205. /* Free output */
  206. av_frame_free(&avctx->coded_frame);
  207. av_freep(&utv->buffer);
  208. /* Finish decoding and clean up the instance */
  209. utv->codec->DecodeEnd();
  210. CCodec::DeleteInstance(utv->codec);
  211. return 0;
  212. }
  213. AVCodec ff_libutvideo_decoder = {
  214. "libutvideo",
  215. NULL_IF_CONFIG_SMALL("Ut Video"),
  216. AVMEDIA_TYPE_VIDEO,
  217. AV_CODEC_ID_UTVIDEO,
  218. 0, //capabilities
  219. NULL, //supported_framerates
  220. NULL, //pix_fmts
  221. NULL, //supported_samplerates
  222. NULL, //sample_fmts
  223. NULL, //channel_layouts
  224. 0, //max_lowres
  225. NULL, //priv_class
  226. NULL, //profiles
  227. sizeof(UtVideoContext),
  228. NULL, //next
  229. NULL, //init_thread_copy
  230. NULL, //update_thread_context
  231. NULL, //defaults
  232. NULL, //init_static_data
  233. utvideo_decode_init,
  234. NULL, //encode
  235. NULL, //encode2
  236. utvideo_decode_frame,
  237. utvideo_decode_close,
  238. };