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.

474 lines
15KB

  1. /*
  2. * Cinepak Video Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file cinepak.c
  24. * Cinepak video decoder
  25. * by Ewald Snel <ewald@rambo.its.tudelft.nl>
  26. * For more information on the Cinepak algorithm, visit:
  27. * http://www.csse.monash.edu.au/~timf/
  28. * For more information on the quirky data inside Sega FILM/CPK files, visit:
  29. * http://wiki.multimedia.cx/index.php?title=Sega_FILM
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include "common.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. typedef struct {
  39. uint8_t y0, y1, y2, y3;
  40. uint8_t u, v;
  41. } cvid_codebook_t;
  42. #define MAX_STRIPS 32
  43. typedef struct {
  44. uint16_t id;
  45. uint16_t x1, y1;
  46. uint16_t x2, y2;
  47. cvid_codebook_t v4_codebook[256];
  48. cvid_codebook_t v1_codebook[256];
  49. } cvid_strip_t;
  50. typedef struct CinepakContext {
  51. AVCodecContext *avctx;
  52. DSPContext dsp;
  53. AVFrame frame;
  54. unsigned char *data;
  55. int size;
  56. int width, height;
  57. int palette_video;
  58. cvid_strip_t strips[MAX_STRIPS];
  59. int sega_film_skip_bytes;
  60. } CinepakContext;
  61. static void cinepak_decode_codebook (cvid_codebook_t *codebook,
  62. int chunk_id, int size, uint8_t *data)
  63. {
  64. uint8_t *eod = (data + size);
  65. uint32_t flag, mask;
  66. int i, n;
  67. /* check if this chunk contains 4- or 6-element vectors */
  68. n = (chunk_id & 0x0400) ? 4 : 6;
  69. flag = 0;
  70. mask = 0;
  71. for (i=0; i < 256; i++) {
  72. if ((chunk_id & 0x0100) && !(mask >>= 1)) {
  73. if ((data + 4) > eod)
  74. break;
  75. flag = AV_RB32 (data);
  76. data += 4;
  77. mask = 0x80000000;
  78. }
  79. if (!(chunk_id & 0x0100) || (flag & mask)) {
  80. if ((data + n) > eod)
  81. break;
  82. if (n == 6) {
  83. codebook[i].y0 = *data++;
  84. codebook[i].y1 = *data++;
  85. codebook[i].y2 = *data++;
  86. codebook[i].y3 = *data++;
  87. codebook[i].u = 128 + *data++;
  88. codebook[i].v = 128 + *data++;
  89. } else {
  90. /* this codebook type indicates either greyscale or
  91. * palettized video; if palettized, U & V components will
  92. * not be used so it is safe to set them to 128 for the
  93. * benefit of greyscale rendering in YUV420P */
  94. codebook[i].y0 = *data++;
  95. codebook[i].y1 = *data++;
  96. codebook[i].y2 = *data++;
  97. codebook[i].y3 = *data++;
  98. codebook[i].u = 128;
  99. codebook[i].v = 128;
  100. }
  101. }
  102. }
  103. }
  104. static int cinepak_decode_vectors (CinepakContext *s, cvid_strip_t *strip,
  105. int chunk_id, int size, uint8_t *data)
  106. {
  107. uint8_t *eod = (data + size);
  108. uint32_t flag, mask;
  109. cvid_codebook_t *codebook;
  110. unsigned int x, y;
  111. uint32_t iy[4];
  112. uint32_t iu[2];
  113. uint32_t iv[2];
  114. flag = 0;
  115. mask = 0;
  116. for (y=strip->y1; y < strip->y2; y+=4) {
  117. iy[0] = strip->x1 + (y * s->frame.linesize[0]);
  118. iy[1] = iy[0] + s->frame.linesize[0];
  119. iy[2] = iy[1] + s->frame.linesize[0];
  120. iy[3] = iy[2] + s->frame.linesize[0];
  121. iu[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[1]);
  122. iu[1] = iu[0] + s->frame.linesize[1];
  123. iv[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[2]);
  124. iv[1] = iv[0] + s->frame.linesize[2];
  125. for (x=strip->x1; x < strip->x2; x+=4) {
  126. if ((chunk_id & 0x0100) && !(mask >>= 1)) {
  127. if ((data + 4) > eod)
  128. return -1;
  129. flag = AV_RB32 (data);
  130. data += 4;
  131. mask = 0x80000000;
  132. }
  133. if (!(chunk_id & 0x0100) || (flag & mask)) {
  134. if (!(chunk_id & 0x0200) && !(mask >>= 1)) {
  135. if ((data + 4) > eod)
  136. return -1;
  137. flag = AV_RB32 (data);
  138. data += 4;
  139. mask = 0x80000000;
  140. }
  141. if ((chunk_id & 0x0200) || (~flag & mask)) {
  142. if (data >= eod)
  143. return -1;
  144. codebook = &strip->v1_codebook[*data++];
  145. s->frame.data[0][iy[0] + 0] = codebook->y0;
  146. s->frame.data[0][iy[0] + 1] = codebook->y0;
  147. s->frame.data[0][iy[1] + 0] = codebook->y0;
  148. s->frame.data[0][iy[1] + 1] = codebook->y0;
  149. if (!s->palette_video) {
  150. s->frame.data[1][iu[0]] = codebook->u;
  151. s->frame.data[2][iv[0]] = codebook->v;
  152. }
  153. s->frame.data[0][iy[0] + 2] = codebook->y1;
  154. s->frame.data[0][iy[0] + 3] = codebook->y1;
  155. s->frame.data[0][iy[1] + 2] = codebook->y1;
  156. s->frame.data[0][iy[1] + 3] = codebook->y1;
  157. if (!s->palette_video) {
  158. s->frame.data[1][iu[0] + 1] = codebook->u;
  159. s->frame.data[2][iv[0] + 1] = codebook->v;
  160. }
  161. s->frame.data[0][iy[2] + 0] = codebook->y2;
  162. s->frame.data[0][iy[2] + 1] = codebook->y2;
  163. s->frame.data[0][iy[3] + 0] = codebook->y2;
  164. s->frame.data[0][iy[3] + 1] = codebook->y2;
  165. if (!s->palette_video) {
  166. s->frame.data[1][iu[1]] = codebook->u;
  167. s->frame.data[2][iv[1]] = codebook->v;
  168. }
  169. s->frame.data[0][iy[2] + 2] = codebook->y3;
  170. s->frame.data[0][iy[2] + 3] = codebook->y3;
  171. s->frame.data[0][iy[3] + 2] = codebook->y3;
  172. s->frame.data[0][iy[3] + 3] = codebook->y3;
  173. if (!s->palette_video) {
  174. s->frame.data[1][iu[1] + 1] = codebook->u;
  175. s->frame.data[2][iv[1] + 1] = codebook->v;
  176. }
  177. } else if (flag & mask) {
  178. if ((data + 4) > eod)
  179. return -1;
  180. codebook = &strip->v4_codebook[*data++];
  181. s->frame.data[0][iy[0] + 0] = codebook->y0;
  182. s->frame.data[0][iy[0] + 1] = codebook->y1;
  183. s->frame.data[0][iy[1] + 0] = codebook->y2;
  184. s->frame.data[0][iy[1] + 1] = codebook->y3;
  185. if (!s->palette_video) {
  186. s->frame.data[1][iu[0]] = codebook->u;
  187. s->frame.data[2][iv[0]] = codebook->v;
  188. }
  189. codebook = &strip->v4_codebook[*data++];
  190. s->frame.data[0][iy[0] + 2] = codebook->y0;
  191. s->frame.data[0][iy[0] + 3] = codebook->y1;
  192. s->frame.data[0][iy[1] + 2] = codebook->y2;
  193. s->frame.data[0][iy[1] + 3] = codebook->y3;
  194. if (!s->palette_video) {
  195. s->frame.data[1][iu[0] + 1] = codebook->u;
  196. s->frame.data[2][iv[0] + 1] = codebook->v;
  197. }
  198. codebook = &strip->v4_codebook[*data++];
  199. s->frame.data[0][iy[2] + 0] = codebook->y0;
  200. s->frame.data[0][iy[2] + 1] = codebook->y1;
  201. s->frame.data[0][iy[3] + 0] = codebook->y2;
  202. s->frame.data[0][iy[3] + 1] = codebook->y3;
  203. if (!s->palette_video) {
  204. s->frame.data[1][iu[1]] = codebook->u;
  205. s->frame.data[2][iv[1]] = codebook->v;
  206. }
  207. codebook = &strip->v4_codebook[*data++];
  208. s->frame.data[0][iy[2] + 2] = codebook->y0;
  209. s->frame.data[0][iy[2] + 3] = codebook->y1;
  210. s->frame.data[0][iy[3] + 2] = codebook->y2;
  211. s->frame.data[0][iy[3] + 3] = codebook->y3;
  212. if (!s->palette_video) {
  213. s->frame.data[1][iu[1] + 1] = codebook->u;
  214. s->frame.data[2][iv[1] + 1] = codebook->v;
  215. }
  216. }
  217. }
  218. iy[0] += 4; iy[1] += 4;
  219. iy[2] += 4; iy[3] += 4;
  220. iu[0] += 2; iu[1] += 2;
  221. iv[0] += 2; iv[1] += 2;
  222. }
  223. }
  224. return 0;
  225. }
  226. static int cinepak_decode_strip (CinepakContext *s,
  227. cvid_strip_t *strip, uint8_t *data, int size)
  228. {
  229. uint8_t *eod = (data + size);
  230. int chunk_id, chunk_size;
  231. /* coordinate sanity checks */
  232. if (strip->x1 >= s->width || strip->x2 > s->width ||
  233. strip->y1 >= s->height || strip->y2 > s->height ||
  234. strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
  235. return -1;
  236. while ((data + 4) <= eod) {
  237. chunk_id = AV_RB16 (&data[0]);
  238. chunk_size = AV_RB16 (&data[2]) - 4;
  239. if(chunk_size < 0)
  240. return -1;
  241. data += 4;
  242. chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
  243. switch (chunk_id) {
  244. case 0x2000:
  245. case 0x2100:
  246. case 0x2400:
  247. case 0x2500:
  248. cinepak_decode_codebook (strip->v4_codebook, chunk_id,
  249. chunk_size, data);
  250. break;
  251. case 0x2200:
  252. case 0x2300:
  253. case 0x2600:
  254. case 0x2700:
  255. cinepak_decode_codebook (strip->v1_codebook, chunk_id,
  256. chunk_size, data);
  257. break;
  258. case 0x3000:
  259. case 0x3100:
  260. case 0x3200:
  261. return cinepak_decode_vectors (s, strip, chunk_id,
  262. chunk_size, data);
  263. }
  264. data += chunk_size;
  265. }
  266. return -1;
  267. }
  268. static int cinepak_decode (CinepakContext *s)
  269. {
  270. uint8_t *eod = (s->data + s->size);
  271. int i, result, strip_size, frame_flags, num_strips;
  272. int y0 = 0;
  273. int encoded_buf_size;
  274. if (s->size < 10)
  275. return -1;
  276. frame_flags = s->data[0];
  277. num_strips = AV_RB16 (&s->data[8]);
  278. encoded_buf_size = ((s->data[1] << 16) | AV_RB16 (&s->data[2]));
  279. /* if this is the first frame, check for deviant Sega FILM data */
  280. if (s->sega_film_skip_bytes == -1) {
  281. if (encoded_buf_size != s->size) {
  282. /* If the encoded frame size differs from the frame size as indicated
  283. * by the container file, this data likely comes from a Sega FILM/CPK file.
  284. * If the frame header is followed by the bytes FE 00 00 06 00 00 then
  285. * this is probably one of the two known files that have 6 extra bytes
  286. * after the frame header. Else, assume 2 extra bytes. */
  287. if ((s->data[10] == 0xFE) &&
  288. (s->data[11] == 0x00) &&
  289. (s->data[12] == 0x00) &&
  290. (s->data[13] == 0x06) &&
  291. (s->data[14] == 0x00) &&
  292. (s->data[15] == 0x00))
  293. s->sega_film_skip_bytes = 6;
  294. else
  295. s->sega_film_skip_bytes = 2;
  296. } else
  297. s->sega_film_skip_bytes = 0;
  298. }
  299. s->data += 10 + s->sega_film_skip_bytes;
  300. if (num_strips > MAX_STRIPS)
  301. num_strips = MAX_STRIPS;
  302. for (i=0; i < num_strips; i++) {
  303. if ((s->data + 12) > eod)
  304. return -1;
  305. s->strips[i].id = AV_RB16 (s->data);
  306. s->strips[i].y1 = y0;
  307. s->strips[i].x1 = 0;
  308. s->strips[i].y2 = y0 + AV_RB16 (&s->data[8]);
  309. s->strips[i].x2 = s->avctx->width;
  310. strip_size = AV_RB16 (&s->data[2]) - 12;
  311. s->data += 12;
  312. strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
  313. if ((i > 0) && !(frame_flags & 0x01)) {
  314. memcpy (s->strips[i].v4_codebook, s->strips[i-1].v4_codebook,
  315. sizeof(s->strips[i].v4_codebook));
  316. memcpy (s->strips[i].v1_codebook, s->strips[i-1].v1_codebook,
  317. sizeof(s->strips[i].v1_codebook));
  318. }
  319. result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
  320. if (result != 0)
  321. return result;
  322. s->data += strip_size;
  323. y0 = s->strips[i].y2;
  324. }
  325. return 0;
  326. }
  327. static int cinepak_decode_init(AVCodecContext *avctx)
  328. {
  329. CinepakContext *s = (CinepakContext *)avctx->priv_data;
  330. s->avctx = avctx;
  331. s->width = (avctx->width + 3) & ~3;
  332. s->height = (avctx->height + 3) & ~3;
  333. s->sega_film_skip_bytes = -1; /* uninitialized state */
  334. // check for paletted data
  335. if ((avctx->palctrl == NULL) || (avctx->bits_per_sample == 40)) {
  336. s->palette_video = 0;
  337. avctx->pix_fmt = PIX_FMT_YUV420P;
  338. } else {
  339. s->palette_video = 1;
  340. avctx->pix_fmt = PIX_FMT_PAL8;
  341. }
  342. avctx->has_b_frames = 0;
  343. dsputil_init(&s->dsp, avctx);
  344. s->frame.data[0] = NULL;
  345. return 0;
  346. }
  347. static int cinepak_decode_frame(AVCodecContext *avctx,
  348. void *data, int *data_size,
  349. uint8_t *buf, int buf_size)
  350. {
  351. CinepakContext *s = (CinepakContext *)avctx->priv_data;
  352. s->data = buf;
  353. s->size = buf_size;
  354. s->frame.reference = 1;
  355. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  356. FF_BUFFER_HINTS_REUSABLE;
  357. if (avctx->reget_buffer(avctx, &s->frame)) {
  358. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  359. return -1;
  360. }
  361. cinepak_decode(s);
  362. if (s->palette_video) {
  363. memcpy (s->frame.data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  364. if (avctx->palctrl->palette_changed) {
  365. s->frame.palette_has_changed = 1;
  366. avctx->palctrl->palette_changed = 0;
  367. } else
  368. s->frame.palette_has_changed = 0;
  369. }
  370. *data_size = sizeof(AVFrame);
  371. *(AVFrame*)data = s->frame;
  372. /* report that the buffer was completely consumed */
  373. return buf_size;
  374. }
  375. static int cinepak_decode_end(AVCodecContext *avctx)
  376. {
  377. CinepakContext *s = (CinepakContext *)avctx->priv_data;
  378. if (s->frame.data[0])
  379. avctx->release_buffer(avctx, &s->frame);
  380. return 0;
  381. }
  382. AVCodec cinepak_decoder = {
  383. "cinepak",
  384. CODEC_TYPE_VIDEO,
  385. CODEC_ID_CINEPAK,
  386. sizeof(CinepakContext),
  387. cinepak_decode_init,
  388. NULL,
  389. cinepak_decode_end,
  390. cinepak_decode_frame,
  391. CODEC_CAP_DR1,
  392. };