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.

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