cinepak.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 libavcodec/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 "libavutil/intreadwrite.h"
  35. #include "avcodec.h"
  36. typedef struct {
  37. uint8_t y0, y1, y2, y3;
  38. uint8_t u, v;
  39. } cvid_codebook;
  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 v4_codebook[256];
  46. cvid_codebook v1_codebook[256];
  47. } cvid_strip;
  48. typedef struct CinepakContext {
  49. AVCodecContext *avctx;
  50. AVFrame frame;
  51. const unsigned char *data;
  52. int size;
  53. int width, height;
  54. int palette_video;
  55. cvid_strip strips[MAX_STRIPS];
  56. int sega_film_skip_bytes;
  57. } CinepakContext;
  58. static void cinepak_decode_codebook (cvid_codebook *codebook,
  59. int chunk_id, int size, const uint8_t *data)
  60. {
  61. const uint8_t *eod = (data + size);
  62. uint32_t flag, mask;
  63. int i, n;
  64. /* check if this chunk contains 4- or 6-element vectors */
  65. n = (chunk_id & 0x04) ? 4 : 6;
  66. flag = 0;
  67. mask = 0;
  68. for (i=0; i < 256; i++) {
  69. if ((chunk_id & 0x01) && !(mask >>= 1)) {
  70. if ((data + 4) > eod)
  71. break;
  72. flag = AV_RB32 (data);
  73. data += 4;
  74. mask = 0x80000000;
  75. }
  76. if (!(chunk_id & 0x01) || (flag & mask)) {
  77. if ((data + n) > eod)
  78. break;
  79. if (n == 6) {
  80. codebook[i].y0 = *data++;
  81. codebook[i].y1 = *data++;
  82. codebook[i].y2 = *data++;
  83. codebook[i].y3 = *data++;
  84. codebook[i].u = 128 + *data++;
  85. codebook[i].v = 128 + *data++;
  86. } else {
  87. /* this codebook type indicates either greyscale or
  88. * palettized video; if palettized, U & V components will
  89. * not be used so it is safe to set them to 128 for the
  90. * benefit of greyscale rendering in YUV420P */
  91. codebook[i].y0 = *data++;
  92. codebook[i].y1 = *data++;
  93. codebook[i].y2 = *data++;
  94. codebook[i].y3 = *data++;
  95. codebook[i].u = 128;
  96. codebook[i].v = 128;
  97. }
  98. }
  99. }
  100. }
  101. static int cinepak_decode_vectors (CinepakContext *s, cvid_strip *strip,
  102. int chunk_id, int size, const uint8_t *data)
  103. {
  104. const uint8_t *eod = (data + size);
  105. uint32_t flag, mask;
  106. cvid_codebook *codebook;
  107. unsigned int x, y;
  108. uint32_t iy[4];
  109. uint32_t iu[2];
  110. uint32_t iv[2];
  111. flag = 0;
  112. mask = 0;
  113. for (y=strip->y1; y < strip->y2; y+=4) {
  114. iy[0] = strip->x1 + (y * s->frame.linesize[0]);
  115. iy[1] = iy[0] + s->frame.linesize[0];
  116. iy[2] = iy[1] + s->frame.linesize[0];
  117. iy[3] = iy[2] + s->frame.linesize[0];
  118. iu[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[1]);
  119. iu[1] = iu[0] + s->frame.linesize[1];
  120. iv[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[2]);
  121. iv[1] = iv[0] + s->frame.linesize[2];
  122. for (x=strip->x1; x < strip->x2; x+=4) {
  123. if ((chunk_id & 0x01) && !(mask >>= 1)) {
  124. if ((data + 4) > eod)
  125. return -1;
  126. flag = AV_RB32 (data);
  127. data += 4;
  128. mask = 0x80000000;
  129. }
  130. if (!(chunk_id & 0x01) || (flag & mask)) {
  131. if (!(chunk_id & 0x02) && !(mask >>= 1)) {
  132. if ((data + 4) > eod)
  133. return -1;
  134. flag = AV_RB32 (data);
  135. data += 4;
  136. mask = 0x80000000;
  137. }
  138. if ((chunk_id & 0x02) || (~flag & mask)) {
  139. if (data >= eod)
  140. return -1;
  141. codebook = &strip->v1_codebook[*data++];
  142. s->frame.data[0][iy[0] + 0] = codebook->y0;
  143. s->frame.data[0][iy[0] + 1] = codebook->y0;
  144. s->frame.data[0][iy[1] + 0] = codebook->y0;
  145. s->frame.data[0][iy[1] + 1] = codebook->y0;
  146. if (!s->palette_video) {
  147. s->frame.data[1][iu[0]] = codebook->u;
  148. s->frame.data[2][iv[0]] = codebook->v;
  149. }
  150. s->frame.data[0][iy[0] + 2] = codebook->y1;
  151. s->frame.data[0][iy[0] + 3] = codebook->y1;
  152. s->frame.data[0][iy[1] + 2] = codebook->y1;
  153. s->frame.data[0][iy[1] + 3] = codebook->y1;
  154. if (!s->palette_video) {
  155. s->frame.data[1][iu[0] + 1] = codebook->u;
  156. s->frame.data[2][iv[0] + 1] = codebook->v;
  157. }
  158. s->frame.data[0][iy[2] + 0] = codebook->y2;
  159. s->frame.data[0][iy[2] + 1] = codebook->y2;
  160. s->frame.data[0][iy[3] + 0] = codebook->y2;
  161. s->frame.data[0][iy[3] + 1] = codebook->y2;
  162. if (!s->palette_video) {
  163. s->frame.data[1][iu[1]] = codebook->u;
  164. s->frame.data[2][iv[1]] = codebook->v;
  165. }
  166. s->frame.data[0][iy[2] + 2] = codebook->y3;
  167. s->frame.data[0][iy[2] + 3] = codebook->y3;
  168. s->frame.data[0][iy[3] + 2] = codebook->y3;
  169. s->frame.data[0][iy[3] + 3] = codebook->y3;
  170. if (!s->palette_video) {
  171. s->frame.data[1][iu[1] + 1] = codebook->u;
  172. s->frame.data[2][iv[1] + 1] = codebook->v;
  173. }
  174. } else if (flag & mask) {
  175. if ((data + 4) > eod)
  176. return -1;
  177. codebook = &strip->v4_codebook[*data++];
  178. s->frame.data[0][iy[0] + 0] = codebook->y0;
  179. s->frame.data[0][iy[0] + 1] = codebook->y1;
  180. s->frame.data[0][iy[1] + 0] = codebook->y2;
  181. s->frame.data[0][iy[1] + 1] = codebook->y3;
  182. if (!s->palette_video) {
  183. s->frame.data[1][iu[0]] = codebook->u;
  184. s->frame.data[2][iv[0]] = codebook->v;
  185. }
  186. codebook = &strip->v4_codebook[*data++];
  187. s->frame.data[0][iy[0] + 2] = codebook->y0;
  188. s->frame.data[0][iy[0] + 3] = codebook->y1;
  189. s->frame.data[0][iy[1] + 2] = codebook->y2;
  190. s->frame.data[0][iy[1] + 3] = codebook->y3;
  191. if (!s->palette_video) {
  192. s->frame.data[1][iu[0] + 1] = codebook->u;
  193. s->frame.data[2][iv[0] + 1] = codebook->v;
  194. }
  195. codebook = &strip->v4_codebook[*data++];
  196. s->frame.data[0][iy[2] + 0] = codebook->y0;
  197. s->frame.data[0][iy[2] + 1] = codebook->y1;
  198. s->frame.data[0][iy[3] + 0] = codebook->y2;
  199. s->frame.data[0][iy[3] + 1] = codebook->y3;
  200. if (!s->palette_video) {
  201. s->frame.data[1][iu[1]] = codebook->u;
  202. s->frame.data[2][iv[1]] = codebook->v;
  203. }
  204. codebook = &strip->v4_codebook[*data++];
  205. s->frame.data[0][iy[2] + 2] = codebook->y0;
  206. s->frame.data[0][iy[2] + 3] = codebook->y1;
  207. s->frame.data[0][iy[3] + 2] = codebook->y2;
  208. s->frame.data[0][iy[3] + 3] = codebook->y3;
  209. if (!s->palette_video) {
  210. s->frame.data[1][iu[1] + 1] = codebook->u;
  211. s->frame.data[2][iv[1] + 1] = codebook->v;
  212. }
  213. }
  214. }
  215. iy[0] += 4; iy[1] += 4;
  216. iy[2] += 4; iy[3] += 4;
  217. iu[0] += 2; iu[1] += 2;
  218. iv[0] += 2; iv[1] += 2;
  219. }
  220. }
  221. return 0;
  222. }
  223. static int cinepak_decode_strip (CinepakContext *s,
  224. cvid_strip *strip, const uint8_t *data, int size)
  225. {
  226. const uint8_t *eod = (data + size);
  227. int chunk_id, chunk_size;
  228. /* coordinate sanity checks */
  229. if (strip->x1 >= s->width || strip->x2 > s->width ||
  230. strip->y1 >= s->height || strip->y2 > s->height ||
  231. strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
  232. return -1;
  233. while ((data + 4) <= eod) {
  234. chunk_id = data[0];
  235. chunk_size = AV_RB24 (&data[1]) - 4;
  236. if(chunk_size < 0)
  237. return -1;
  238. data += 4;
  239. chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
  240. switch (chunk_id) {
  241. case 0x20:
  242. case 0x21:
  243. case 0x24:
  244. case 0x25:
  245. cinepak_decode_codebook (strip->v4_codebook, chunk_id,
  246. chunk_size, data);
  247. break;
  248. case 0x22:
  249. case 0x23:
  250. case 0x26:
  251. case 0x27:
  252. cinepak_decode_codebook (strip->v1_codebook, chunk_id,
  253. chunk_size, data);
  254. break;
  255. case 0x30:
  256. case 0x31:
  257. case 0x32:
  258. return cinepak_decode_vectors (s, strip, chunk_id,
  259. chunk_size, data);
  260. }
  261. data += chunk_size;
  262. }
  263. return -1;
  264. }
  265. static int cinepak_decode (CinepakContext *s)
  266. {
  267. const uint8_t *eod = (s->data + s->size);
  268. int i, result, strip_size, frame_flags, num_strips;
  269. int y0 = 0;
  270. int encoded_buf_size;
  271. if (s->size < 10)
  272. return -1;
  273. frame_flags = s->data[0];
  274. num_strips = AV_RB16 (&s->data[8]);
  275. encoded_buf_size = ((s->data[1] << 16) | AV_RB16 (&s->data[2]));
  276. /* if this is the first frame, check for deviant Sega FILM data */
  277. if (s->sega_film_skip_bytes == -1) {
  278. if (encoded_buf_size != s->size) {
  279. /* If the encoded frame size differs from the frame size as indicated
  280. * by the container file, this data likely comes from a Sega FILM/CPK file.
  281. * If the frame header is followed by the bytes FE 00 00 06 00 00 then
  282. * this is probably one of the two known files that have 6 extra bytes
  283. * after the frame header. Else, assume 2 extra bytes. */
  284. if (s->size >= 16 &&
  285. (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 = s->data[0];
  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_RB24 (&s->data[1]) - 12;
  309. if(strip_size < 0)
  310. return -1;
  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 av_cold int cinepak_decode_init(AVCodecContext *avctx)
  328. {
  329. CinepakContext *s = 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_coded_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. s->frame.data[0] = NULL;
  343. return 0;
  344. }
  345. static int cinepak_decode_frame(AVCodecContext *avctx,
  346. void *data, int *data_size,
  347. const uint8_t *buf, int buf_size)
  348. {
  349. CinepakContext *s = avctx->priv_data;
  350. s->data = buf;
  351. s->size = buf_size;
  352. s->frame.reference = 1;
  353. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  354. FF_BUFFER_HINTS_REUSABLE;
  355. if (avctx->reget_buffer(avctx, &s->frame)) {
  356. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  357. return -1;
  358. }
  359. cinepak_decode(s);
  360. if (s->palette_video) {
  361. memcpy (s->frame.data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  362. if (avctx->palctrl->palette_changed) {
  363. s->frame.palette_has_changed = 1;
  364. avctx->palctrl->palette_changed = 0;
  365. } else
  366. s->frame.palette_has_changed = 0;
  367. }
  368. *data_size = sizeof(AVFrame);
  369. *(AVFrame*)data = s->frame;
  370. /* report that the buffer was completely consumed */
  371. return buf_size;
  372. }
  373. static av_cold int cinepak_decode_end(AVCodecContext *avctx)
  374. {
  375. CinepakContext *s = avctx->priv_data;
  376. if (s->frame.data[0])
  377. avctx->release_buffer(avctx, &s->frame);
  378. return 0;
  379. }
  380. AVCodec cinepak_decoder = {
  381. "cinepak",
  382. CODEC_TYPE_VIDEO,
  383. CODEC_ID_CINEPAK,
  384. sizeof(CinepakContext),
  385. cinepak_decode_init,
  386. NULL,
  387. cinepak_decode_end,
  388. cinepak_decode_frame,
  389. CODEC_CAP_DR1,
  390. .long_name = NULL_IF_CONFIG_SMALL("Cinepak"),
  391. };