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.

182 lines
5.1KB

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