cyuv.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "common.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "mpegvideo.h"
  39. typedef struct CyuvDecodeContext {
  40. AVCodecContext *avctx;
  41. int width, height;
  42. AVFrame frame;
  43. } CyuvDecodeContext;
  44. static int cyuv_decode_init(AVCodecContext *avctx)
  45. {
  46. CyuvDecodeContext *s = avctx->priv_data;
  47. s->avctx = avctx;
  48. s->width = avctx->width;
  49. /* width needs to be divisible by 4 for this codec to work */
  50. if (s->width & 0x3)
  51. return -1;
  52. s->height = avctx->height;
  53. avctx->pix_fmt = PIX_FMT_YUV411P;
  54. avctx->has_b_frames = 0;
  55. return 0;
  56. }
  57. static int cyuv_decode_frame(AVCodecContext *avctx,
  58. void *data, int *data_size,
  59. uint8_t *buf, int buf_size)
  60. {
  61. CyuvDecodeContext *s=avctx->priv_data;
  62. unsigned char *y_plane;
  63. unsigned char *u_plane;
  64. unsigned char *v_plane;
  65. int y_ptr;
  66. int u_ptr;
  67. int v_ptr;
  68. /* prediction error tables (make it clear that they are signed values) */
  69. signed char *y_table = (signed char*)buf + 0;
  70. signed char *u_table = (signed char*)buf + 16;
  71. signed char *v_table = (signed char*)buf + 32;
  72. unsigned char y_pred, u_pred, v_pred;
  73. int stream_ptr;
  74. unsigned char cur_byte;
  75. int pixel_groups;
  76. /* sanity check the buffer size: A buffer has 3x16-bytes tables
  77. * followed by (height) lines each with 3 bytes to represent groups
  78. * of 4 pixels. Thus, the total size of the buffer ought to be:
  79. * (3 * 16) + height * (width * 3 / 4) */
  80. if (buf_size != 48 + s->height * (s->width * 3 / 4)) {
  81. av_log(avctx, AV_LOG_ERROR, "ffmpeg: cyuv: got a buffer with %d bytes when %d were expected\n",
  82. buf_size,
  83. 48 + s->height * (s->width * 3 / 4));
  84. return -1;
  85. }
  86. /* pixel data starts 48 bytes in, after 3x16-byte tables */
  87. stream_ptr = 48;
  88. if(s->frame.data[0])
  89. avctx->release_buffer(avctx, &s->frame);
  90. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  91. s->frame.reference = 0;
  92. if(avctx->get_buffer(avctx, &s->frame) < 0) {
  93. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  94. return -1;
  95. }
  96. y_plane = s->frame.data[0];
  97. u_plane = s->frame.data[1];
  98. v_plane = s->frame.data[2];
  99. /* iterate through each line in the height */
  100. for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
  101. y_ptr < (s->height * s->frame.linesize[0]);
  102. y_ptr += s->frame.linesize[0] - s->width,
  103. u_ptr += s->frame.linesize[1] - s->width / 4,
  104. v_ptr += s->frame.linesize[2] - s->width / 4) {
  105. /* reset predictors */
  106. cur_byte = buf[stream_ptr++];
  107. u_plane[u_ptr++] = u_pred = cur_byte & 0xF0;
  108. y_plane[y_ptr++] = y_pred = (cur_byte & 0x0F) << 4;
  109. cur_byte = buf[stream_ptr++];
  110. v_plane[v_ptr++] = v_pred = cur_byte & 0xF0;
  111. y_pred += y_table[cur_byte & 0x0F];
  112. y_plane[y_ptr++] = y_pred;
  113. cur_byte = buf[stream_ptr++];
  114. y_pred += y_table[cur_byte & 0x0F];
  115. y_plane[y_ptr++] = y_pred;
  116. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  117. y_plane[y_ptr++] = y_pred;
  118. /* iterate through the remaining pixel groups (4 pixels/group) */
  119. pixel_groups = s->width / 4 - 1;
  120. while (pixel_groups--) {
  121. cur_byte = buf[stream_ptr++];
  122. u_pred += u_table[(cur_byte & 0xF0) >> 4];
  123. u_plane[u_ptr++] = u_pred;
  124. y_pred += y_table[cur_byte & 0x0F];
  125. y_plane[y_ptr++] = y_pred;
  126. cur_byte = buf[stream_ptr++];
  127. v_pred += v_table[(cur_byte & 0xF0) >> 4];
  128. v_plane[v_ptr++] = v_pred;
  129. y_pred += y_table[cur_byte & 0x0F];
  130. y_plane[y_ptr++] = y_pred;
  131. cur_byte = buf[stream_ptr++];
  132. y_pred += y_table[cur_byte & 0x0F];
  133. y_plane[y_ptr++] = y_pred;
  134. y_pred += y_table[(cur_byte & 0xF0) >> 4];
  135. y_plane[y_ptr++] = y_pred;
  136. }
  137. }
  138. *data_size=sizeof(AVFrame);
  139. *(AVFrame*)data= s->frame;
  140. return buf_size;
  141. }
  142. static int cyuv_decode_end(AVCodecContext *avctx)
  143. {
  144. /* CyuvDecodeContext *s = avctx->priv_data;*/
  145. return 0;
  146. }
  147. AVCodec cyuv_decoder = {
  148. "cyuv",
  149. CODEC_TYPE_VIDEO,
  150. CODEC_ID_CYUV,
  151. sizeof(CyuvDecodeContext),
  152. cyuv_decode_init,
  153. NULL,
  154. cyuv_decode_end,
  155. cyuv_decode_frame,
  156. CODEC_CAP_DR1,
  157. NULL
  158. };