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.

489 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
  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. * Cinepak colorspace support (c) 2013 Rl, Aetey Global Technologies AB
  32. * @author Cinepak colorspace, Rl, Aetey Global Technologies AB
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include "libavutil/common.h"
  38. #include "libavutil/intreadwrite.h"
  39. #include "avcodec.h"
  40. #include "internal.h"
  41. typedef uint8_t cvid_codebook[12];
  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 v4_codebook[256];
  48. cvid_codebook v1_codebook[256];
  49. } cvid_strip;
  50. typedef struct CinepakContext {
  51. AVCodecContext *avctx;
  52. AVFrame *frame;
  53. const unsigned char *data;
  54. int size;
  55. int width, height;
  56. int palette_video;
  57. cvid_strip strips[MAX_STRIPS];
  58. int sega_film_skip_bytes;
  59. uint32_t pal[256];
  60. } CinepakContext;
  61. static void cinepak_decode_codebook (cvid_codebook *codebook,
  62. int chunk_id, int size, const uint8_t *data)
  63. {
  64. const uint8_t *eod = (data + size);
  65. uint32_t flag, mask;
  66. int i, n;
  67. uint8_t *p;
  68. /* check if this chunk contains 4- or 6-element vectors */
  69. n = (chunk_id & 0x04) ? 4 : 6;
  70. flag = 0;
  71. mask = 0;
  72. p = codebook[0];
  73. for (i=0; i < 256; i++) {
  74. if ((chunk_id & 0x01) && !(mask >>= 1)) {
  75. if ((data + 4) > eod)
  76. break;
  77. flag = AV_RB32 (data);
  78. data += 4;
  79. mask = 0x80000000;
  80. }
  81. if (!(chunk_id & 0x01) || (flag & mask)) {
  82. int k, kk;
  83. if ((data + n) > eod)
  84. break;
  85. for (k = 0; k < 4; ++k) {
  86. int r = *data++;
  87. for (kk = 0; kk < 3; ++kk)
  88. *p++ = r;
  89. }
  90. if (n == 6) {
  91. int r, g, b, u, v;
  92. u = *(int8_t *)data++;
  93. v = *(int8_t *)data++;
  94. p -= 12;
  95. for(k=0; k<4; ++k) {
  96. r = *p++ + v*2;
  97. g = *p++ - (u/2) - v;
  98. b = *p + u*2;
  99. p -= 2;
  100. *p++ = av_clip_uint8(r);
  101. *p++ = av_clip_uint8(g);
  102. *p++ = av_clip_uint8(b);
  103. }
  104. }
  105. } else {
  106. p += 12;
  107. }
  108. }
  109. }
  110. static int cinepak_decode_vectors (CinepakContext *s, cvid_strip *strip,
  111. int chunk_id, int size, const uint8_t *data)
  112. {
  113. const uint8_t *eod = (data + size);
  114. uint32_t flag, mask;
  115. uint8_t *cb0, *cb1, *cb2, *cb3;
  116. unsigned int x, y;
  117. char *ip0, *ip1, *ip2, *ip3;
  118. flag = 0;
  119. mask = 0;
  120. for (y=strip->y1; y < strip->y2; y+=4) {
  121. /* take care of y dimension not being multiple of 4, such streams exist */
  122. ip0 = ip1 = ip2 = ip3 = s->frame->data[0] +
  123. (s->palette_video?strip->x1:strip->x1*3) + (y * s->frame->linesize[0]);
  124. if(s->avctx->height - y > 1) {
  125. ip1 = ip0 + s->frame->linesize[0];
  126. if(s->avctx->height - y > 2) {
  127. ip2 = ip1 + s->frame->linesize[0];
  128. if(s->avctx->height - y > 3) {
  129. ip3 = ip2 + s->frame->linesize[0];
  130. }
  131. }
  132. }
  133. /* to get the correct picture for not-multiple-of-4 cases let us fill
  134. * each block from the bottom up, thus possibly overwriting the top line
  135. * more than once but ending with the correct data in place
  136. * (instead of in-loop checking) */
  137. for (x=strip->x1; x < strip->x2; x+=4) {
  138. if ((chunk_id & 0x01) && !(mask >>= 1)) {
  139. if ((data + 4) > eod)
  140. return AVERROR_INVALIDDATA;
  141. flag = AV_RB32 (data);
  142. data += 4;
  143. mask = 0x80000000;
  144. }
  145. if (!(chunk_id & 0x01) || (flag & mask)) {
  146. if (!(chunk_id & 0x02) && !(mask >>= 1)) {
  147. if ((data + 4) > eod)
  148. return AVERROR_INVALIDDATA;
  149. flag = AV_RB32 (data);
  150. data += 4;
  151. mask = 0x80000000;
  152. }
  153. if ((chunk_id & 0x02) || (~flag & mask)) {
  154. uint8_t *p;
  155. if (data >= eod)
  156. return AVERROR_INVALIDDATA;
  157. p = strip->v1_codebook[*data++];
  158. if (s->palette_video) {
  159. ip3[0] = ip3[1] = ip2[0] = ip2[1] = p[6];
  160. ip3[2] = ip3[3] = ip2[2] = ip2[3] = p[9];
  161. ip1[0] = ip1[1] = ip0[0] = ip0[1] = p[0];
  162. ip1[2] = ip1[3] = ip0[2] = ip0[3] = p[3];
  163. } else {
  164. p += 6;
  165. memcpy(ip3 + 0, p, 3); memcpy(ip3 + 3, p, 3);
  166. memcpy(ip2 + 0, p, 3); memcpy(ip2 + 3, p, 3);
  167. p += 3; /* ... + 9 */
  168. memcpy(ip3 + 6, p, 3); memcpy(ip3 + 9, p, 3);
  169. memcpy(ip2 + 6, p, 3); memcpy(ip2 + 9, p, 3);
  170. p -= 9; /* ... + 0 */
  171. memcpy(ip1 + 0, p, 3); memcpy(ip1 + 3, p, 3);
  172. memcpy(ip0 + 0, p, 3); memcpy(ip0 + 3, p, 3);
  173. p += 3; /* ... + 3 */
  174. memcpy(ip1 + 6, p, 3); memcpy(ip1 + 9, p, 3);
  175. memcpy(ip0 + 6, p, 3); memcpy(ip0 + 9, p, 3);
  176. }
  177. } else if (flag & mask) {
  178. if ((data + 4) > eod)
  179. return AVERROR_INVALIDDATA;
  180. cb0 = strip->v4_codebook[*data++];
  181. cb1 = strip->v4_codebook[*data++];
  182. cb2 = strip->v4_codebook[*data++];
  183. cb3 = strip->v4_codebook[*data++];
  184. if (s->palette_video) {
  185. uint8_t *p;
  186. p = ip3;
  187. *p++ = cb2[6];
  188. *p++ = cb2[9];
  189. *p++ = cb3[6];
  190. *p = cb3[9];
  191. p = ip2;
  192. *p++ = cb2[0];
  193. *p++ = cb2[3];
  194. *p++ = cb3[0];
  195. *p = cb3[3];
  196. p = ip1;
  197. *p++ = cb0[6];
  198. *p++ = cb0[9];
  199. *p++ = cb1[6];
  200. *p = cb1[9];
  201. p = ip0;
  202. *p++ = cb0[0];
  203. *p++ = cb0[3];
  204. *p++ = cb1[0];
  205. *p = cb1[3];
  206. } else {
  207. memcpy(ip3 + 0, cb2 + 6, 6);
  208. memcpy(ip3 + 6, cb3 + 6, 6);
  209. memcpy(ip2 + 0, cb2 + 0, 6);
  210. memcpy(ip2 + 6, cb3 + 0, 6);
  211. memcpy(ip1 + 0, cb0 + 6, 6);
  212. memcpy(ip1 + 6, cb1 + 6, 6);
  213. memcpy(ip0 + 0, cb0 + 0, 6);
  214. memcpy(ip0 + 6, cb1 + 0, 6);
  215. }
  216. }
  217. }
  218. if (s->palette_video) {
  219. ip0 += 4; ip1 += 4;
  220. ip2 += 4; ip3 += 4;
  221. } else {
  222. ip0 += 12; ip1 += 12;
  223. ip2 += 12; ip3 += 12;
  224. }
  225. }
  226. }
  227. return 0;
  228. }
  229. static int cinepak_decode_strip (CinepakContext *s,
  230. cvid_strip *strip, const uint8_t *data, int size)
  231. {
  232. const uint8_t *eod = (data + size);
  233. int chunk_id, chunk_size;
  234. /* coordinate sanity checks */
  235. if (strip->x2 > s->width ||
  236. strip->y2 > s->height ||
  237. strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
  238. return AVERROR_INVALIDDATA;
  239. while ((data + 4) <= eod) {
  240. chunk_id = data[0];
  241. chunk_size = AV_RB24 (&data[1]) - 4;
  242. if(chunk_size < 0)
  243. return AVERROR_INVALIDDATA;
  244. data += 4;
  245. chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
  246. switch (chunk_id) {
  247. case 0x20:
  248. case 0x21:
  249. case 0x24:
  250. case 0x25:
  251. cinepak_decode_codebook (strip->v4_codebook, chunk_id,
  252. chunk_size, data);
  253. break;
  254. case 0x22:
  255. case 0x23:
  256. case 0x26:
  257. case 0x27:
  258. cinepak_decode_codebook (strip->v1_codebook, chunk_id,
  259. chunk_size, data);
  260. break;
  261. case 0x30:
  262. case 0x31:
  263. case 0x32:
  264. return cinepak_decode_vectors (s, strip, chunk_id,
  265. chunk_size, data);
  266. }
  267. data += chunk_size;
  268. }
  269. return AVERROR_INVALIDDATA;
  270. }
  271. static int cinepak_decode (CinepakContext *s)
  272. {
  273. const uint8_t *eod = (s->data + s->size);
  274. int i, result, strip_size, frame_flags, num_strips;
  275. int y0 = 0;
  276. int encoded_buf_size;
  277. if (s->size < 10)
  278. return AVERROR_INVALIDDATA;
  279. frame_flags = s->data[0];
  280. num_strips = AV_RB16 (&s->data[8]);
  281. encoded_buf_size = AV_RB24(&s->data[1]);
  282. /* if this is the first frame, check for deviant Sega FILM data */
  283. if (s->sega_film_skip_bytes == -1) {
  284. if (!encoded_buf_size) {
  285. avpriv_request_sample(s->avctx, "encoded_buf_size 0");
  286. return AVERROR_PATCHWELCOME;
  287. }
  288. if (encoded_buf_size != s->size && (s->size % encoded_buf_size) != 0) {
  289. /* If the encoded frame size differs from the frame size as indicated
  290. * by the container file, this data likely comes from a Sega FILM/CPK file.
  291. * If the frame header is followed by the bytes FE 00 00 06 00 00 then
  292. * this is probably one of the two known files that have 6 extra bytes
  293. * after the frame header. Else, assume 2 extra bytes. The container
  294. * size also cannot be a multiple of the encoded size. */
  295. if (s->size >= 16 &&
  296. (s->data[10] == 0xFE) &&
  297. (s->data[11] == 0x00) &&
  298. (s->data[12] == 0x00) &&
  299. (s->data[13] == 0x06) &&
  300. (s->data[14] == 0x00) &&
  301. (s->data[15] == 0x00))
  302. s->sega_film_skip_bytes = 6;
  303. else
  304. s->sega_film_skip_bytes = 2;
  305. } else
  306. s->sega_film_skip_bytes = 0;
  307. }
  308. s->data += 10 + s->sega_film_skip_bytes;
  309. num_strips = FFMIN(num_strips, MAX_STRIPS);
  310. s->frame->key_frame = 0;
  311. for (i=0; i < num_strips; i++) {
  312. if ((s->data + 12) > eod)
  313. return AVERROR_INVALIDDATA;
  314. s->strips[i].id = s->data[0];
  315. /* zero y1 means "relative to the previous stripe" */
  316. if (!(s->strips[i].y1 = AV_RB16 (&s->data[4])))
  317. s->strips[i].y2 = (s->strips[i].y1 = y0) + AV_RB16 (&s->data[8]);
  318. else
  319. s->strips[i].y2 = AV_RB16 (&s->data[8]);
  320. s->strips[i].x1 = AV_RB16 (&s->data[6]);
  321. s->strips[i].x2 = AV_RB16 (&s->data[10]);
  322. if (s->strips[i].id == 0x10)
  323. s->frame->key_frame = 1;
  324. strip_size = AV_RB24 (&s->data[1]) - 12;
  325. if (strip_size < 0)
  326. return AVERROR_INVALIDDATA;
  327. s->data += 12;
  328. strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
  329. if ((i > 0) && !(frame_flags & 0x01)) {
  330. memcpy (s->strips[i].v4_codebook, s->strips[i-1].v4_codebook,
  331. sizeof(s->strips[i].v4_codebook));
  332. memcpy (s->strips[i].v1_codebook, s->strips[i-1].v1_codebook,
  333. sizeof(s->strips[i].v1_codebook));
  334. }
  335. result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
  336. if (result != 0)
  337. return result;
  338. s->data += strip_size;
  339. y0 = s->strips[i].y2;
  340. }
  341. return 0;
  342. }
  343. static av_cold int cinepak_decode_init(AVCodecContext *avctx)
  344. {
  345. CinepakContext *s = avctx->priv_data;
  346. s->avctx = avctx;
  347. s->width = (avctx->width + 3) & ~3;
  348. s->height = (avctx->height + 3) & ~3;
  349. s->sega_film_skip_bytes = -1; /* uninitialized state */
  350. // check for paletted data
  351. if (avctx->bits_per_coded_sample != 8) {
  352. s->palette_video = 0;
  353. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  354. } else {
  355. s->palette_video = 1;
  356. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  357. }
  358. s->frame = av_frame_alloc();
  359. if (!s->frame)
  360. return AVERROR(ENOMEM);
  361. return 0;
  362. }
  363. static int cinepak_decode_frame(AVCodecContext *avctx,
  364. void *data, int *got_frame,
  365. AVPacket *avpkt)
  366. {
  367. const uint8_t *buf = avpkt->data;
  368. int ret = 0, buf_size = avpkt->size;
  369. CinepakContext *s = avctx->priv_data;
  370. s->data = buf;
  371. s->size = buf_size;
  372. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  373. return ret;
  374. if (s->palette_video) {
  375. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  376. if (pal) {
  377. s->frame->palette_has_changed = 1;
  378. memcpy(s->pal, pal, AVPALETTE_SIZE);
  379. }
  380. }
  381. if ((ret = cinepak_decode(s)) < 0) {
  382. av_log(avctx, AV_LOG_ERROR, "cinepak_decode failed\n");
  383. }
  384. if (s->palette_video)
  385. memcpy (s->frame->data[1], s->pal, AVPALETTE_SIZE);
  386. if ((ret = av_frame_ref(data, s->frame)) < 0)
  387. return ret;
  388. *got_frame = 1;
  389. /* report that the buffer was completely consumed */
  390. return buf_size;
  391. }
  392. static av_cold int cinepak_decode_end(AVCodecContext *avctx)
  393. {
  394. CinepakContext *s = avctx->priv_data;
  395. av_frame_free(&s->frame);
  396. return 0;
  397. }
  398. AVCodec ff_cinepak_decoder = {
  399. .name = "cinepak",
  400. .type = AVMEDIA_TYPE_VIDEO,
  401. .id = AV_CODEC_ID_CINEPAK,
  402. .priv_data_size = sizeof(CinepakContext),
  403. .init = cinepak_decode_init,
  404. .close = cinepak_decode_end,
  405. .decode = cinepak_decode_frame,
  406. .capabilities = CODEC_CAP_DR1,
  407. .long_name = NULL_IF_CONFIG_SMALL("Cinepak"),
  408. };