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.

203 lines
6.3KB

  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 "internal.h"
  34. #include "libavutil/internal.h"
  35. typedef struct CyuvDecodeContext {
  36. AVCodecContext *avctx;
  37. int width, height;
  38. } CyuvDecodeContext;
  39. static av_cold int cyuv_decode_init(AVCodecContext *avctx)
  40. {
  41. CyuvDecodeContext *s = avctx->priv_data;
  42. s->avctx = avctx;
  43. s->width = avctx->width;
  44. /* width needs to be divisible by 4 for this codec to work */
  45. if (s->width & 0x3)
  46. return AVERROR_INVALIDDATA;
  47. s->height = avctx->height;
  48. return 0;
  49. }
  50. static int cyuv_decode_frame(AVCodecContext *avctx,
  51. void *data, int *got_frame,
  52. AVPacket *avpkt)
  53. {
  54. const uint8_t *buf = avpkt->data;
  55. int buf_size = avpkt->size;
  56. CyuvDecodeContext *s=avctx->priv_data;
  57. AVFrame *frame = data;
  58. unsigned char *y_plane;
  59. unsigned char *u_plane;
  60. unsigned char *v_plane;
  61. int y_ptr;
  62. int u_ptr;
  63. int v_ptr;
  64. /* prediction error tables (make it clear that they are signed values) */
  65. const signed char *y_table = (const signed char*)buf + 0;
  66. const signed char *u_table = (const signed char*)buf + 16;
  67. const signed char *v_table = (const signed char*)buf + 32;
  68. unsigned char y_pred, u_pred, v_pred;
  69. int stream_ptr;
  70. unsigned char cur_byte;
  71. int pixel_groups;
  72. int rawsize = s->height * FFALIGN(s->width,2) * 2;
  73. int ret;
  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 AVERROR_INVALIDDATA;
  90. }
  91. /* pixel data starts 48 bytes in, after 3x16-byte tables */
  92. stream_ptr = 48;
  93. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  94. return ret;
  95. y_plane = frame->data[0];
  96. u_plane = frame->data[1];
  97. v_plane = frame->data[2];
  98. if (buf_size == rawsize) {
  99. int linesize = FFALIGN(s->width,2) * 2;
  100. y_plane += frame->linesize[0] * s->height;
  101. for (stream_ptr = 0; stream_ptr < rawsize; stream_ptr += linesize) {
  102. y_plane -= frame->linesize[0];
  103. memcpy(y_plane, buf+stream_ptr, linesize);
  104. }
  105. } else {
  106. /* iterate through each line in the height */
  107. for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
  108. y_ptr < (s->height * frame->linesize[0]);
  109. y_ptr += frame->linesize[0] - s->width,
  110. u_ptr += frame->linesize[1] - s->width / 4,
  111. v_ptr += frame->linesize[2] - s->width / 4) {
  112. /* reset predictors */
  113. cur_byte = buf[stream_ptr++];
  114. u_plane[u_ptr++] = u_pred = cur_byte & 0xF0;
  115. y_plane[y_ptr++] = y_pred = (cur_byte & 0x0F) << 4;
  116. cur_byte = buf[stream_ptr++];
  117. v_plane[v_ptr++] = v_pred = cur_byte & 0xF0;
  118. y_pred += y_table[cur_byte & 0x0F];
  119. y_plane[y_ptr++] = y_pred;
  120. cur_byte = buf[stream_ptr++];
  121. y_pred += y_table[cur_byte & 0x0F];
  122. y_plane[y_ptr++] = y_pred;
  123. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  124. y_plane[y_ptr++] = y_pred;
  125. /* iterate through the remaining pixel groups (4 pixels/group) */
  126. pixel_groups = s->width / 4 - 1;
  127. while (pixel_groups--) {
  128. cur_byte = buf[stream_ptr++];
  129. u_pred += u_table[(cur_byte & 0xF0) >> 4];
  130. u_plane[u_ptr++] = u_pred;
  131. y_pred += y_table[cur_byte & 0x0F];
  132. y_plane[y_ptr++] = y_pred;
  133. cur_byte = buf[stream_ptr++];
  134. v_pred += v_table[(cur_byte & 0xF0) >> 4];
  135. v_plane[v_ptr++] = v_pred;
  136. y_pred += y_table[cur_byte & 0x0F];
  137. y_plane[y_ptr++] = y_pred;
  138. cur_byte = buf[stream_ptr++];
  139. y_pred += y_table[cur_byte & 0x0F];
  140. y_plane[y_ptr++] = y_pred;
  141. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  142. y_plane[y_ptr++] = y_pred;
  143. }
  144. }
  145. }
  146. *got_frame = 1;
  147. return buf_size;
  148. }
  149. #if CONFIG_AURA_DECODER
  150. AVCodec ff_aura_decoder = {
  151. .name = "aura",
  152. .long_name = NULL_IF_CONFIG_SMALL("Auravision AURA"),
  153. .type = AVMEDIA_TYPE_VIDEO,
  154. .id = AV_CODEC_ID_AURA,
  155. .priv_data_size = sizeof(CyuvDecodeContext),
  156. .init = cyuv_decode_init,
  157. .decode = cyuv_decode_frame,
  158. .capabilities = AV_CODEC_CAP_DR1,
  159. };
  160. #endif
  161. #if CONFIG_CYUV_DECODER
  162. AVCodec ff_cyuv_decoder = {
  163. .name = "cyuv",
  164. .long_name = NULL_IF_CONFIG_SMALL("Creative YUV (CYUV)"),
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. .id = AV_CODEC_ID_CYUV,
  167. .priv_data_size = sizeof(CyuvDecodeContext),
  168. .init = cyuv_decode_init,
  169. .decode = cyuv_decode_frame,
  170. .capabilities = AV_CODEC_CAP_DR1,
  171. };
  172. #endif