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.

181 lines
5.4KB

  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 libavcodec/cyuv.c
  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. typedef struct CyuvDecodeContext {
  35. AVCodecContext *avctx;
  36. int width, height;
  37. AVFrame frame;
  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 -1;
  47. s->height = avctx->height;
  48. avctx->pix_fmt = PIX_FMT_YUV411P;
  49. return 0;
  50. }
  51. static int cyuv_decode_frame(AVCodecContext *avctx,
  52. void *data, int *data_size,
  53. AVPacket *avpkt)
  54. {
  55. const uint8_t *buf = avpkt->data;
  56. int buf_size = avpkt->size;
  57. CyuvDecodeContext *s=avctx->priv_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. /* sanity check the buffer size: A buffer has 3x16-bytes tables
  73. * followed by (height) lines each with 3 bytes to represent groups
  74. * of 4 pixels. Thus, the total size of the buffer ought to be:
  75. * (3 * 16) + height * (width * 3 / 4) */
  76. if (buf_size != 48 + s->height * (s->width * 3 / 4)) {
  77. av_log(avctx, AV_LOG_ERROR, "ffmpeg: cyuv: got a buffer with %d bytes when %d were expected\n",
  78. buf_size,
  79. 48 + s->height * (s->width * 3 / 4));
  80. return -1;
  81. }
  82. /* pixel data starts 48 bytes in, after 3x16-byte tables */
  83. stream_ptr = 48;
  84. if(s->frame.data[0])
  85. avctx->release_buffer(avctx, &s->frame);
  86. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  87. s->frame.reference = 0;
  88. if(avctx->get_buffer(avctx, &s->frame) < 0) {
  89. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  90. return -1;
  91. }
  92. y_plane = s->frame.data[0];
  93. u_plane = s->frame.data[1];
  94. v_plane = s->frame.data[2];
  95. /* iterate through each line in the height */
  96. for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
  97. y_ptr < (s->height * s->frame.linesize[0]);
  98. y_ptr += s->frame.linesize[0] - s->width,
  99. u_ptr += s->frame.linesize[1] - s->width / 4,
  100. v_ptr += s->frame.linesize[2] - s->width / 4) {
  101. /* reset predictors */
  102. cur_byte = buf[stream_ptr++];
  103. u_plane[u_ptr++] = u_pred = cur_byte & 0xF0;
  104. y_plane[y_ptr++] = y_pred = (cur_byte & 0x0F) << 4;
  105. cur_byte = buf[stream_ptr++];
  106. v_plane[v_ptr++] = v_pred = cur_byte & 0xF0;
  107. y_pred += y_table[cur_byte & 0x0F];
  108. y_plane[y_ptr++] = y_pred;
  109. cur_byte = buf[stream_ptr++];
  110. y_pred += y_table[cur_byte & 0x0F];
  111. y_plane[y_ptr++] = y_pred;
  112. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  113. y_plane[y_ptr++] = y_pred;
  114. /* iterate through the remaining pixel groups (4 pixels/group) */
  115. pixel_groups = s->width / 4 - 1;
  116. while (pixel_groups--) {
  117. cur_byte = buf[stream_ptr++];
  118. u_pred += u_table[(cur_byte & 0xF0) >> 4];
  119. u_plane[u_ptr++] = u_pred;
  120. y_pred += y_table[cur_byte & 0x0F];
  121. y_plane[y_ptr++] = y_pred;
  122. cur_byte = buf[stream_ptr++];
  123. v_pred += v_table[(cur_byte & 0xF0) >> 4];
  124. v_plane[v_ptr++] = v_pred;
  125. y_pred += y_table[cur_byte & 0x0F];
  126. y_plane[y_ptr++] = y_pred;
  127. cur_byte = buf[stream_ptr++];
  128. y_pred += y_table[cur_byte & 0x0F];
  129. y_plane[y_ptr++] = y_pred;
  130. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  131. y_plane[y_ptr++] = y_pred;
  132. }
  133. }
  134. *data_size=sizeof(AVFrame);
  135. *(AVFrame*)data= s->frame;
  136. return buf_size;
  137. }
  138. AVCodec cyuv_decoder = {
  139. "cyuv",
  140. CODEC_TYPE_VIDEO,
  141. CODEC_ID_CYUV,
  142. sizeof(CyuvDecodeContext),
  143. cyuv_decode_init,
  144. NULL,
  145. NULL,
  146. cyuv_decode_frame,
  147. CODEC_CAP_DR1,
  148. NULL,
  149. .long_name = NULL_IF_CONFIG_SMALL("Creative YUV (CYUV)"),
  150. };