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.

407 lines
12KB

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