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.

221 lines
7.4KB

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