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.

180 lines
5.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 libavcodec/cyuv.c
  27. * Creative YUV (CYUV) Video Decoder.
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include "avcodec.h"
  34. #include "dsputil.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. avctx->pix_fmt = PIX_FMT_YUV411P;
  50. return 0;
  51. }
  52. static int cyuv_decode_frame(AVCodecContext *avctx,
  53. void *data, int *data_size,
  54. const uint8_t *buf, int buf_size)
  55. {
  56. CyuvDecodeContext *s=avctx->priv_data;
  57. unsigned char *y_plane;
  58. unsigned char *u_plane;
  59. unsigned char *v_plane;
  60. int y_ptr;
  61. int u_ptr;
  62. int v_ptr;
  63. /* prediction error tables (make it clear that they are signed values) */
  64. const signed char *y_table = (const signed char*)buf + 0;
  65. const signed char *u_table = (const signed char*)buf + 16;
  66. const signed char *v_table = (const signed char*)buf + 32;
  67. unsigned char y_pred, u_pred, v_pred;
  68. int stream_ptr;
  69. unsigned char cur_byte;
  70. int pixel_groups;
  71. /* sanity check the buffer size: A buffer has 3x16-bytes tables
  72. * followed by (height) lines each with 3 bytes to represent groups
  73. * of 4 pixels. Thus, the total size of the buffer ought to be:
  74. * (3 * 16) + height * (width * 3 / 4) */
  75. if (buf_size != 48 + s->height * (s->width * 3 / 4)) {
  76. av_log(avctx, AV_LOG_ERROR, "ffmpeg: cyuv: got a buffer with %d bytes when %d were expected\n",
  77. buf_size,
  78. 48 + s->height * (s->width * 3 / 4));
  79. return -1;
  80. }
  81. /* pixel data starts 48 bytes in, after 3x16-byte tables */
  82. stream_ptr = 48;
  83. if(s->frame.data[0])
  84. avctx->release_buffer(avctx, &s->frame);
  85. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  86. s->frame.reference = 0;
  87. if(avctx->get_buffer(avctx, &s->frame) < 0) {
  88. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  89. return -1;
  90. }
  91. y_plane = s->frame.data[0];
  92. u_plane = s->frame.data[1];
  93. v_plane = s->frame.data[2];
  94. /* iterate through each line in the height */
  95. for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
  96. y_ptr < (s->height * s->frame.linesize[0]);
  97. y_ptr += s->frame.linesize[0] - s->width,
  98. u_ptr += s->frame.linesize[1] - s->width / 4,
  99. v_ptr += s->frame.linesize[2] - s->width / 4) {
  100. /* reset predictors */
  101. cur_byte = buf[stream_ptr++];
  102. u_plane[u_ptr++] = u_pred = cur_byte & 0xF0;
  103. y_plane[y_ptr++] = y_pred = (cur_byte & 0x0F) << 4;
  104. cur_byte = buf[stream_ptr++];
  105. v_plane[v_ptr++] = v_pred = cur_byte & 0xF0;
  106. y_pred += y_table[cur_byte & 0x0F];
  107. y_plane[y_ptr++] = y_pred;
  108. cur_byte = buf[stream_ptr++];
  109. y_pred += y_table[cur_byte & 0x0F];
  110. y_plane[y_ptr++] = y_pred;
  111. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  112. y_plane[y_ptr++] = y_pred;
  113. /* iterate through the remaining pixel groups (4 pixels/group) */
  114. pixel_groups = s->width / 4 - 1;
  115. while (pixel_groups--) {
  116. cur_byte = buf[stream_ptr++];
  117. u_pred += u_table[(cur_byte & 0xF0) >> 4];
  118. u_plane[u_ptr++] = u_pred;
  119. y_pred += y_table[cur_byte & 0x0F];
  120. y_plane[y_ptr++] = y_pred;
  121. cur_byte = buf[stream_ptr++];
  122. v_pred += v_table[(cur_byte & 0xF0) >> 4];
  123. v_plane[v_ptr++] = v_pred;
  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. }
  132. }
  133. *data_size=sizeof(AVFrame);
  134. *(AVFrame*)data= s->frame;
  135. return buf_size;
  136. }
  137. AVCodec cyuv_decoder = {
  138. "cyuv",
  139. CODEC_TYPE_VIDEO,
  140. CODEC_ID_CYUV,
  141. sizeof(CyuvDecodeContext),
  142. cyuv_decode_init,
  143. NULL,
  144. NULL,
  145. cyuv_decode_frame,
  146. CODEC_CAP_DR1,
  147. NULL,
  148. .long_name = NULL_IF_CONFIG_SMALL("Creative YUV (CYUV)"),
  149. };