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.

202 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. 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 format;
  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.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. {
  61. case MKTAG('U', 'L', 'Y', '0'):
  62. avctx->pix_fmt = PIX_FMT_YUV420P;
  63. format = UTVF_YV12;
  64. break;
  65. case MKTAG('U', 'L', 'Y', '2'):
  66. avctx->pix_fmt = PIX_FMT_YUYV422;
  67. format = UTVF_YUY2;
  68. break;
  69. case MKTAG('U', 'L', 'R', 'G'):
  70. avctx->pix_fmt = PIX_FMT_BGR24;
  71. format = UTVF_RGB24_WIN;
  72. break;
  73. case MKTAG('U', 'L', 'R', 'A'):
  74. avctx->pix_fmt = PIX_FMT_RGB32;
  75. format = UTVF_RGB32_WIN;
  76. break;
  77. default:
  78. av_log(avctx, AV_LOG_ERROR,
  79. "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
  80. return -1;
  81. }
  82. /* Only allocate the buffer once */
  83. utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  84. utv->output = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
  85. if(utv->output == NULL)
  86. {
  87. av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
  88. return -1;
  89. }
  90. /* Allocate the output frame */
  91. avctx->coded_frame = avcodec_alloc_frame();
  92. /* Ut Video only supports 8-bit */
  93. avctx->bits_per_raw_sample = 8;
  94. /* Is it interlaced? */
  95. avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
  96. /* Apparently Ut Video doesn't store this info... */
  97. avctx->coded_frame->top_field_first = 1;
  98. /*
  99. * Create a Ut Video instance. Since the function wants
  100. * an "interface name" string, pass it the name of the lib.
  101. */
  102. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  103. /* Initialize Decoding */
  104. utv->codec->DecodeBegin(format, avctx->width, avctx->height,
  105. CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
  106. return 0;
  107. }
  108. static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
  109. int *data_size, AVPacket *avpkt)
  110. {
  111. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  112. AVFrame *pic = avctx->coded_frame;
  113. unsigned int w = avctx->width, h = avctx->height;
  114. /* Set flags */
  115. pic->reference = 0;
  116. pic->pict_type = AV_PICTURE_TYPE_I;
  117. pic->key_frame = 1;
  118. /* Decode the frame */
  119. utv->codec->DecodeFrame(utv->output, avpkt->data, true);
  120. /* Set the output data depending on the colorspace */
  121. switch(avctx->pix_fmt)
  122. {
  123. case PIX_FMT_YUV420P:
  124. pic->linesize[0] = w;
  125. pic->linesize[1] = pic->linesize[2] = w / 2;
  126. pic->data[0] = utv->output;
  127. pic->data[2] = utv->output + (w * h);
  128. pic->data[1] = pic->data[2] + (w * h / 4);
  129. break;
  130. case PIX_FMT_YUYV422:
  131. pic->linesize[0] = w * 2;
  132. pic->data[0] = utv->output;
  133. break;
  134. case PIX_FMT_BGR24:
  135. case PIX_FMT_RGB32:
  136. /* Make the linesize negative, since Ut Video uses bottom-up BGR */
  137. pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
  138. pic->data[0] = utv->output + utv->buf_size + pic->linesize[0];
  139. break;
  140. }
  141. *data_size = sizeof(AVFrame);
  142. *(AVFrame *)data = *pic;
  143. return avpkt->size;
  144. }
  145. static av_cold int utvideo_decode_close(AVCodecContext *avctx)
  146. {
  147. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  148. /* Free output */
  149. av_freep(&avctx->coded_frame);
  150. av_freep(&utv->output);
  151. /* Finish decoding and clean up the instance */
  152. utv->codec->DecodeEnd();
  153. CCodec::DeleteInstance(utv->codec);
  154. return 0;
  155. }
  156. AVCodec ff_libutvideo_decoder = {
  157. "libutvideo",
  158. AVMEDIA_TYPE_VIDEO,
  159. CODEC_ID_UTVIDEO,
  160. sizeof(UtVideoContext),
  161. utvideo_decode_init,
  162. NULL,
  163. utvideo_decode_close,
  164. utvideo_decode_frame,
  165. CODEC_CAP_LOSSLESS,
  166. NULL,
  167. NULL,
  168. NULL,
  169. NULL,
  170. NULL_IF_CONFIG_SMALL("Ut Video"),
  171. };