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.

225 lines
6.9KB

  1. /*
  2. * Creative YUV (CYUV) Video Decoder
  3. * by Mike Melanson (melanson@pcisys.net)
  4. * based on "Creative YUV (CYUV) stream format for AVI":
  5. * http://www.csse.monash.edu.au/~timf/videocodec/cyuv.txt
  6. *
  7. * Copyright (C) 2003 the ffmpeg project
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /**
  26. * @file
  27. * Creative YUV (CYUV) Video Decoder.
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "avcodec.h"
  33. #include "dsputil.h"
  34. #include "internal.h"
  35. #include "libavutil/internal.h"
  36. typedef struct CyuvDecodeContext {
  37. AVCodecContext *avctx;
  38. int width, height;
  39. AVFrame frame;
  40. } CyuvDecodeContext;
  41. static av_cold int cyuv_decode_init(AVCodecContext *avctx)
  42. {
  43. CyuvDecodeContext *s = avctx->priv_data;
  44. s->avctx = avctx;
  45. s->width = avctx->width;
  46. /* width needs to be divisible by 4 for this codec to work */
  47. if (s->width & 0x3)
  48. return AVERROR_INVALIDDATA;
  49. s->height = avctx->height;
  50. avcodec_get_frame_defaults(&s->frame);
  51. return 0;
  52. }
  53. static int cyuv_decode_frame(AVCodecContext *avctx,
  54. void *data, int *got_frame,
  55. AVPacket *avpkt)
  56. {
  57. const uint8_t *buf = avpkt->data;
  58. int buf_size = avpkt->size;
  59. CyuvDecodeContext *s=avctx->priv_data;
  60. unsigned char *y_plane;
  61. unsigned char *u_plane;
  62. unsigned char *v_plane;
  63. int y_ptr;
  64. int u_ptr;
  65. int v_ptr;
  66. /* prediction error tables (make it clear that they are signed values) */
  67. const signed char *y_table = (const signed char*)buf + 0;
  68. const signed char *u_table = (const signed char*)buf + 16;
  69. const signed char *v_table = (const signed char*)buf + 32;
  70. unsigned char y_pred, u_pred, v_pred;
  71. int stream_ptr;
  72. unsigned char cur_byte;
  73. int pixel_groups;
  74. int rawsize = s->height * FFALIGN(s->width,2) * 2;
  75. int ret;
  76. if (avctx->codec_id == AV_CODEC_ID_AURA) {
  77. y_table = u_table;
  78. u_table = v_table;
  79. }
  80. /* sanity check the buffer size: A buffer has 3x16-bytes tables
  81. * followed by (height) lines each with 3 bytes to represent groups
  82. * of 4 pixels. Thus, the total size of the buffer ought to be:
  83. * (3 * 16) + height * (width * 3 / 4) */
  84. if (buf_size == 48 + s->height * (s->width * 3 / 4)) {
  85. avctx->pix_fmt = AV_PIX_FMT_YUV411P;
  86. } else if(buf_size == rawsize ) {
  87. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  88. } else {
  89. av_log(avctx, AV_LOG_ERROR, "got a buffer with %d bytes when %d were expected\n",
  90. buf_size, 48 + s->height * (s->width * 3 / 4));
  91. return AVERROR_INVALIDDATA;
  92. }
  93. /* pixel data starts 48 bytes in, after 3x16-byte tables */
  94. stream_ptr = 48;
  95. if (s->frame.data[0])
  96. avctx->release_buffer(avctx, &s->frame);
  97. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  98. s->frame.reference = 0;
  99. if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
  100. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  101. return ret;
  102. }
  103. y_plane = s->frame.data[0];
  104. u_plane = s->frame.data[1];
  105. v_plane = s->frame.data[2];
  106. if (buf_size == rawsize) {
  107. int linesize = FFALIGN(s->width,2) * 2;
  108. y_plane += s->frame.linesize[0] * s->height;
  109. for (stream_ptr = 0; stream_ptr < rawsize; stream_ptr += linesize) {
  110. y_plane -= s->frame.linesize[0];
  111. memcpy(y_plane, buf+stream_ptr, linesize);
  112. }
  113. } else {
  114. /* iterate through each line in the height */
  115. for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
  116. y_ptr < (s->height * s->frame.linesize[0]);
  117. y_ptr += s->frame.linesize[0] - s->width,
  118. u_ptr += s->frame.linesize[1] - s->width / 4,
  119. v_ptr += s->frame.linesize[2] - s->width / 4) {
  120. /* reset predictors */
  121. cur_byte = buf[stream_ptr++];
  122. u_plane[u_ptr++] = u_pred = cur_byte & 0xF0;
  123. y_plane[y_ptr++] = y_pred = (cur_byte & 0x0F) << 4;
  124. cur_byte = buf[stream_ptr++];
  125. v_plane[v_ptr++] = v_pred = cur_byte & 0xF0;
  126. y_pred += y_table[cur_byte & 0x0F];
  127. y_plane[y_ptr++] = y_pred;
  128. cur_byte = buf[stream_ptr++];
  129. y_pred += y_table[cur_byte & 0x0F];
  130. y_plane[y_ptr++] = y_pred;
  131. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  132. y_plane[y_ptr++] = y_pred;
  133. /* iterate through the remaining pixel groups (4 pixels/group) */
  134. pixel_groups = s->width / 4 - 1;
  135. while (pixel_groups--) {
  136. cur_byte = buf[stream_ptr++];
  137. u_pred += u_table[(cur_byte & 0xF0) >> 4];
  138. u_plane[u_ptr++] = u_pred;
  139. y_pred += y_table[cur_byte & 0x0F];
  140. y_plane[y_ptr++] = y_pred;
  141. cur_byte = buf[stream_ptr++];
  142. v_pred += v_table[(cur_byte & 0xF0) >> 4];
  143. v_plane[v_ptr++] = v_pred;
  144. y_pred += y_table[cur_byte & 0x0F];
  145. y_plane[y_ptr++] = y_pred;
  146. cur_byte = buf[stream_ptr++];
  147. y_pred += y_table[cur_byte & 0x0F];
  148. y_plane[y_ptr++] = y_pred;
  149. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  150. y_plane[y_ptr++] = y_pred;
  151. }
  152. }
  153. }
  154. *got_frame = 1;
  155. *(AVFrame*)data= s->frame;
  156. return buf_size;
  157. }
  158. static av_cold int cyuv_decode_end(AVCodecContext *avctx)
  159. {
  160. CyuvDecodeContext *s = avctx->priv_data;
  161. if (s->frame.data[0])
  162. avctx->release_buffer(avctx, &s->frame);
  163. return 0;
  164. }
  165. #if CONFIG_AURA_DECODER
  166. AVCodec ff_aura_decoder = {
  167. .name = "aura",
  168. .type = AVMEDIA_TYPE_VIDEO,
  169. .id = AV_CODEC_ID_AURA,
  170. .priv_data_size = sizeof(CyuvDecodeContext),
  171. .init = cyuv_decode_init,
  172. .close = cyuv_decode_end,
  173. .decode = cyuv_decode_frame,
  174. .capabilities = CODEC_CAP_DR1,
  175. .long_name = NULL_IF_CONFIG_SMALL("Auravision AURA"),
  176. };
  177. #endif
  178. #if CONFIG_CYUV_DECODER
  179. AVCodec ff_cyuv_decoder = {
  180. .name = "cyuv",
  181. .type = AVMEDIA_TYPE_VIDEO,
  182. .id = AV_CODEC_ID_CYUV,
  183. .priv_data_size = sizeof(CyuvDecodeContext),
  184. .init = cyuv_decode_init,
  185. .close = cyuv_decode_end,
  186. .decode = cyuv_decode_frame,
  187. .capabilities = CODEC_CAP_DR1,
  188. .long_name = NULL_IF_CONFIG_SMALL("Creative YUV (CYUV)"),
  189. };
  190. #endif