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.

283 lines
8.3KB

  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. #include "libavutil/imgutils.h"
  29. }
  30. #include "libutvideo.h"
  31. #include "get_bits.h"
  32. static av_cold int utvideo_decode_init(AVCodecContext *avctx)
  33. {
  34. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  35. UtVideoExtra info;
  36. int format;
  37. int begin_ret;
  38. if (avctx->extradata_size != 16 && avctx->extradata_size != 8 ) {
  39. av_log(avctx, AV_LOG_ERROR, "Extradata size (%d) mismatch.\n", avctx->extradata_size);
  40. return -1;
  41. }
  42. /* Read extradata */
  43. info.version = AV_RL32(avctx->extradata);
  44. info.original_format = AV_RL32(avctx->extradata + 4);
  45. info.frameinfo_size = AV_RL32(avctx->extradata + 8);
  46. info.flags = AV_RL32(avctx->extradata + 12);
  47. /* Pick format based on FOURCC */
  48. switch (avctx->codec_tag) {
  49. #ifdef UTV_BT709
  50. case MKTAG('U', 'L', 'H', '0'):
  51. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  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->colorspace = AVCOL_SPC_BT709;
  58. format = UTVF_YUY2;
  59. break;
  60. #endif
  61. case MKTAG('U', 'L', 'Y', '0'):
  62. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  63. format = UTVF_YV12;
  64. break;
  65. case MKTAG('U', 'L', 'Y', '2'):
  66. avctx->pix_fmt = AV_PIX_FMT_YUYV422;
  67. format = UTVF_YUY2;
  68. break;
  69. case MKTAG('U', 'L', 'R', 'G'):
  70. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  71. format = UTVF_NFCC_BGR_BU;
  72. break;
  73. case MKTAG('U', 'L', 'R', 'A'):
  74. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  75. format = UTVF_NFCC_BGRA_BU;
  76. break;
  77. #ifdef UTVF_UQY2
  78. case MKTAG('U', 'Q', 'Y', '2'):
  79. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  80. format = UTVF_v210;
  81. break;
  82. #endif
  83. default:
  84. av_log(avctx, AV_LOG_ERROR,
  85. "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
  86. return -1;
  87. }
  88. /* Only allocate the buffer once */
  89. utv->buf_size = av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
  90. #ifdef UTVF_UQY2
  91. if (format == UTVF_v210)
  92. utv->buf_size += avctx->height * ((avctx->width + 47) / 48) * 128; // the linesize used by the decoder, this does not seem to be exported
  93. #endif
  94. utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
  95. if (utv->buffer == NULL) {
  96. av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
  97. return -1;
  98. }
  99. /* Allocate the output frame */
  100. avctx->coded_frame = av_frame_alloc();
  101. /* Ut Video only supports 8-bit */
  102. avctx->bits_per_raw_sample = 8;
  103. /* Is it interlaced? */
  104. avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
  105. /* Apparently Ut Video doesn't store this info... */
  106. avctx->coded_frame->top_field_first = 1;
  107. /*
  108. * Create a Ut Video instance. Since the function wants
  109. * an "interface name" string, pass it the name of the lib.
  110. */
  111. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  112. /* Initialize Decoding */
  113. begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
  114. CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
  115. /* Check to see if the decoder initlized properly */
  116. if (begin_ret != 0) {
  117. av_log(avctx, AV_LOG_ERROR,
  118. "Could not initialize decoder: %d\n", begin_ret);
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
  124. int *got_frame, AVPacket *avpkt)
  125. {
  126. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  127. AVFrame *pic = avctx->coded_frame;
  128. int w = avctx->width, h = avctx->height;
  129. /* Set flags */
  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. pic->width = w;
  199. pic->height = h;
  200. pic->format = avctx->pix_fmt;
  201. if (avctx->refcounted_frames) {
  202. int ret = av_frame_ref((AVFrame*)data, pic);
  203. if (ret < 0)
  204. return ret;
  205. } else {
  206. av_frame_move_ref((AVFrame*)data, pic);
  207. }
  208. *got_frame = 1;
  209. return avpkt->size;
  210. }
  211. static av_cold int utvideo_decode_close(AVCodecContext *avctx)
  212. {
  213. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  214. /* Free output */
  215. av_frame_free(&avctx->coded_frame);
  216. av_freep(&utv->buffer);
  217. /* Finish decoding and clean up the instance */
  218. utv->codec->DecodeEnd();
  219. CCodec::DeleteInstance(utv->codec);
  220. return 0;
  221. }
  222. AVCodec ff_libutvideo_decoder = {
  223. "libutvideo",
  224. NULL_IF_CONFIG_SMALL("Ut Video"),
  225. AVMEDIA_TYPE_VIDEO,
  226. AV_CODEC_ID_UTVIDEO,
  227. 0, //capabilities
  228. NULL, //supported_framerates
  229. NULL, //pix_fmts
  230. NULL, //supported_samplerates
  231. NULL, //sample_fmts
  232. NULL, //channel_layouts
  233. 0, //max_lowres
  234. NULL, //priv_class
  235. NULL, //profiles
  236. sizeof(UtVideoContext),
  237. NULL, //next
  238. NULL, //init_thread_copy
  239. NULL, //update_thread_context
  240. NULL, //defaults
  241. NULL, //init_static_data
  242. utvideo_decode_init,
  243. NULL, //encode
  244. NULL, //encode2
  245. utvideo_decode_frame,
  246. utvideo_decode_close,
  247. };