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.

233 lines
7.8KB

  1. /*
  2. * Copyright (C) 2003 the ffmpeg project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. /**
  22. * @file roqvideodec.c
  23. * Id RoQ Video Decoder by Dr. Tim Ferguson
  24. * For more information about the Id RoQ format, visit:
  25. * http://www.csse.monash.edu.au/~timf/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include "avcodec.h"
  32. #include "bytestream.h"
  33. #include "dsputil.h"
  34. #include "roqvideo.h"
  35. #define avg2(a,b) av_clip_uint8(((int)(a)+(int)(b)+1)>>1)
  36. #define avg4(a,b,c,d) av_clip_uint8(((int)(a)+(int)(b)+(int)(c)+(int)(d)+2)>>2)
  37. #define get_byte(in_buffer) *(in_buffer++)
  38. static void roqvideo_decode_frame(RoqContext *ri)
  39. {
  40. unsigned int chunk_id = 0, chunk_arg = 0;
  41. unsigned long chunk_size = 0;
  42. int i, j, k, nv1, nv2, vqflg = 0, vqflg_pos = -1;
  43. int vqid, bpos, xpos, ypos, xp, yp, x, y, mx, my;
  44. int frame_stats[2][4] = {{0},{0}};
  45. roq_qcell *qcell;
  46. unsigned char *buf = ri->buf;
  47. unsigned char *buf_end = ri->buf + ri->size;
  48. while (buf < buf_end) {
  49. chunk_id = bytestream_get_le16(&buf);
  50. chunk_size = bytestream_get_le32(&buf);
  51. chunk_arg = bytestream_get_le16(&buf);
  52. if(chunk_id == RoQ_QUAD_VQ)
  53. break;
  54. if(chunk_id == RoQ_QUAD_CODEBOOK) {
  55. if((nv1 = chunk_arg >> 8) == 0)
  56. nv1 = 256;
  57. if((nv2 = chunk_arg & 0xff) == 0 && nv1 * 6 < chunk_size)
  58. nv2 = 256;
  59. for(i = 0; i < nv1; i++) {
  60. ri->cells[i].y[0] = get_byte(buf);
  61. ri->cells[i].y[1] = get_byte(buf);
  62. ri->cells[i].y[2] = get_byte(buf);
  63. ri->cells[i].y[3] = get_byte(buf);
  64. ri->cells[i].u = get_byte(buf);
  65. ri->cells[i].v = get_byte(buf);
  66. }
  67. for(i = 0; i < nv2; i++)
  68. for(j = 0; j < 4; j++)
  69. ri->qcells[i].idx[j] = get_byte(buf);
  70. }
  71. }
  72. bpos = xpos = ypos = 0;
  73. while(bpos < chunk_size) {
  74. for (yp = ypos; yp < ypos + 16; yp += 8)
  75. for (xp = xpos; xp < xpos + 16; xp += 8) {
  76. if (vqflg_pos < 0) {
  77. vqflg = buf[bpos++]; vqflg |= (buf[bpos++] << 8);
  78. vqflg_pos = 7;
  79. }
  80. vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
  81. frame_stats[0][vqid]++;
  82. vqflg_pos--;
  83. switch(vqid) {
  84. case RoQ_ID_MOT:
  85. ff_apply_motion_8x8(ri, xp, yp, 0, 0);
  86. break;
  87. case RoQ_ID_FCC:
  88. mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
  89. my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
  90. ff_apply_motion_8x8(ri, xp, yp, mx, my);
  91. break;
  92. case RoQ_ID_SLD:
  93. qcell = ri->qcells + buf[bpos++];
  94. ff_apply_vector_4x4(ri, xp, yp, ri->cells + qcell->idx[0]);
  95. ff_apply_vector_4x4(ri, xp+4, yp, ri->cells + qcell->idx[1]);
  96. ff_apply_vector_4x4(ri, xp, yp+4, ri->cells + qcell->idx[2]);
  97. ff_apply_vector_4x4(ri, xp+4, yp+4, ri->cells + qcell->idx[3]);
  98. break;
  99. case RoQ_ID_CCC:
  100. for (k = 0; k < 4; k++) {
  101. x = xp; y = yp;
  102. if(k & 0x01) x += 4;
  103. if(k & 0x02) y += 4;
  104. if (vqflg_pos < 0) {
  105. vqflg = buf[bpos++];
  106. vqflg |= (buf[bpos++] << 8);
  107. vqflg_pos = 7;
  108. }
  109. vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
  110. frame_stats[1][vqid]++;
  111. vqflg_pos--;
  112. switch(vqid) {
  113. case RoQ_ID_MOT:
  114. ff_apply_motion_4x4(ri, x, y, 0, 0);
  115. break;
  116. case RoQ_ID_FCC:
  117. mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
  118. my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
  119. ff_apply_motion_4x4(ri, x, y, mx, my);
  120. break;
  121. case RoQ_ID_SLD:
  122. qcell = ri->qcells + buf[bpos++];
  123. ff_apply_vector_2x2(ri, x, y, ri->cells + qcell->idx[0]);
  124. ff_apply_vector_2x2(ri, x+2, y, ri->cells + qcell->idx[1]);
  125. ff_apply_vector_2x2(ri, x, y+2, ri->cells + qcell->idx[2]);
  126. ff_apply_vector_2x2(ri, x+2, y+2, ri->cells + qcell->idx[3]);
  127. break;
  128. case RoQ_ID_CCC:
  129. ff_apply_vector_2x2(ri, x, y, ri->cells + buf[bpos]);
  130. ff_apply_vector_2x2(ri, x+2, y, ri->cells + buf[bpos+1]);
  131. ff_apply_vector_2x2(ri, x, y+2, ri->cells + buf[bpos+2]);
  132. ff_apply_vector_2x2(ri, x+2, y+2, ri->cells + buf[bpos+3]);
  133. bpos += 4;
  134. break;
  135. }
  136. }
  137. break;
  138. default:
  139. av_log(ri->avctx, AV_LOG_ERROR, "Unknown vq code: %d\n", vqid);
  140. }
  141. }
  142. xpos += 16;
  143. if (xpos >= ri->avctx->width) {
  144. xpos -= ri->avctx->width;
  145. ypos += 16;
  146. }
  147. if(ypos >= ri->avctx->height)
  148. break;
  149. }
  150. }
  151. static int roq_decode_init(AVCodecContext *avctx)
  152. {
  153. RoqContext *s = avctx->priv_data;
  154. s->avctx = avctx;
  155. s->first_frame = 1;
  156. s->last_frame = &s->frames[0];
  157. s->current_frame = &s->frames[1];
  158. avctx->pix_fmt = PIX_FMT_YUV420P;
  159. dsputil_init(&s->dsp, avctx);
  160. return 0;
  161. }
  162. static int roq_decode_frame(AVCodecContext *avctx,
  163. void *data, int *data_size,
  164. uint8_t *buf, int buf_size)
  165. {
  166. RoqContext *s = avctx->priv_data;
  167. if (avctx->get_buffer(avctx, s->current_frame)) {
  168. av_log(avctx, AV_LOG_ERROR, " RoQ: get_buffer() failed\n");
  169. return -1;
  170. }
  171. s->y_stride = s->current_frame->linesize[0];
  172. s->c_stride = s->current_frame->linesize[1];
  173. s->buf = buf;
  174. s->size = buf_size;
  175. roqvideo_decode_frame(s);
  176. /* release the last frame if it is allocated */
  177. if (s->first_frame)
  178. s->first_frame = 0;
  179. else
  180. avctx->release_buffer(avctx, s->last_frame);
  181. *data_size = sizeof(AVFrame);
  182. *(AVFrame*)data = *s->current_frame;
  183. /* shuffle frames */
  184. FFSWAP(AVFrame *, s->current_frame, s->last_frame);
  185. return buf_size;
  186. }
  187. static int roq_decode_end(AVCodecContext *avctx)
  188. {
  189. RoqContext *s = avctx->priv_data;
  190. /* release the last frame */
  191. if (s->last_frame->data[0])
  192. avctx->release_buffer(avctx, s->last_frame);
  193. return 0;
  194. }
  195. AVCodec roq_decoder = {
  196. "roqvideo",
  197. CODEC_TYPE_VIDEO,
  198. CODEC_ID_ROQ,
  199. sizeof(RoqContext),
  200. roq_decode_init,
  201. NULL,
  202. roq_decode_end,
  203. roq_decode_frame,
  204. CODEC_CAP_DR1,
  205. };