roqvideodec.c 7.8 KB

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