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.

223 lines
6.8KB

  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 "libavutil/internal.h"
  35. typedef struct CyuvDecodeContext {
  36. AVCodecContext *avctx;
  37. int width, height;
  38. AVFrame frame;
  39. } CyuvDecodeContext;
  40. static av_cold int cyuv_decode_init(AVCodecContext *avctx)
  41. {
  42. CyuvDecodeContext *s = avctx->priv_data;
  43. s->avctx = avctx;
  44. s->width = avctx->width;
  45. /* width needs to be divisible by 4 for this codec to work */
  46. if (s->width & 0x3)
  47. return -1;
  48. s->height = avctx->height;
  49. avcodec_get_frame_defaults(&s->frame);
  50. return 0;
  51. }
  52. static int cyuv_decode_frame(AVCodecContext *avctx,
  53. void *data, int *data_size,
  54. AVPacket *avpkt)
  55. {
  56. const uint8_t *buf = avpkt->data;
  57. int buf_size = avpkt->size;
  58. CyuvDecodeContext *s=avctx->priv_data;
  59. unsigned char *y_plane;
  60. unsigned char *u_plane;
  61. unsigned char *v_plane;
  62. int y_ptr;
  63. int u_ptr;
  64. int v_ptr;
  65. /* prediction error tables (make it clear that they are signed values) */
  66. const signed char *y_table = (const signed char*)buf + 0;
  67. const signed char *u_table = (const signed char*)buf + 16;
  68. const signed char *v_table = (const signed char*)buf + 32;
  69. unsigned char y_pred, u_pred, v_pred;
  70. int stream_ptr;
  71. unsigned char cur_byte;
  72. int pixel_groups;
  73. int rawsize = s->height * FFALIGN(s->width,2) * 2;
  74. if (avctx->codec_id == AV_CODEC_ID_AURA) {
  75. y_table = u_table;
  76. u_table = v_table;
  77. }
  78. /* sanity check the buffer size: A buffer has 3x16-bytes tables
  79. * followed by (height) lines each with 3 bytes to represent groups
  80. * of 4 pixels. Thus, the total size of the buffer ought to be:
  81. * (3 * 16) + height * (width * 3 / 4) */
  82. if (buf_size == 48 + s->height * (s->width * 3 / 4)) {
  83. avctx->pix_fmt = AV_PIX_FMT_YUV411P;
  84. } else if(buf_size == rawsize ) {
  85. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  86. } else {
  87. av_log(avctx, AV_LOG_ERROR, "got a buffer with %d bytes when %d were expected\n",
  88. buf_size, 48 + s->height * (s->width * 3 / 4));
  89. return -1;
  90. }
  91. /* pixel data starts 48 bytes in, after 3x16-byte tables */
  92. stream_ptr = 48;
  93. if (s->frame.data[0])
  94. avctx->release_buffer(avctx, &s->frame);
  95. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  96. s->frame.reference = 0;
  97. if (avctx->get_buffer(avctx, &s->frame) < 0) {
  98. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  99. return -1;
  100. }
  101. y_plane = s->frame.data[0];
  102. u_plane = s->frame.data[1];
  103. v_plane = s->frame.data[2];
  104. if (buf_size == rawsize) {
  105. int linesize = FFALIGN(s->width,2) * 2;
  106. y_plane += s->frame.linesize[0] * s->height;
  107. for (stream_ptr = 0; stream_ptr < rawsize; stream_ptr += linesize) {
  108. y_plane -= s->frame.linesize[0];
  109. memcpy(y_plane, buf+stream_ptr, linesize);
  110. }
  111. } else {
  112. /* iterate through each line in the height */
  113. for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
  114. y_ptr < (s->height * s->frame.linesize[0]);
  115. y_ptr += s->frame.linesize[0] - s->width,
  116. u_ptr += s->frame.linesize[1] - s->width / 4,
  117. v_ptr += s->frame.linesize[2] - s->width / 4) {
  118. /* reset predictors */
  119. cur_byte = buf[stream_ptr++];
  120. u_plane[u_ptr++] = u_pred = cur_byte & 0xF0;
  121. y_plane[y_ptr++] = y_pred = (cur_byte & 0x0F) << 4;
  122. cur_byte = buf[stream_ptr++];
  123. v_plane[v_ptr++] = v_pred = cur_byte & 0xF0;
  124. y_pred += y_table[cur_byte & 0x0F];
  125. y_plane[y_ptr++] = y_pred;
  126. cur_byte = buf[stream_ptr++];
  127. y_pred += y_table[cur_byte & 0x0F];
  128. y_plane[y_ptr++] = y_pred;
  129. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  130. y_plane[y_ptr++] = y_pred;
  131. /* iterate through the remaining pixel groups (4 pixels/group) */
  132. pixel_groups = s->width / 4 - 1;
  133. while (pixel_groups--) {
  134. cur_byte = buf[stream_ptr++];
  135. u_pred += u_table[(cur_byte & 0xF0) >> 4];
  136. u_plane[u_ptr++] = u_pred;
  137. y_pred += y_table[cur_byte & 0x0F];
  138. y_plane[y_ptr++] = y_pred;
  139. cur_byte = buf[stream_ptr++];
  140. v_pred += v_table[(cur_byte & 0xF0) >> 4];
  141. v_plane[v_ptr++] = v_pred;
  142. y_pred += y_table[cur_byte & 0x0F];
  143. y_plane[y_ptr++] = y_pred;
  144. cur_byte = buf[stream_ptr++];
  145. y_pred += y_table[cur_byte & 0x0F];
  146. y_plane[y_ptr++] = y_pred;
  147. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  148. y_plane[y_ptr++] = y_pred;
  149. }
  150. }
  151. }
  152. *data_size=sizeof(AVFrame);
  153. *(AVFrame*)data= s->frame;
  154. return buf_size;
  155. }
  156. static av_cold int cyuv_decode_end(AVCodecContext *avctx)
  157. {
  158. CyuvDecodeContext *s = avctx->priv_data;
  159. if (s->frame.data[0])
  160. avctx->release_buffer(avctx, &s->frame);
  161. return 0;
  162. }
  163. #if CONFIG_AURA_DECODER
  164. AVCodec ff_aura_decoder = {
  165. .name = "aura",
  166. .type = AVMEDIA_TYPE_VIDEO,
  167. .id = AV_CODEC_ID_AURA,
  168. .priv_data_size = sizeof(CyuvDecodeContext),
  169. .init = cyuv_decode_init,
  170. .close = cyuv_decode_end,
  171. .decode = cyuv_decode_frame,
  172. .capabilities = CODEC_CAP_DR1,
  173. .long_name = NULL_IF_CONFIG_SMALL("Auravision AURA"),
  174. };
  175. #endif
  176. #if CONFIG_CYUV_DECODER
  177. AVCodec ff_cyuv_decoder = {
  178. .name = "cyuv",
  179. .type = AVMEDIA_TYPE_VIDEO,
  180. .id = AV_CODEC_ID_CYUV,
  181. .priv_data_size = sizeof(CyuvDecodeContext),
  182. .init = cyuv_decode_init,
  183. .close = cyuv_decode_end,
  184. .decode = cyuv_decode_frame,
  185. .capabilities = CODEC_CAP_DR1,
  186. .long_name = NULL_IF_CONFIG_SMALL("Creative YUV (CYUV)"),
  187. };
  188. #endif