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.

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