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.

476 lines
15KB

  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 roqvideo.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 "dsputil.h"
  33. typedef struct {
  34. unsigned char y0, y1, y2, y3, u, v;
  35. } roq_cell;
  36. typedef struct {
  37. int idx[4];
  38. } roq_qcell;
  39. static int uiclip[1024], *uiclp; /* clipping table */
  40. #define avg2(a,b) uiclp[(((int)(a)+(int)(b)+1)>>1)]
  41. #define avg4(a,b,c,d) uiclp[(((int)(a)+(int)(b)+(int)(c)+(int)(d)+2)>>2)]
  42. typedef struct RoqContext {
  43. AVCodecContext *avctx;
  44. DSPContext dsp;
  45. AVFrame frames[2];
  46. AVFrame *last_frame;
  47. AVFrame *current_frame;
  48. int first_frame;
  49. int y_stride;
  50. int c_stride;
  51. roq_cell cells[256];
  52. roq_qcell qcells[256];
  53. unsigned char *buf;
  54. int size;
  55. } RoqContext;
  56. #define RoQ_INFO 0x1001
  57. #define RoQ_QUAD_CODEBOOK 0x1002
  58. #define RoQ_QUAD_VQ 0x1011
  59. #define RoQ_SOUND_MONO 0x1020
  60. #define RoQ_SOUND_STEREO 0x1021
  61. #define RoQ_ID_MOT 0x00
  62. #define RoQ_ID_FCC 0x01
  63. #define RoQ_ID_SLD 0x02
  64. #define RoQ_ID_CCC 0x03
  65. #define get_byte(in_buffer) *(in_buffer++)
  66. #define get_word(in_buffer) ((unsigned short)(in_buffer += 2, \
  67. (in_buffer[-1] << 8 | in_buffer[-2])))
  68. #define get_long(in_buffer) ((unsigned long)(in_buffer += 4, \
  69. (in_buffer[-1] << 24 | in_buffer[-2] << 16 | in_buffer[-3] << 8 | in_buffer[-4])))
  70. static void apply_vector_2x2(RoqContext *ri, int x, int y, roq_cell *cell)
  71. {
  72. unsigned char *yptr;
  73. yptr = ri->current_frame->data[0] + (y * ri->y_stride) + x;
  74. *yptr++ = cell->y0;
  75. *yptr++ = cell->y1;
  76. yptr += (ri->y_stride - 2);
  77. *yptr++ = cell->y2;
  78. *yptr++ = cell->y3;
  79. ri->current_frame->data[1][(y/2) * (ri->c_stride) + x/2] = cell->u;
  80. ri->current_frame->data[2][(y/2) * (ri->c_stride) + x/2] = cell->v;
  81. }
  82. static void apply_vector_4x4(RoqContext *ri, int x, int y, roq_cell *cell)
  83. {
  84. unsigned long row_inc, c_row_inc;
  85. register unsigned char y0, y1, u, v;
  86. unsigned char *yptr, *uptr, *vptr;
  87. yptr = ri->current_frame->data[0] + (y * ri->y_stride) + x;
  88. uptr = ri->current_frame->data[1] + (y/2) * (ri->c_stride) + x/2;
  89. vptr = ri->current_frame->data[2] + (y/2) * (ri->c_stride) + x/2;
  90. row_inc = ri->y_stride - 4;
  91. c_row_inc = (ri->c_stride) - 2;
  92. *yptr++ = y0 = cell->y0; *uptr++ = u = cell->u; *vptr++ = v = cell->v;
  93. *yptr++ = y0;
  94. *yptr++ = y1 = cell->y1; *uptr++ = u; *vptr++ = v;
  95. *yptr++ = y1;
  96. yptr += row_inc;
  97. *yptr++ = y0;
  98. *yptr++ = y0;
  99. *yptr++ = y1;
  100. *yptr++ = y1;
  101. yptr += row_inc; uptr += c_row_inc; vptr += c_row_inc;
  102. *yptr++ = y0 = cell->y2; *uptr++ = u; *vptr++ = v;
  103. *yptr++ = y0;
  104. *yptr++ = y1 = cell->y3; *uptr++ = u; *vptr++ = v;
  105. *yptr++ = y1;
  106. yptr += row_inc;
  107. *yptr++ = y0;
  108. *yptr++ = y0;
  109. *yptr++ = y1;
  110. *yptr++ = y1;
  111. }
  112. static void apply_motion_4x4(RoqContext *ri, int x, int y, unsigned char mv,
  113. signed char mean_x, signed char mean_y)
  114. {
  115. int i, hw, mx, my;
  116. unsigned char *pa, *pb;
  117. mx = x + 8 - (mv >> 4) - mean_x;
  118. my = y + 8 - (mv & 0xf) - mean_y;
  119. /* check MV against frame boundaries */
  120. if ((mx < 0) || (mx > ri->avctx->width - 4) ||
  121. (my < 0) || (my > ri->avctx->height - 4)) {
  122. av_log(ri->avctx, AV_LOG_ERROR, "motion vector out of bounds: MV = (%d, %d), boundaries = (0, 0, %d, %d)\n",
  123. mx, my, ri->avctx->width, ri->avctx->height);
  124. return;
  125. }
  126. pa = ri->current_frame->data[0] + (y * ri->y_stride) + x;
  127. pb = ri->last_frame->data[0] + (my * ri->y_stride) + mx;
  128. for(i = 0; i < 4; i++) {
  129. pa[0] = pb[0];
  130. pa[1] = pb[1];
  131. pa[2] = pb[2];
  132. pa[3] = pb[3];
  133. pa += ri->y_stride;
  134. pb += ri->y_stride;
  135. }
  136. hw = ri->y_stride/2;
  137. pa = ri->current_frame->data[1] + (y * ri->y_stride)/4 + x/2;
  138. pb = ri->last_frame->data[1] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
  139. for(i = 0; i < 2; i++) {
  140. switch(((my & 0x01) << 1) | (mx & 0x01)) {
  141. case 0:
  142. pa[0] = pb[0];
  143. pa[1] = pb[1];
  144. pa[hw] = pb[hw];
  145. pa[hw+1] = pb[hw+1];
  146. break;
  147. case 1:
  148. pa[0] = avg2(pb[0], pb[1]);
  149. pa[1] = avg2(pb[1], pb[2]);
  150. pa[hw] = avg2(pb[hw], pb[hw+1]);
  151. pa[hw+1] = avg2(pb[hw+1], pb[hw+2]);
  152. break;
  153. case 2:
  154. pa[0] = avg2(pb[0], pb[hw]);
  155. pa[1] = avg2(pb[1], pb[hw+1]);
  156. pa[hw] = avg2(pb[hw], pb[hw*2]);
  157. pa[hw+1] = avg2(pb[hw+1], pb[(hw*2)+1]);
  158. break;
  159. case 3:
  160. pa[0] = avg4(pb[0], pb[1], pb[hw], pb[hw+1]);
  161. pa[1] = avg4(pb[1], pb[2], pb[hw+1], pb[hw+2]);
  162. pa[hw] = avg4(pb[hw], pb[hw+1], pb[hw*2], pb[(hw*2)+1]);
  163. pa[hw+1] = avg4(pb[hw+1], pb[hw+2], pb[(hw*2)+1], pb[(hw*2)+1]);
  164. break;
  165. }
  166. pa = ri->current_frame->data[2] + (y * ri->y_stride)/4 + x/2;
  167. pb = ri->last_frame->data[2] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
  168. }
  169. }
  170. static void apply_motion_8x8(RoqContext *ri, int x, int y,
  171. unsigned char mv, signed char mean_x, signed char mean_y)
  172. {
  173. int mx, my, i, j, hw;
  174. unsigned char *pa, *pb;
  175. mx = x + 8 - (mv >> 4) - mean_x;
  176. my = y + 8 - (mv & 0xf) - mean_y;
  177. /* check MV against frame boundaries */
  178. if ((mx < 0) || (mx > ri->avctx->width - 8) ||
  179. (my < 0) || (my > ri->avctx->height - 8)) {
  180. av_log(ri->avctx, AV_LOG_ERROR, "motion vector out of bounds: MV = (%d, %d), boundaries = (0, 0, %d, %d)\n",
  181. mx, my, ri->avctx->width, ri->avctx->height);
  182. return;
  183. }
  184. pa = ri->current_frame->data[0] + (y * ri->y_stride) + x;
  185. pb = ri->last_frame->data[0] + (my * ri->y_stride) + mx;
  186. for(i = 0; i < 8; i++) {
  187. pa[0] = pb[0];
  188. pa[1] = pb[1];
  189. pa[2] = pb[2];
  190. pa[3] = pb[3];
  191. pa[4] = pb[4];
  192. pa[5] = pb[5];
  193. pa[6] = pb[6];
  194. pa[7] = pb[7];
  195. pa += ri->y_stride;
  196. pb += ri->y_stride;
  197. }
  198. hw = ri->c_stride;
  199. pa = ri->current_frame->data[1] + (y * ri->y_stride)/4 + x/2;
  200. pb = ri->last_frame->data[1] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
  201. for(j = 0; j < 2; j++) {
  202. for(i = 0; i < 4; i++) {
  203. switch(((my & 0x01) << 1) | (mx & 0x01)) {
  204. case 0:
  205. pa[0] = pb[0];
  206. pa[1] = pb[1];
  207. pa[2] = pb[2];
  208. pa[3] = pb[3];
  209. break;
  210. case 1:
  211. pa[0] = avg2(pb[0], pb[1]);
  212. pa[1] = avg2(pb[1], pb[2]);
  213. pa[2] = avg2(pb[2], pb[3]);
  214. pa[3] = avg2(pb[3], pb[4]);
  215. break;
  216. case 2:
  217. pa[0] = avg2(pb[0], pb[hw]);
  218. pa[1] = avg2(pb[1], pb[hw+1]);
  219. pa[2] = avg2(pb[2], pb[hw+2]);
  220. pa[3] = avg2(pb[3], pb[hw+3]);
  221. break;
  222. case 3:
  223. pa[0] = avg4(pb[0], pb[1], pb[hw], pb[hw+1]);
  224. pa[1] = avg4(pb[1], pb[2], pb[hw+1], pb[hw+2]);
  225. pa[2] = avg4(pb[2], pb[3], pb[hw+2], pb[hw+3]);
  226. pa[3] = avg4(pb[3], pb[4], pb[hw+3], pb[hw+4]);
  227. break;
  228. }
  229. pa += ri->c_stride;
  230. pb += ri->c_stride;
  231. }
  232. pa = ri->current_frame->data[2] + (y * ri->y_stride)/4 + x/2;
  233. pb = ri->last_frame->data[2] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
  234. }
  235. }
  236. static void roqvideo_decode_frame(RoqContext *ri)
  237. {
  238. unsigned int chunk_id = 0, chunk_arg = 0;
  239. unsigned long chunk_size = 0;
  240. int i, j, k, nv1, nv2, vqflg = 0, vqflg_pos = -1;
  241. int vqid, bpos, xpos, ypos, xp, yp, x, y;
  242. int frame_stats[2][4] = {{0},{0}};
  243. roq_qcell *qcell;
  244. unsigned char *buf = ri->buf;
  245. unsigned char *buf_end = ri->buf + ri->size;
  246. while (buf < buf_end) {
  247. chunk_id = get_word(buf);
  248. chunk_size = get_long(buf);
  249. chunk_arg = get_word(buf);
  250. if(chunk_id == RoQ_QUAD_VQ)
  251. break;
  252. if(chunk_id == RoQ_QUAD_CODEBOOK) {
  253. if((nv1 = chunk_arg >> 8) == 0)
  254. nv1 = 256;
  255. if((nv2 = chunk_arg & 0xff) == 0 && nv1 * 6 < chunk_size)
  256. nv2 = 256;
  257. for(i = 0; i < nv1; i++) {
  258. ri->cells[i].y0 = get_byte(buf);
  259. ri->cells[i].y1 = get_byte(buf);
  260. ri->cells[i].y2 = get_byte(buf);
  261. ri->cells[i].y3 = get_byte(buf);
  262. ri->cells[i].u = get_byte(buf);
  263. ri->cells[i].v = get_byte(buf);
  264. }
  265. for(i = 0; i < nv2; i++)
  266. for(j = 0; j < 4; j++)
  267. ri->qcells[i].idx[j] = get_byte(buf);
  268. }
  269. }
  270. bpos = xpos = ypos = 0;
  271. while(bpos < chunk_size) {
  272. for (yp = ypos; yp < ypos + 16; yp += 8)
  273. for (xp = xpos; xp < xpos + 16; xp += 8) {
  274. if (vqflg_pos < 0) {
  275. vqflg = buf[bpos++]; vqflg |= (buf[bpos++] << 8);
  276. vqflg_pos = 7;
  277. }
  278. vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
  279. frame_stats[0][vqid]++;
  280. vqflg_pos--;
  281. switch(vqid) {
  282. case RoQ_ID_MOT:
  283. apply_motion_8x8(ri, xp, yp, 0, 8, 8);
  284. break;
  285. case RoQ_ID_FCC:
  286. apply_motion_8x8(ri, xp, yp, buf[bpos++], chunk_arg >> 8,
  287. chunk_arg & 0xff);
  288. break;
  289. case RoQ_ID_SLD:
  290. qcell = ri->qcells + buf[bpos++];
  291. apply_vector_4x4(ri, xp, yp, ri->cells + qcell->idx[0]);
  292. apply_vector_4x4(ri, xp+4, yp, ri->cells + qcell->idx[1]);
  293. apply_vector_4x4(ri, xp, yp+4, ri->cells + qcell->idx[2]);
  294. apply_vector_4x4(ri, xp+4, yp+4, ri->cells + qcell->idx[3]);
  295. break;
  296. case RoQ_ID_CCC:
  297. for (k = 0; k < 4; k++) {
  298. x = xp; y = yp;
  299. if(k & 0x01) x += 4;
  300. if(k & 0x02) y += 4;
  301. if (vqflg_pos < 0) {
  302. vqflg = buf[bpos++];
  303. vqflg |= (buf[bpos++] << 8);
  304. vqflg_pos = 7;
  305. }
  306. vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
  307. frame_stats[1][vqid]++;
  308. vqflg_pos--;
  309. switch(vqid) {
  310. case RoQ_ID_MOT:
  311. apply_motion_4x4(ri, x, y, 0, 8, 8);
  312. break;
  313. case RoQ_ID_FCC:
  314. apply_motion_4x4(ri, x, y, buf[bpos++],
  315. chunk_arg >> 8, chunk_arg & 0xff);
  316. break;
  317. case RoQ_ID_SLD:
  318. qcell = ri->qcells + buf[bpos++];
  319. apply_vector_2x2(ri, x, y, ri->cells + qcell->idx[0]);
  320. apply_vector_2x2(ri, x+2, y, ri->cells + qcell->idx[1]);
  321. apply_vector_2x2(ri, x, y+2, ri->cells + qcell->idx[2]);
  322. apply_vector_2x2(ri, x+2, y+2, ri->cells + qcell->idx[3]);
  323. break;
  324. case RoQ_ID_CCC:
  325. apply_vector_2x2(ri, x, y, ri->cells + buf[bpos]);
  326. apply_vector_2x2(ri, x+2, y, ri->cells + buf[bpos+1]);
  327. apply_vector_2x2(ri, x, y+2, ri->cells + buf[bpos+2]);
  328. apply_vector_2x2(ri, x+2, y+2, ri->cells + buf[bpos+3]);
  329. bpos += 4;
  330. break;
  331. }
  332. }
  333. break;
  334. default:
  335. av_log(ri->avctx, AV_LOG_ERROR, "Unknown vq code: %d\n", vqid);
  336. }
  337. }
  338. xpos += 16;
  339. if (xpos >= ri->avctx->width) {
  340. xpos -= ri->avctx->width;
  341. ypos += 16;
  342. }
  343. if(ypos >= ri->avctx->height)
  344. break;
  345. }
  346. }
  347. static int roq_decode_init(AVCodecContext *avctx)
  348. {
  349. RoqContext *s = avctx->priv_data;
  350. int i;
  351. s->avctx = avctx;
  352. s->first_frame = 1;
  353. s->last_frame = &s->frames[0];
  354. s->current_frame = &s->frames[1];
  355. avctx->pix_fmt = PIX_FMT_YUV420P;
  356. dsputil_init(&s->dsp, avctx);
  357. uiclp = uiclip+512;
  358. for(i = -512; i < 512; i++)
  359. uiclp[i] = (i < 0 ? 0 : (i > 255 ? 255 : i));
  360. return 0;
  361. }
  362. static int roq_decode_frame(AVCodecContext *avctx,
  363. void *data, int *data_size,
  364. uint8_t *buf, int buf_size)
  365. {
  366. RoqContext *s = avctx->priv_data;
  367. if (avctx->get_buffer(avctx, s->current_frame)) {
  368. av_log(avctx, AV_LOG_ERROR, " RoQ: get_buffer() failed\n");
  369. return -1;
  370. }
  371. s->y_stride = s->current_frame->linesize[0];
  372. s->c_stride = s->current_frame->linesize[1];
  373. s->buf = buf;
  374. s->size = buf_size;
  375. roqvideo_decode_frame(s);
  376. /* release the last frame if it is allocated */
  377. if (s->first_frame)
  378. s->first_frame = 0;
  379. else
  380. avctx->release_buffer(avctx, s->last_frame);
  381. *data_size = sizeof(AVFrame);
  382. *(AVFrame*)data = *s->current_frame;
  383. /* shuffle frames */
  384. FFSWAP(AVFrame *, s->current_frame, s->last_frame);
  385. return buf_size;
  386. }
  387. static int roq_decode_end(AVCodecContext *avctx)
  388. {
  389. RoqContext *s = avctx->priv_data;
  390. /* release the last frame */
  391. if (s->last_frame->data[0])
  392. avctx->release_buffer(avctx, s->last_frame);
  393. return 0;
  394. }
  395. AVCodec roq_decoder = {
  396. "roqvideo",
  397. CODEC_TYPE_VIDEO,
  398. CODEC_ID_ROQ,
  399. sizeof(RoqContext),
  400. roq_decode_init,
  401. NULL,
  402. roq_decode_end,
  403. roq_decode_frame,
  404. CODEC_CAP_DR1,
  405. };