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.

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