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.

206 lines
5.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 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. 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.frameinfo_size = AV_RL32(avctx->extradata + 8);
  57. info.flags = AV_RL32(avctx->extradata + 12);
  58. /* Pick format based on FOURCC */
  59. switch (avctx->codec_tag) {
  60. case MKTAG('U', 'L', 'Y', '0'):
  61. avctx->pix_fmt = PIX_FMT_YUV420P;
  62. format = UTVF_YV12;
  63. break;
  64. case MKTAG('U', 'L', 'Y', '2'):
  65. avctx->pix_fmt = PIX_FMT_YUYV422;
  66. format = UTVF_YUY2;
  67. break;
  68. case MKTAG('U', 'L', 'R', 'G'):
  69. avctx->pix_fmt = PIX_FMT_BGR24;
  70. format = UTVF_RGB24_WIN;
  71. break;
  72. case MKTAG('U', 'L', 'R', 'A'):
  73. avctx->pix_fmt = PIX_FMT_RGB32;
  74. format = UTVF_RGB32_WIN;
  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. buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  83. utv->output = (uint8_t *)av_malloc(buf_size * sizeof(uint8_t));
  84. if (utv->output == 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 = avcodec_alloc_frame();
  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 *data_size, 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->output, avpkt->data, true);
  124. /* Set the output data depending on the colorspace */
  125. switch (avctx->pix_fmt) {
  126. case PIX_FMT_YUV420P:
  127. pic->linesize[0] = w;
  128. pic->linesize[1] = pic->linesize[2] = w / 2;
  129. pic->data[0] = utv->output;
  130. pic->data[2] = utv->output + (w * h);
  131. pic->data[1] = pic->data[2] + (w * h / 4);
  132. break;
  133. case PIX_FMT_YUYV422:
  134. pic->linesize[0] = w * 2;
  135. pic->data[0] = utv->output;
  136. break;
  137. case PIX_FMT_BGR24:
  138. case PIX_FMT_RGB32:
  139. /* Make the linesize negative, since Ut Video uses bottom-up BGR */
  140. pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
  141. pic->data[0] = utv->output + utv->buf_size + pic->linesize[0];
  142. break;
  143. }
  144. *data_size = sizeof(AVFrame);
  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->output);
  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. AVMEDIA_TYPE_VIDEO,
  162. CODEC_ID_UTVIDEO,
  163. sizeof(UtVideoContext),
  164. utvideo_decode_init,
  165. NULL,
  166. utvideo_decode_close,
  167. utvideo_decode_frame,
  168. NULL,
  169. NULL,
  170. NULL,
  171. NULL,
  172. NULL,
  173. NULL_IF_CONFIG_SMALL("Ut Video"),
  174. };