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.6KB

  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 "avcodec.h"
  30. #include "bytestream.h"
  31. #include "roqvideo.h"
  32. static void roqvideo_decode_frame(RoqContext *ri)
  33. {
  34. unsigned int chunk_id = 0, chunk_arg = 0;
  35. unsigned long chunk_size = 0;
  36. int i, j, k, nv1, nv2, vqflg = 0, vqflg_pos = -1;
  37. int vqid, bpos, xpos, ypos, xp, yp, x, y, mx, my;
  38. int frame_stats[2][4] = {{0},{0}};
  39. roq_qcell *qcell;
  40. const unsigned char *buf = ri->buf;
  41. const unsigned char *buf_end = ri->buf + ri->size;
  42. while (buf < buf_end) {
  43. chunk_id = bytestream_get_le16(&buf);
  44. chunk_size = bytestream_get_le32(&buf);
  45. chunk_arg = bytestream_get_le16(&buf);
  46. if(chunk_id == RoQ_QUAD_VQ)
  47. break;
  48. if(chunk_id == RoQ_QUAD_CODEBOOK) {
  49. if((nv1 = chunk_arg >> 8) == 0)
  50. nv1 = 256;
  51. if((nv2 = chunk_arg & 0xff) == 0 && nv1 * 6 < chunk_size)
  52. nv2 = 256;
  53. for(i = 0; i < nv1; i++) {
  54. ri->cb2x2[i].y[0] = *buf++;
  55. ri->cb2x2[i].y[1] = *buf++;
  56. ri->cb2x2[i].y[2] = *buf++;
  57. ri->cb2x2[i].y[3] = *buf++;
  58. ri->cb2x2[i].u = *buf++;
  59. ri->cb2x2[i].v = *buf++;
  60. }
  61. for(i = 0; i < nv2; i++)
  62. for(j = 0; j < 4; j++)
  63. ri->cb4x4[i].idx[j] = *buf++;
  64. }
  65. }
  66. bpos = xpos = ypos = 0;
  67. while(bpos < chunk_size) {
  68. for (yp = ypos; yp < ypos + 16; yp += 8)
  69. for (xp = xpos; xp < xpos + 16; xp += 8) {
  70. if (vqflg_pos < 0) {
  71. vqflg = buf[bpos++]; vqflg |= (buf[bpos++] << 8);
  72. vqflg_pos = 7;
  73. }
  74. vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
  75. frame_stats[0][vqid]++;
  76. vqflg_pos--;
  77. switch(vqid) {
  78. case RoQ_ID_MOT:
  79. break;
  80. case RoQ_ID_FCC:
  81. mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
  82. my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
  83. ff_apply_motion_8x8(ri, xp, yp, mx, my);
  84. break;
  85. case RoQ_ID_SLD:
  86. qcell = ri->cb4x4 + buf[bpos++];
  87. ff_apply_vector_4x4(ri, xp, yp, ri->cb2x2 + qcell->idx[0]);
  88. ff_apply_vector_4x4(ri, xp+4, yp, ri->cb2x2 + qcell->idx[1]);
  89. ff_apply_vector_4x4(ri, xp, yp+4, ri->cb2x2 + qcell->idx[2]);
  90. ff_apply_vector_4x4(ri, xp+4, yp+4, ri->cb2x2 + qcell->idx[3]);
  91. break;
  92. case RoQ_ID_CCC:
  93. for (k = 0; k < 4; k++) {
  94. x = xp; y = yp;
  95. if(k & 0x01) x += 4;
  96. if(k & 0x02) y += 4;
  97. if (vqflg_pos < 0) {
  98. vqflg = buf[bpos++];
  99. vqflg |= (buf[bpos++] << 8);
  100. vqflg_pos = 7;
  101. }
  102. vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
  103. frame_stats[1][vqid]++;
  104. vqflg_pos--;
  105. switch(vqid) {
  106. case RoQ_ID_MOT:
  107. break;
  108. case RoQ_ID_FCC:
  109. mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
  110. my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
  111. ff_apply_motion_4x4(ri, x, y, mx, my);
  112. break;
  113. case RoQ_ID_SLD:
  114. qcell = ri->cb4x4 + buf[bpos++];
  115. ff_apply_vector_2x2(ri, x, y, ri->cb2x2 + qcell->idx[0]);
  116. ff_apply_vector_2x2(ri, x+2, y, ri->cb2x2 + qcell->idx[1]);
  117. ff_apply_vector_2x2(ri, x, y+2, ri->cb2x2 + qcell->idx[2]);
  118. ff_apply_vector_2x2(ri, x+2, y+2, ri->cb2x2 + qcell->idx[3]);
  119. break;
  120. case RoQ_ID_CCC:
  121. ff_apply_vector_2x2(ri, x, y, ri->cb2x2 + buf[bpos]);
  122. ff_apply_vector_2x2(ri, x+2, y, ri->cb2x2 + buf[bpos+1]);
  123. ff_apply_vector_2x2(ri, x, y+2, ri->cb2x2 + buf[bpos+2]);
  124. ff_apply_vector_2x2(ri, x+2, y+2, ri->cb2x2 + buf[bpos+3]);
  125. bpos += 4;
  126. break;
  127. }
  128. }
  129. break;
  130. default:
  131. av_log(ri->avctx, AV_LOG_ERROR, "Unknown vq code: %d\n", vqid);
  132. }
  133. }
  134. xpos += 16;
  135. if (xpos >= ri->width) {
  136. xpos -= ri->width;
  137. ypos += 16;
  138. }
  139. if(ypos >= ri->height)
  140. break;
  141. }
  142. }
  143. static av_cold int roq_decode_init(AVCodecContext *avctx)
  144. {
  145. RoqContext *s = avctx->priv_data;
  146. s->avctx = avctx;
  147. s->width = avctx->width;
  148. s->height = avctx->height;
  149. s->last_frame = &s->frames[0];
  150. s->current_frame = &s->frames[1];
  151. avctx->pix_fmt = PIX_FMT_YUV444P;
  152. return 0;
  153. }
  154. static int roq_decode_frame(AVCodecContext *avctx,
  155. void *data, int *data_size,
  156. AVPacket *avpkt)
  157. {
  158. const uint8_t *buf = avpkt->data;
  159. int buf_size = avpkt->size;
  160. RoqContext *s = avctx->priv_data;
  161. int copy= !s->current_frame->data[0];
  162. if (avctx->reget_buffer(avctx, s->current_frame)) {
  163. av_log(avctx, AV_LOG_ERROR, " RoQ: get_buffer() failed\n");
  164. return -1;
  165. }
  166. if(copy)
  167. av_picture_copy((AVPicture*)s->current_frame, (AVPicture*)s->last_frame,
  168. avctx->pix_fmt, avctx->width, avctx->height);
  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 av_cold 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. .long_name = NULL_IF_CONFIG_SMALL("id RoQ video"),
  199. };