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.

189 lines
5.4KB

  1. /*
  2. *
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * Creative YUV (CYUV) Video Decoder
  22. * by Mike Melanson (melanson@pcisys.net)
  23. * based on "Creative YUV (CYUV) stream format for AVI":
  24. * http://www.csse.monash.edu.au/~timf/videocodec/cyuv.txt
  25. *
  26. */
  27. /**
  28. * @file cyuv.c
  29. * Creative YUV (CYUV) Video Decoder.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include "avcodec.h"
  36. #include "dsputil.h"
  37. #include "mpegvideo.h"
  38. typedef struct CyuvDecodeContext {
  39. AVCodecContext *avctx;
  40. int width, height;
  41. AVFrame frame;
  42. } CyuvDecodeContext;
  43. static int cyuv_decode_init(AVCodecContext *avctx)
  44. {
  45. CyuvDecodeContext *s = avctx->priv_data;
  46. s->avctx = avctx;
  47. s->width = avctx->width;
  48. /* width needs to be divisible by 4 for this codec to work */
  49. if (s->width & 0x3)
  50. return -1;
  51. s->height = avctx->height;
  52. avctx->pix_fmt = PIX_FMT_YUV411P;
  53. return 0;
  54. }
  55. static int cyuv_decode_frame(AVCodecContext *avctx,
  56. void *data, int *data_size,
  57. uint8_t *buf, int buf_size)
  58. {
  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. signed char *y_table = (signed char*)buf + 0;
  68. signed char *u_table = (signed char*)buf + 16;
  69. signed char *v_table = (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. /* sanity check the buffer size: A buffer has 3x16-bytes tables
  75. * followed by (height) lines each with 3 bytes to represent groups
  76. * of 4 pixels. Thus, the total size of the buffer ought to be:
  77. * (3 * 16) + height * (width * 3 / 4) */
  78. if (buf_size != 48 + s->height * (s->width * 3 / 4)) {
  79. av_log(avctx, AV_LOG_ERROR, "ffmpeg: cyuv: got a buffer with %d bytes when %d were expected\n",
  80. buf_size,
  81. 48 + s->height * (s->width * 3 / 4));
  82. return -1;
  83. }
  84. /* pixel data starts 48 bytes in, after 3x16-byte tables */
  85. stream_ptr = 48;
  86. if(s->frame.data[0])
  87. avctx->release_buffer(avctx, &s->frame);
  88. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  89. s->frame.reference = 0;
  90. if(avctx->get_buffer(avctx, &s->frame) < 0) {
  91. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  92. return -1;
  93. }
  94. y_plane = s->frame.data[0];
  95. u_plane = s->frame.data[1];
  96. v_plane = s->frame.data[2];
  97. /* iterate through each line in the height */
  98. for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
  99. y_ptr < (s->height * s->frame.linesize[0]);
  100. y_ptr += s->frame.linesize[0] - s->width,
  101. u_ptr += s->frame.linesize[1] - s->width / 4,
  102. v_ptr += s->frame.linesize[2] - s->width / 4) {
  103. /* reset predictors */
  104. cur_byte = buf[stream_ptr++];
  105. u_plane[u_ptr++] = u_pred = cur_byte & 0xF0;
  106. y_plane[y_ptr++] = y_pred = (cur_byte & 0x0F) << 4;
  107. cur_byte = buf[stream_ptr++];
  108. v_plane[v_ptr++] = v_pred = cur_byte & 0xF0;
  109. y_pred += y_table[cur_byte & 0x0F];
  110. y_plane[y_ptr++] = y_pred;
  111. cur_byte = buf[stream_ptr++];
  112. y_pred += y_table[cur_byte & 0x0F];
  113. y_plane[y_ptr++] = y_pred;
  114. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  115. y_plane[y_ptr++] = y_pred;
  116. /* iterate through the remaining pixel groups (4 pixels/group) */
  117. pixel_groups = s->width / 4 - 1;
  118. while (pixel_groups--) {
  119. cur_byte = buf[stream_ptr++];
  120. u_pred += u_table[(cur_byte & 0xF0) >> 4];
  121. u_plane[u_ptr++] = u_pred;
  122. y_pred += y_table[cur_byte & 0x0F];
  123. y_plane[y_ptr++] = y_pred;
  124. cur_byte = buf[stream_ptr++];
  125. v_pred += v_table[(cur_byte & 0xF0) >> 4];
  126. v_plane[v_ptr++] = v_pred;
  127. y_pred += y_table[cur_byte & 0x0F];
  128. y_plane[y_ptr++] = y_pred;
  129. cur_byte = buf[stream_ptr++];
  130. y_pred += y_table[cur_byte & 0x0F];
  131. y_plane[y_ptr++] = y_pred;
  132. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  133. y_plane[y_ptr++] = y_pred;
  134. }
  135. }
  136. *data_size=sizeof(AVFrame);
  137. *(AVFrame*)data= s->frame;
  138. return buf_size;
  139. }
  140. static int cyuv_decode_end(AVCodecContext *avctx)
  141. {
  142. /* CyuvDecodeContext *s = avctx->priv_data;*/
  143. return 0;
  144. }
  145. AVCodec cyuv_decoder = {
  146. "cyuv",
  147. CODEC_TYPE_VIDEO,
  148. CODEC_ID_CYUV,
  149. sizeof(CyuvDecodeContext),
  150. cyuv_decode_init,
  151. NULL,
  152. cyuv_decode_end,
  153. cyuv_decode_frame,
  154. CODEC_CAP_DR1,
  155. NULL
  156. };