libutvideodec.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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->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. default:
  77. av_log(avctx, AV_LOG_ERROR,
  78. "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
  79. return -1;
  80. }
  81. /* Only allocate the buffer once */
  82. utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  83. utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
  84. if (utv->buffer == NULL) {
  85. av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
  86. return -1;
  87. }
  88. /* Allocate the output frame */
  89. avctx->coded_frame = av_frame_alloc();
  90. /* Ut Video only supports 8-bit */
  91. avctx->bits_per_raw_sample = 8;
  92. /* Is it interlaced? */
  93. avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
  94. /* Apparently Ut Video doesn't store this info... */
  95. avctx->coded_frame->top_field_first = 1;
  96. /*
  97. * Create a Ut Video instance. Since the function wants
  98. * an "interface name" string, pass it the name of the lib.
  99. */
  100. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  101. /* Initialize Decoding */
  102. begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
  103. CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
  104. /* Check to see if the decoder initlized properly */
  105. if (begin_ret != 0) {
  106. av_log(avctx, AV_LOG_ERROR,
  107. "Could not initialize decoder: %d\n", begin_ret);
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
  113. int *got_frame, AVPacket *avpkt)
  114. {
  115. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  116. AVFrame *pic = avctx->coded_frame;
  117. int w = avctx->width, h = avctx->height;
  118. /* Set flags */
  119. pic->reference = 0;
  120. pic->pict_type = AV_PICTURE_TYPE_I;
  121. pic->key_frame = 1;
  122. /* Decode the frame */
  123. utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
  124. /* Set the output data depending on the colorspace */
  125. switch (avctx->pix_fmt) {
  126. case AV_PIX_FMT_YUV420P:
  127. pic->linesize[0] = w;
  128. pic->linesize[1] = pic->linesize[2] = w / 2;
  129. pic->data[0] = utv->buffer;
  130. pic->data[2] = utv->buffer + (w * h);
  131. pic->data[1] = pic->data[2] + (w * h / 4);
  132. break;
  133. case AV_PIX_FMT_YUYV422:
  134. pic->linesize[0] = w * 2;
  135. pic->data[0] = utv->buffer;
  136. break;
  137. case AV_PIX_FMT_BGR24:
  138. case AV_PIX_FMT_RGB32:
  139. /* Make the linesize negative, since Ut Video uses bottom-up BGR */
  140. pic->linesize[0] = -1 * w * (avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4);
  141. pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
  142. break;
  143. }
  144. *got_frame = 1;
  145. *(AVFrame *)data = *pic;
  146. return avpkt->size;
  147. }
  148. static av_cold int utvideo_decode_close(AVCodecContext *avctx)
  149. {
  150. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  151. /* Free output */
  152. av_freep(&avctx->coded_frame);
  153. av_freep(&utv->buffer);
  154. /* Finish decoding and clean up the instance */
  155. utv->codec->DecodeEnd();
  156. CCodec::DeleteInstance(utv->codec);
  157. return 0;
  158. }
  159. AVCodec ff_libutvideo_decoder = {
  160. "libutvideo",
  161. NULL_IF_CONFIG_SMALL("Ut Video"),
  162. AVMEDIA_TYPE_VIDEO,
  163. AV_CODEC_ID_UTVIDEO,
  164. 0, //capabilities
  165. NULL, //supported_framerates
  166. NULL, //pix_fmts
  167. NULL, //supported_samplerates
  168. NULL, //sample_fmts
  169. NULL, //channel_layouts
  170. 0, //max_lowres
  171. NULL, //priv_class
  172. NULL, //profiles
  173. sizeof(UtVideoContext),
  174. NULL, //next
  175. NULL, //init_thread_copy
  176. NULL, //update_thread_context
  177. NULL, //defaults
  178. NULL, //init_static_data
  179. utvideo_decode_init,
  180. NULL, //encode
  181. NULL, //encode2
  182. utvideo_decode_frame,
  183. utvideo_decode_close,
  184. };