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.

473 lines
15KB

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