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.

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