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.

224 lines
7.5KB

  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. static void roqvideo_decode_frame(RoqContext *ri)
  38. {
  39. unsigned int chunk_id = 0, chunk_arg = 0;
  40. unsigned long chunk_size = 0;
  41. int i, j, k, nv1, nv2, vqflg = 0, vqflg_pos = -1;
  42. int vqid, bpos, xpos, ypos, xp, yp, x, y, mx, my;
  43. int frame_stats[2][4] = {{0},{0}};
  44. roq_qcell *qcell;
  45. unsigned char *buf = ri->buf;
  46. unsigned char *buf_end = ri->buf + ri->size;
  47. while (buf < buf_end) {
  48. chunk_id = bytestream_get_le16(&buf);
  49. chunk_size = bytestream_get_le32(&buf);
  50. chunk_arg = bytestream_get_le16(&buf);
  51. if(chunk_id == RoQ_QUAD_VQ)
  52. break;
  53. if(chunk_id == RoQ_QUAD_CODEBOOK) {
  54. if((nv1 = chunk_arg >> 8) == 0)
  55. nv1 = 256;
  56. if((nv2 = chunk_arg & 0xff) == 0 && nv1 * 6 < chunk_size)
  57. nv2 = 256;
  58. for(i = 0; i < nv1; i++) {
  59. ri->cells[i].y[0] = *buf++;
  60. ri->cells[i].y[1] = *buf++;
  61. ri->cells[i].y[2] = *buf++;
  62. ri->cells[i].y[3] = *buf++;
  63. ri->cells[i].u = *buf++;
  64. ri->cells[i].v = *buf++;
  65. }
  66. for(i = 0; i < nv2; i++)
  67. for(j = 0; j < 4; j++)
  68. ri->qcells[i].idx[j] = *buf++;
  69. }
  70. }
  71. bpos = xpos = ypos = 0;
  72. while(bpos < chunk_size) {
  73. for (yp = ypos; yp < ypos + 16; yp += 8)
  74. for (xp = xpos; xp < xpos + 16; xp += 8) {
  75. if (vqflg_pos < 0) {
  76. vqflg = buf[bpos++]; vqflg |= (buf[bpos++] << 8);
  77. vqflg_pos = 7;
  78. }
  79. vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
  80. frame_stats[0][vqid]++;
  81. vqflg_pos--;
  82. switch(vqid) {
  83. case RoQ_ID_MOT:
  84. break;
  85. case RoQ_ID_FCC:
  86. mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
  87. my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
  88. ff_apply_motion_8x8(ri, xp, yp, mx, my);
  89. break;
  90. case RoQ_ID_SLD:
  91. qcell = ri->qcells + buf[bpos++];
  92. ff_apply_vector_4x4(ri, xp, yp, ri->cells + qcell->idx[0]);
  93. ff_apply_vector_4x4(ri, xp+4, yp, ri->cells + qcell->idx[1]);
  94. ff_apply_vector_4x4(ri, xp, yp+4, ri->cells + qcell->idx[2]);
  95. ff_apply_vector_4x4(ri, xp+4, yp+4, ri->cells + qcell->idx[3]);
  96. break;
  97. case RoQ_ID_CCC:
  98. for (k = 0; k < 4; k++) {
  99. x = xp; y = yp;
  100. if(k & 0x01) x += 4;
  101. if(k & 0x02) y += 4;
  102. if (vqflg_pos < 0) {
  103. vqflg = buf[bpos++];
  104. vqflg |= (buf[bpos++] << 8);
  105. vqflg_pos = 7;
  106. }
  107. vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
  108. frame_stats[1][vqid]++;
  109. vqflg_pos--;
  110. switch(vqid) {
  111. case RoQ_ID_MOT:
  112. break;
  113. case RoQ_ID_FCC:
  114. mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
  115. my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
  116. ff_apply_motion_4x4(ri, x, y, mx, my);
  117. break;
  118. case RoQ_ID_SLD:
  119. qcell = ri->qcells + buf[bpos++];
  120. ff_apply_vector_2x2(ri, x, y, ri->cells + qcell->idx[0]);
  121. ff_apply_vector_2x2(ri, x+2, y, ri->cells + qcell->idx[1]);
  122. ff_apply_vector_2x2(ri, x, y+2, ri->cells + qcell->idx[2]);
  123. ff_apply_vector_2x2(ri, x+2, y+2, ri->cells + qcell->idx[3]);
  124. break;
  125. case RoQ_ID_CCC:
  126. ff_apply_vector_2x2(ri, x, y, ri->cells + buf[bpos]);
  127. ff_apply_vector_2x2(ri, x+2, y, ri->cells + buf[bpos+1]);
  128. ff_apply_vector_2x2(ri, x, y+2, ri->cells + buf[bpos+2]);
  129. ff_apply_vector_2x2(ri, x+2, y+2, ri->cells + buf[bpos+3]);
  130. bpos += 4;
  131. break;
  132. }
  133. }
  134. break;
  135. default:
  136. av_log(ri->avctx, AV_LOG_ERROR, "Unknown vq code: %d\n", vqid);
  137. }
  138. }
  139. xpos += 16;
  140. if (xpos >= ri->avctx->width) {
  141. xpos -= ri->avctx->width;
  142. ypos += 16;
  143. }
  144. if(ypos >= ri->avctx->height)
  145. break;
  146. }
  147. }
  148. static int roq_decode_init(AVCodecContext *avctx)
  149. {
  150. RoqContext *s = avctx->priv_data;
  151. s->avctx = avctx;
  152. s->last_frame = &s->frames[0];
  153. s->current_frame = &s->frames[1];
  154. avctx->pix_fmt = PIX_FMT_YUV420P;
  155. dsputil_init(&s->dsp, avctx);
  156. return 0;
  157. }
  158. static int roq_decode_frame(AVCodecContext *avctx,
  159. void *data, int *data_size,
  160. uint8_t *buf, int buf_size)
  161. {
  162. RoqContext *s = avctx->priv_data;
  163. if (avctx->reget_buffer(avctx, s->current_frame)) {
  164. av_log(avctx, AV_LOG_ERROR, " RoQ: get_buffer() failed\n");
  165. return -1;
  166. }
  167. s->y_stride = s->current_frame->linesize[0];
  168. s->c_stride = s->current_frame->linesize[1];
  169. s->buf = buf;
  170. s->size = buf_size;
  171. roqvideo_decode_frame(s);
  172. *data_size = sizeof(AVFrame);
  173. *(AVFrame*)data = *s->current_frame;
  174. /* shuffle frames */
  175. FFSWAP(AVFrame *, s->current_frame, s->last_frame);
  176. return buf_size;
  177. }
  178. static int roq_decode_end(AVCodecContext *avctx)
  179. {
  180. RoqContext *s = avctx->priv_data;
  181. /* release the last frame */
  182. if (s->last_frame->data[0])
  183. avctx->release_buffer(avctx, s->last_frame);
  184. if (s->current_frame->data[0])
  185. avctx->release_buffer(avctx, s->current_frame);
  186. return 0;
  187. }
  188. AVCodec roq_decoder = {
  189. "roqvideo",
  190. CODEC_TYPE_VIDEO,
  191. CODEC_ID_ROQ,
  192. sizeof(RoqContext),
  193. roq_decode_init,
  194. NULL,
  195. roq_decode_end,
  196. roq_decode_frame,
  197. CODEC_CAP_DR1,
  198. };