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.

480 lines
16KB

  1. /*
  2. * Cinepak Video Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Cinepak video decoder
  24. * @author Ewald Snel <ewald@rambo.its.tudelft.nl>
  25. *
  26. * @see For more information on the Cinepak algorithm, visit:
  27. * http://www.csse.monash.edu.au/~timf/
  28. * @see 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 "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. uint32_t pal[256];
  58. } CinepakContext;
  59. static void cinepak_decode_codebook (cvid_codebook *codebook,
  60. int chunk_id, int size, const uint8_t *data)
  61. {
  62. const 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 & 0x04) ? 4 : 6;
  67. flag = 0;
  68. mask = 0;
  69. for (i=0; i < 256; i++) {
  70. if ((chunk_id & 0x01) && !(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 & 0x01) || (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 *strip,
  103. int chunk_id, int size, const uint8_t *data)
  104. {
  105. const uint8_t *eod = (data + size);
  106. uint32_t flag, mask;
  107. cvid_codebook *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 & 0x01) && !(mask >>= 1)) {
  125. if ((data + 4) > eod)
  126. return AVERROR_INVALIDDATA;
  127. flag = AV_RB32 (data);
  128. data += 4;
  129. mask = 0x80000000;
  130. }
  131. if (!(chunk_id & 0x01) || (flag & mask)) {
  132. if (!(chunk_id & 0x02) && !(mask >>= 1)) {
  133. if ((data + 4) > eod)
  134. return AVERROR_INVALIDDATA;
  135. flag = AV_RB32 (data);
  136. data += 4;
  137. mask = 0x80000000;
  138. }
  139. if ((chunk_id & 0x02) || (~flag & mask)) {
  140. if (data >= eod)
  141. return AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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 *strip, const uint8_t *data, int size)
  226. {
  227. const uint8_t *eod = (data + size);
  228. int chunk_id, chunk_size;
  229. /* coordinate sanity checks */
  230. if (strip->x2 > s->width ||
  231. strip->y2 > s->height ||
  232. strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
  233. return AVERROR_INVALIDDATA;
  234. while ((data + 4) <= eod) {
  235. chunk_id = data[0];
  236. chunk_size = AV_RB24 (&data[1]) - 4;
  237. if(chunk_size < 0)
  238. return AVERROR_INVALIDDATA;
  239. data += 4;
  240. chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
  241. switch (chunk_id) {
  242. case 0x20:
  243. case 0x21:
  244. case 0x24:
  245. case 0x25:
  246. cinepak_decode_codebook (strip->v4_codebook, chunk_id,
  247. chunk_size, data);
  248. break;
  249. case 0x22:
  250. case 0x23:
  251. case 0x26:
  252. case 0x27:
  253. cinepak_decode_codebook (strip->v1_codebook, chunk_id,
  254. chunk_size, data);
  255. break;
  256. case 0x30:
  257. case 0x31:
  258. case 0x32:
  259. return cinepak_decode_vectors (s, strip, chunk_id,
  260. chunk_size, data);
  261. }
  262. data += chunk_size;
  263. }
  264. return AVERROR_INVALIDDATA;
  265. }
  266. static int cinepak_decode (CinepakContext *s)
  267. {
  268. const 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 AVERROR_INVALIDDATA;
  274. frame_flags = s->data[0];
  275. num_strips = AV_RB16 (&s->data[8]);
  276. encoded_buf_size = AV_RB24(&s->data[1]);
  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) {
  280. av_log_ask_for_sample(s->avctx, "encoded_buf_size is 0");
  281. return AVERROR_INVALIDDATA;
  282. }
  283. if (encoded_buf_size != s->size && (s->size % encoded_buf_size) != 0) {
  284. /* If the encoded frame size differs from the frame size as indicated
  285. * by the container file, this data likely comes from a Sega FILM/CPK file.
  286. * If the frame header is followed by the bytes FE 00 00 06 00 00 then
  287. * this is probably one of the two known files that have 6 extra bytes
  288. * after the frame header. Else, assume 2 extra bytes. The container
  289. * size also cannot be a multiple of the encoded size. */
  290. if (s->size >= 16 &&
  291. (s->data[10] == 0xFE) &&
  292. (s->data[11] == 0x00) &&
  293. (s->data[12] == 0x00) &&
  294. (s->data[13] == 0x06) &&
  295. (s->data[14] == 0x00) &&
  296. (s->data[15] == 0x00))
  297. s->sega_film_skip_bytes = 6;
  298. else
  299. s->sega_film_skip_bytes = 2;
  300. } else
  301. s->sega_film_skip_bytes = 0;
  302. }
  303. s->data += 10 + s->sega_film_skip_bytes;
  304. num_strips = FFMIN(num_strips, MAX_STRIPS);
  305. for (i=0; i < num_strips; i++) {
  306. if ((s->data + 12) > eod)
  307. return AVERROR_INVALIDDATA;
  308. s->strips[i].id = s->data[0];
  309. s->strips[i].y1 = y0;
  310. s->strips[i].x1 = 0;
  311. s->strips[i].y2 = y0 + AV_RB16 (&s->data[8]);
  312. s->strips[i].x2 = s->avctx->width;
  313. strip_size = AV_RB24 (&s->data[1]) - 12;
  314. if (strip_size < 0)
  315. return AVERROR_INVALIDDATA;
  316. s->data += 12;
  317. strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
  318. if ((i > 0) && !(frame_flags & 0x01)) {
  319. memcpy (s->strips[i].v4_codebook, s->strips[i-1].v4_codebook,
  320. sizeof(s->strips[i].v4_codebook));
  321. memcpy (s->strips[i].v1_codebook, s->strips[i-1].v1_codebook,
  322. sizeof(s->strips[i].v1_codebook));
  323. }
  324. result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
  325. if (result != 0)
  326. return result;
  327. s->data += strip_size;
  328. y0 = s->strips[i].y2;
  329. }
  330. return 0;
  331. }
  332. static av_cold int cinepak_decode_init(AVCodecContext *avctx)
  333. {
  334. CinepakContext *s = avctx->priv_data;
  335. s->avctx = avctx;
  336. s->width = (avctx->width + 3) & ~3;
  337. s->height = (avctx->height + 3) & ~3;
  338. s->sega_film_skip_bytes = -1; /* uninitialized state */
  339. // check for paletted data
  340. if (avctx->bits_per_coded_sample != 8) {
  341. s->palette_video = 0;
  342. avctx->pix_fmt = PIX_FMT_YUV420P;
  343. } else {
  344. s->palette_video = 1;
  345. avctx->pix_fmt = PIX_FMT_PAL8;
  346. }
  347. s->frame.data[0] = NULL;
  348. return 0;
  349. }
  350. static int cinepak_decode_frame(AVCodecContext *avctx,
  351. void *data, int *data_size,
  352. AVPacket *avpkt)
  353. {
  354. const uint8_t *buf = avpkt->data;
  355. int ret = 0, buf_size = avpkt->size;
  356. CinepakContext *s = avctx->priv_data;
  357. s->data = buf;
  358. s->size = buf_size;
  359. s->frame.reference = 1;
  360. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  361. FF_BUFFER_HINTS_REUSABLE;
  362. if ((ret = avctx->reget_buffer(avctx, &s->frame))) {
  363. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  364. return ret;
  365. }
  366. if (s->palette_video) {
  367. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  368. if (pal) {
  369. s->frame.palette_has_changed = 1;
  370. memcpy(s->pal, pal, AVPALETTE_SIZE);
  371. }
  372. }
  373. cinepak_decode(s);
  374. if (s->palette_video)
  375. memcpy (s->frame.data[1], s->pal, AVPALETTE_SIZE);
  376. *data_size = sizeof(AVFrame);
  377. *(AVFrame*)data = s->frame;
  378. /* report that the buffer was completely consumed */
  379. return buf_size;
  380. }
  381. static av_cold int cinepak_decode_end(AVCodecContext *avctx)
  382. {
  383. CinepakContext *s = avctx->priv_data;
  384. if (s->frame.data[0])
  385. avctx->release_buffer(avctx, &s->frame);
  386. return 0;
  387. }
  388. AVCodec ff_cinepak_decoder = {
  389. .name = "cinepak",
  390. .type = AVMEDIA_TYPE_VIDEO,
  391. .id = CODEC_ID_CINEPAK,
  392. .priv_data_size = sizeof(CinepakContext),
  393. .init = cinepak_decode_init,
  394. .close = cinepak_decode_end,
  395. .decode = cinepak_decode_frame,
  396. .capabilities = CODEC_CAP_DR1,
  397. .long_name = NULL_IF_CONFIG_SMALL("Cinepak"),
  398. };