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.

231 lines
6.6KB

  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 stripes;
  36. uint32_t flags;
  37. } UtVideoExtra;
  38. typedef struct {
  39. CCodec *codec;
  40. unsigned int buf_size;
  41. uint8_t *output;
  42. } UtVideoContext;
  43. static av_cold int utvideo_decode_init(AVCodecContext *avctx)
  44. {
  45. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  46. UtVideoExtra info;
  47. int defined_fourcc = 0;
  48. if(avctx->extradata_size != 4*4)
  49. {
  50. av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
  51. return -1;
  52. }
  53. /* Read extradata */
  54. info.version = AV_RL32(avctx->extradata);
  55. info.original_format = AV_RL32(avctx->extradata + 4);
  56. info.stripes = AV_RL32(avctx->extradata + 8);
  57. info.flags = AV_RL32(avctx->extradata + 12);
  58. /* Try to output the original format */
  59. switch(UNFCC(info.original_format))
  60. {
  61. case UTVF_YV12:
  62. avctx->pix_fmt = PIX_FMT_YUV420P;
  63. break;
  64. case UTVF_YUY2:
  65. case UTVF_YUYV:
  66. case UTVF_YUNV:
  67. avctx->pix_fmt = PIX_FMT_YUYV422;
  68. break;
  69. case UTVF_UYVY:
  70. case UTVF_UYNV:
  71. avctx->pix_fmt = PIX_FMT_UYVY422;
  72. break;
  73. case UTVF_RGB24_WIN:
  74. avctx->pix_fmt = PIX_FMT_BGR24;
  75. break;
  76. case UTVF_RGB32_WIN:
  77. avctx->pix_fmt = PIX_FMT_RGB32;
  78. break;
  79. case UTVF_ARGB32_WIN:
  80. avctx->pix_fmt = PIX_FMT_ARGB;
  81. break;
  82. case 0:
  83. /* Fall back on FOURCC */
  84. switch(UNFCC(avctx->codec_tag))
  85. {
  86. case UTVF_ULY0:
  87. avctx->pix_fmt = PIX_FMT_YUV420P;
  88. defined_fourcc = UTVF_YV12;
  89. break;
  90. case UTVF_ULY2:
  91. avctx->pix_fmt = PIX_FMT_YUYV422;
  92. defined_fourcc = UTVF_YUY2;
  93. break;
  94. case UTVF_ULRG:
  95. avctx->pix_fmt = PIX_FMT_BGR24;
  96. defined_fourcc = UTVF_RGB24_WIN;
  97. break;
  98. case UTVF_ULRA:
  99. avctx->pix_fmt = PIX_FMT_RGB32;
  100. defined_fourcc = UTVF_RGB32_WIN;
  101. break;
  102. }
  103. break;
  104. default:
  105. av_log(avctx, AV_LOG_ERROR,
  106. "Codec ExtraData is Corrupt or Invalid: %X\n", info.original_format);
  107. return -1;
  108. }
  109. /* Only allocate the buffer once */
  110. utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  111. utv->output = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
  112. if(utv->output == NULL)
  113. {
  114. av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
  115. return -1;
  116. }
  117. /* Allocate the output frame */
  118. avctx->coded_frame = avcodec_alloc_frame();
  119. /* Ut Video only supports 8-bit */
  120. avctx->bits_per_raw_sample = 8;
  121. /* Is it interlaced? */
  122. avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
  123. /* Apparently Ut Video doesn't store this info... */
  124. avctx->coded_frame->top_field_first = 1;
  125. /*
  126. * Create a Ut Video instance. Since the function wants
  127. * an "interface name" string, pass it the name of the lib.
  128. */
  129. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  130. /* Initialize Decoding */
  131. utv->codec->DecodeBegin(defined_fourcc ? defined_fourcc : UNFCC(info.original_format),
  132. avctx->width, avctx->height, CBGROSSWIDTH_WINDOWS, &info,
  133. sizeof(UtVideoExtra));
  134. return 0;
  135. }
  136. static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
  137. int *data_size, AVPacket *avpkt)
  138. {
  139. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  140. AVFrame *pic = avctx->coded_frame;
  141. unsigned int w = avctx->width, h = avctx->height;
  142. /* Set flags */
  143. pic->reference = 0;
  144. pic->pict_type = AV_PICTURE_TYPE_I;
  145. pic->key_frame = 1;
  146. /* Decode the frame */
  147. utv->codec->DecodeFrame(utv->output, avpkt->data, true);
  148. /* Set the output data depending on the colorspace */
  149. switch(avctx->pix_fmt)
  150. {
  151. case PIX_FMT_YUV420P:
  152. pic->linesize[0] = w;
  153. pic->linesize[1] = pic->linesize[2] = w / 2;
  154. pic->data[0] = utv->output;
  155. pic->data[2] = utv->output + (w * h);
  156. pic->data[1] = pic->data[2] + (w * h / 4);
  157. break;
  158. case PIX_FMT_YUYV422:
  159. case PIX_FMT_UYVY422:
  160. pic->linesize[0] = w * 2;
  161. pic->data[0] = utv->output;
  162. break;
  163. case PIX_FMT_BGR24:
  164. case PIX_FMT_RGB32:
  165. /* Make the linesize negative, since Ut Video uses bottom-up BGR */
  166. pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
  167. pic->data[0] = utv->output + utv->buf_size + pic->linesize[0];
  168. break;
  169. }
  170. *data_size = sizeof(AVFrame);
  171. *(AVFrame *)data = *pic;
  172. return avpkt->size;
  173. }
  174. static av_cold int utvideo_decode_close(AVCodecContext *avctx)
  175. {
  176. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  177. /* Free output */
  178. av_freep(&avctx->coded_frame);
  179. av_freep(&utv->output);
  180. /* Finish decoding and clean up the instance */
  181. utv->codec->DecodeEnd();
  182. CCodec::DeleteInstance(utv->codec);
  183. return 0;
  184. }
  185. AVCodec ff_libutvideo_decoder = {
  186. "utvideo",
  187. AVMEDIA_TYPE_VIDEO,
  188. CODEC_ID_UTVIDEO,
  189. sizeof(UtVideoContext),
  190. utvideo_decode_init,
  191. NULL,
  192. utvideo_decode_close,
  193. utvideo_decode_frame,
  194. CODEC_CAP_LOSSLESS,
  195. NULL,
  196. NULL,
  197. NULL,
  198. NULL,
  199. NULL_IF_CONFIG_SMALL("Ut Video"),
  200. };