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.

280 lines
8.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. #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. /* Is it interlaced? */
  102. avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
  103. /* Apparently Ut Video doesn't store this info... */
  104. avctx->coded_frame->top_field_first = 1;
  105. /*
  106. * Create a Ut Video instance. Since the function wants
  107. * an "interface name" string, pass it the name of the lib.
  108. */
  109. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  110. /* Initialize Decoding */
  111. begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
  112. CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
  113. /* Check to see if the decoder initlized properly */
  114. if (begin_ret != 0) {
  115. av_log(avctx, AV_LOG_ERROR,
  116. "Could not initialize decoder: %d\n", begin_ret);
  117. return -1;
  118. }
  119. return 0;
  120. }
  121. static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
  122. int *got_frame, AVPacket *avpkt)
  123. {
  124. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  125. AVFrame *pic = avctx->coded_frame;
  126. int w = avctx->width, h = avctx->height;
  127. /* Set flags */
  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. pic->width = w;
  197. pic->height = h;
  198. pic->format = avctx->pix_fmt;
  199. if (avctx->refcounted_frames) {
  200. int ret = av_frame_ref((AVFrame*)data, pic);
  201. if (ret < 0)
  202. return ret;
  203. } else {
  204. av_frame_move_ref((AVFrame*)data, pic);
  205. }
  206. *got_frame = 1;
  207. return avpkt->size;
  208. }
  209. static av_cold int utvideo_decode_close(AVCodecContext *avctx)
  210. {
  211. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  212. /* Free output */
  213. av_frame_free(&avctx->coded_frame);
  214. av_freep(&utv->buffer);
  215. /* Finish decoding and clean up the instance */
  216. utv->codec->DecodeEnd();
  217. CCodec::DeleteInstance(utv->codec);
  218. return 0;
  219. }
  220. AVCodec ff_libutvideo_decoder = {
  221. "libutvideo",
  222. NULL_IF_CONFIG_SMALL("Ut Video"),
  223. AVMEDIA_TYPE_VIDEO,
  224. AV_CODEC_ID_UTVIDEO,
  225. 0, //capabilities
  226. NULL, //supported_framerates
  227. NULL, //pix_fmts
  228. NULL, //supported_samplerates
  229. NULL, //sample_fmts
  230. NULL, //channel_layouts
  231. 0, //max_lowres
  232. NULL, //priv_class
  233. NULL, //profiles
  234. sizeof(UtVideoContext),
  235. NULL, //next
  236. NULL, //init_thread_copy
  237. NULL, //update_thread_context
  238. NULL, //defaults
  239. NULL, //init_static_data
  240. utvideo_decode_init,
  241. NULL, //encode
  242. NULL, //encode2
  243. utvideo_decode_frame,
  244. utvideo_decode_close,
  245. };