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.

211 lines
5.8KB

  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. */
  25. extern "C" {
  26. #include "avcodec.h"
  27. }
  28. #include <stdlib.h>
  29. #include <utvideo/utvideo.h>
  30. #include <utvideo/Codec.h>
  31. #include "get_bits.h"
  32. typedef struct {
  33. uint32_t version;
  34. uint32_t original_format;
  35. uint32_t frameinfo_size;
  36. uint32_t flags;
  37. } UtVideoExtra;
  38. typedef struct {
  39. CCodec *codec;
  40. uint8_t *output;
  41. } UtVideoContext;
  42. static av_cold int utvideo_decode_init(AVCodecContext *avctx)
  43. {
  44. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  45. UtVideoExtra info;
  46. int format;
  47. int begin_ret;
  48. unsigned int buf_size;
  49. if(avctx->extradata_size != 4*4)
  50. {
  51. av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
  52. return -1;
  53. }
  54. /* Read extradata */
  55. info.version = AV_RL32(avctx->extradata);
  56. info.original_format = AV_RL32(avctx->extradata + 4);
  57. info.frameinfo_size = AV_RL32(avctx->extradata + 8);
  58. info.flags = AV_RL32(avctx->extradata + 12);
  59. /* Pick format based on FOURCC */
  60. switch(avctx->codec_tag)
  61. {
  62. case MKTAG('U', 'L', 'Y', '0'):
  63. avctx->pix_fmt = PIX_FMT_YUV420P;
  64. format = UTVF_YV12;
  65. break;
  66. case MKTAG('U', 'L', 'Y', '2'):
  67. avctx->pix_fmt = PIX_FMT_YUYV422;
  68. format = UTVF_YUY2;
  69. break;
  70. case MKTAG('U', 'L', 'R', 'G'):
  71. avctx->pix_fmt = PIX_FMT_BGR24;
  72. format = UTVF_RGB24_WIN;
  73. break;
  74. case MKTAG('U', 'L', 'R', 'A'):
  75. avctx->pix_fmt = PIX_FMT_RGB32;
  76. format = UTVF_RGB32_WIN;
  77. break;
  78. default:
  79. av_log(avctx, AV_LOG_ERROR,
  80. "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
  81. return -1;
  82. }
  83. /* Only allocate the buffer once */
  84. buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  85. utv->output = (uint8_t *)av_malloc(buf_size * sizeof(uint8_t));
  86. if(utv->output == NULL)
  87. {
  88. av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
  89. return -1;
  90. }
  91. /* Allocate the output frame */
  92. avctx->coded_frame = avcodec_alloc_frame();
  93. /* Ut Video only supports 8-bit */
  94. avctx->bits_per_raw_sample = 8;
  95. /* Is it interlaced? */
  96. avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
  97. /* Apparently Ut Video doesn't store this info... */
  98. avctx->coded_frame->top_field_first = 1;
  99. /*
  100. * Create a Ut Video instance. Since the function wants
  101. * an "interface name" string, pass it the name of the lib.
  102. */
  103. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  104. /* Initialize Decoding */
  105. begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
  106. CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
  107. /* Check to see if the decoder initlized properly */
  108. if(begin_ret != 0)
  109. {
  110. av_log(avctx, AV_LOG_ERROR,
  111. "Could not initialize decoder: %d\n", begin_ret);
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
  117. int *data_size, AVPacket *avpkt)
  118. {
  119. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  120. AVFrame *pic = avctx->coded_frame;
  121. int w = avctx->width, h = avctx->height;
  122. /* Set flags */
  123. pic->reference = 0;
  124. pic->pict_type = AV_PICTURE_TYPE_I;
  125. pic->key_frame = 1;
  126. /* Decode the frame */
  127. utv->codec->DecodeFrame(utv->output, avpkt->data, true);
  128. /* Set the output data depending on the colorspace */
  129. switch(avctx->pix_fmt)
  130. {
  131. case PIX_FMT_YUV420P:
  132. pic->linesize[0] = w;
  133. pic->linesize[1] = pic->linesize[2] = w / 2;
  134. pic->data[0] = utv->output;
  135. pic->data[2] = utv->output + (w * h);
  136. pic->data[1] = pic->data[2] + (w * h / 4);
  137. break;
  138. case PIX_FMT_YUYV422:
  139. pic->linesize[0] = w * 2;
  140. pic->data[0] = utv->output;
  141. break;
  142. case PIX_FMT_BGR24:
  143. case PIX_FMT_RGB32:
  144. /* Make the linesize negative, since Ut Video uses bottom-up BGR */
  145. pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
  146. pic->data[0] = utv->output + utv->buf_size + pic->linesize[0];
  147. break;
  148. }
  149. *data_size = sizeof(AVFrame);
  150. *(AVFrame *)data = *pic;
  151. return avpkt->size;
  152. }
  153. static av_cold int utvideo_decode_close(AVCodecContext *avctx)
  154. {
  155. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  156. /* Free output */
  157. av_freep(&avctx->coded_frame);
  158. av_freep(&utv->output);
  159. /* Finish decoding and clean up the instance */
  160. utv->codec->DecodeEnd();
  161. CCodec::DeleteInstance(utv->codec);
  162. return 0;
  163. }
  164. AVCodec ff_libutvideo_decoder = {
  165. "libutvideo",
  166. AVMEDIA_TYPE_VIDEO,
  167. CODEC_ID_UTVIDEO,
  168. sizeof(UtVideoContext),
  169. utvideo_decode_init,
  170. NULL,
  171. utvideo_decode_close,
  172. utvideo_decode_frame,
  173. NULL,
  174. NULL,
  175. NULL,
  176. NULL,
  177. NULL,
  178. NULL_IF_CONFIG_SMALL("Ut Video"),
  179. };