smacker.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Smacker demuxer
  3. * Copyright (c) 2006 Konstantin Shishkov
  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. * Based on http://wiki.multimedia.cx/index.php?title=Smacker
  23. */
  24. #include "libavutil/bswap.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #define SMACKER_PAL 0x01
  29. #define SMACKER_FLAG_RING_FRAME 0x01
  30. enum SAudFlags {
  31. SMK_AUD_PACKED = 0x80,
  32. SMK_AUD_16BITS = 0x20,
  33. SMK_AUD_STEREO = 0x10,
  34. SMK_AUD_BINKAUD = 0x08,
  35. SMK_AUD_USEDCT = 0x04
  36. };
  37. typedef struct SmackerContext {
  38. /* Smacker file header */
  39. uint32_t magic;
  40. uint32_t width, height;
  41. uint32_t frames;
  42. int pts_inc;
  43. uint32_t flags;
  44. uint32_t audio[7];
  45. uint32_t treesize;
  46. uint32_t mmap_size, mclr_size, full_size, type_size;
  47. uint8_t aflags[7];
  48. uint32_t rates[7];
  49. uint32_t pad;
  50. /* frame info */
  51. uint32_t *frm_size;
  52. uint8_t *frm_flags;
  53. /* internal variables */
  54. int cur_frame;
  55. int is_ver4;
  56. int64_t cur_pts;
  57. /* current frame for demuxing */
  58. uint8_t pal[768];
  59. int indexes[7];
  60. int videoindex;
  61. uint8_t *bufs[7];
  62. int buf_sizes[7];
  63. int stream_id[7];
  64. int curstream;
  65. int64_t nextpos;
  66. int64_t aud_pts[7];
  67. } SmackerContext;
  68. typedef struct SmackerFrame {
  69. int64_t pts;
  70. int stream;
  71. } SmackerFrame;
  72. /* palette used in Smacker */
  73. static const uint8_t smk_pal[64] = {
  74. 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
  75. 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
  76. 0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
  77. 0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
  78. 0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
  79. 0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
  80. 0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
  81. 0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
  82. };
  83. static int smacker_probe(AVProbeData *p)
  84. {
  85. if(p->buf[0] == 'S' && p->buf[1] == 'M' && p->buf[2] == 'K'
  86. && (p->buf[3] == '2' || p->buf[3] == '4'))
  87. return AVPROBE_SCORE_MAX;
  88. else
  89. return 0;
  90. }
  91. static int smacker_read_header(AVFormatContext *s, AVFormatParameters *ap)
  92. {
  93. AVIOContext *pb = s->pb;
  94. SmackerContext *smk = s->priv_data;
  95. AVStream *st, *ast[7];
  96. int i, ret;
  97. int tbase;
  98. /* read and check header */
  99. smk->magic = avio_rl32(pb);
  100. if (smk->magic != MKTAG('S', 'M', 'K', '2') && smk->magic != MKTAG('S', 'M', 'K', '4'))
  101. return -1;
  102. smk->width = avio_rl32(pb);
  103. smk->height = avio_rl32(pb);
  104. smk->frames = avio_rl32(pb);
  105. smk->pts_inc = (int32_t)avio_rl32(pb);
  106. smk->flags = avio_rl32(pb);
  107. if(smk->flags & SMACKER_FLAG_RING_FRAME)
  108. smk->frames++;
  109. for(i = 0; i < 7; i++)
  110. smk->audio[i] = avio_rl32(pb);
  111. smk->treesize = avio_rl32(pb);
  112. if(smk->treesize >= UINT_MAX/4){ // smk->treesize + 16 must not overflow (this check is probably redundant)
  113. av_log(s, AV_LOG_ERROR, "treesize too large\n");
  114. return -1;
  115. }
  116. //FIXME remove extradata "rebuilding"
  117. smk->mmap_size = avio_rl32(pb);
  118. smk->mclr_size = avio_rl32(pb);
  119. smk->full_size = avio_rl32(pb);
  120. smk->type_size = avio_rl32(pb);
  121. for(i = 0; i < 7; i++) {
  122. smk->rates[i] = avio_rl24(pb);
  123. smk->aflags[i] = avio_r8(pb);
  124. }
  125. smk->pad = avio_rl32(pb);
  126. /* setup data */
  127. if(smk->frames > 0xFFFFFF) {
  128. av_log(s, AV_LOG_ERROR, "Too many frames: %i\n", smk->frames);
  129. return -1;
  130. }
  131. smk->frm_size = av_malloc(smk->frames * 4);
  132. smk->frm_flags = av_malloc(smk->frames);
  133. smk->is_ver4 = (smk->magic != MKTAG('S', 'M', 'K', '2'));
  134. /* read frame info */
  135. for(i = 0; i < smk->frames; i++) {
  136. smk->frm_size[i] = avio_rl32(pb);
  137. }
  138. for(i = 0; i < smk->frames; i++) {
  139. smk->frm_flags[i] = avio_r8(pb);
  140. }
  141. /* init video codec */
  142. st = avformat_new_stream(s, NULL);
  143. if (!st)
  144. return -1;
  145. smk->videoindex = st->index;
  146. st->codec->width = smk->width;
  147. st->codec->height = smk->height;
  148. st->codec->pix_fmt = PIX_FMT_PAL8;
  149. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  150. st->codec->codec_id = CODEC_ID_SMACKVIDEO;
  151. st->codec->codec_tag = smk->magic;
  152. /* Smacker uses 100000 as internal timebase */
  153. if(smk->pts_inc < 0)
  154. smk->pts_inc = -smk->pts_inc;
  155. else
  156. smk->pts_inc *= 100;
  157. tbase = 100000;
  158. av_reduce(&tbase, &smk->pts_inc, tbase, smk->pts_inc, (1UL<<31)-1);
  159. avpriv_set_pts_info(st, 33, smk->pts_inc, tbase);
  160. st->duration = smk->frames;
  161. /* handle possible audio streams */
  162. for(i = 0; i < 7; i++) {
  163. smk->indexes[i] = -1;
  164. if (smk->rates[i]) {
  165. ast[i] = avformat_new_stream(s, NULL);
  166. smk->indexes[i] = ast[i]->index;
  167. ast[i]->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  168. if (smk->aflags[i] & SMK_AUD_BINKAUD) {
  169. ast[i]->codec->codec_id = CODEC_ID_BINKAUDIO_RDFT;
  170. } else if (smk->aflags[i] & SMK_AUD_USEDCT) {
  171. ast[i]->codec->codec_id = CODEC_ID_BINKAUDIO_DCT;
  172. } else if (smk->aflags[i] & SMK_AUD_PACKED){
  173. ast[i]->codec->codec_id = CODEC_ID_SMACKAUDIO;
  174. ast[i]->codec->codec_tag = MKTAG('S', 'M', 'K', 'A');
  175. } else {
  176. ast[i]->codec->codec_id = CODEC_ID_PCM_U8;
  177. }
  178. ast[i]->codec->channels = (smk->aflags[i] & SMK_AUD_STEREO) ? 2 : 1;
  179. ast[i]->codec->sample_rate = smk->rates[i];
  180. ast[i]->codec->bits_per_coded_sample = (smk->aflags[i] & SMK_AUD_16BITS) ? 16 : 8;
  181. if(ast[i]->codec->bits_per_coded_sample == 16 && ast[i]->codec->codec_id == CODEC_ID_PCM_U8)
  182. ast[i]->codec->codec_id = CODEC_ID_PCM_S16LE;
  183. avpriv_set_pts_info(ast[i], 64, 1, ast[i]->codec->sample_rate
  184. * ast[i]->codec->channels * ast[i]->codec->bits_per_coded_sample / 8);
  185. }
  186. }
  187. /* load trees to extradata, they will be unpacked by decoder */
  188. st->codec->extradata = av_mallocz(smk->treesize + 16 +
  189. FF_INPUT_BUFFER_PADDING_SIZE);
  190. st->codec->extradata_size = smk->treesize + 16;
  191. if(!st->codec->extradata){
  192. av_log(s, AV_LOG_ERROR, "Cannot allocate %i bytes of extradata\n", smk->treesize + 16);
  193. av_free(smk->frm_size);
  194. av_free(smk->frm_flags);
  195. return -1;
  196. }
  197. ret = avio_read(pb, st->codec->extradata + 16, st->codec->extradata_size - 16);
  198. if(ret != st->codec->extradata_size - 16){
  199. av_free(smk->frm_size);
  200. av_free(smk->frm_flags);
  201. return AVERROR(EIO);
  202. }
  203. ((int32_t*)st->codec->extradata)[0] = av_le2ne32(smk->mmap_size);
  204. ((int32_t*)st->codec->extradata)[1] = av_le2ne32(smk->mclr_size);
  205. ((int32_t*)st->codec->extradata)[2] = av_le2ne32(smk->full_size);
  206. ((int32_t*)st->codec->extradata)[3] = av_le2ne32(smk->type_size);
  207. smk->curstream = -1;
  208. smk->nextpos = avio_tell(pb);
  209. return 0;
  210. }
  211. static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
  212. {
  213. SmackerContext *smk = s->priv_data;
  214. int flags;
  215. int ret;
  216. int i;
  217. int frame_size = 0;
  218. int palchange = 0;
  219. if (url_feof(s->pb) || smk->cur_frame >= smk->frames)
  220. return AVERROR_EOF;
  221. /* if we demuxed all streams, pass another frame */
  222. if(smk->curstream < 0) {
  223. avio_seek(s->pb, smk->nextpos, 0);
  224. frame_size = smk->frm_size[smk->cur_frame] & (~3);
  225. flags = smk->frm_flags[smk->cur_frame];
  226. /* handle palette change event */
  227. if(flags & SMACKER_PAL){
  228. int size, sz, t, off, j, pos;
  229. uint8_t *pal = smk->pal;
  230. uint8_t oldpal[768];
  231. memcpy(oldpal, pal, 768);
  232. size = avio_r8(s->pb);
  233. size = size * 4 - 1;
  234. if(size + 1 > frame_size)
  235. return AVERROR_INVALIDDATA;
  236. frame_size -= size;
  237. frame_size--;
  238. sz = 0;
  239. pos = avio_tell(s->pb) + size;
  240. while(sz < 256){
  241. t = avio_r8(s->pb);
  242. if(t & 0x80){ /* skip palette entries */
  243. sz += (t & 0x7F) + 1;
  244. pal += ((t & 0x7F) + 1) * 3;
  245. } else if(t & 0x40){ /* copy with offset */
  246. off = avio_r8(s->pb);
  247. j = (t & 0x3F) + 1;
  248. if (off + j - 1 > 0xff) {
  249. av_log(s, AV_LOG_ERROR,
  250. "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
  251. off, j);
  252. return AVERROR_INVALIDDATA;
  253. }
  254. off *= 3;
  255. while(j-- && sz < 256) {
  256. *pal++ = oldpal[off + 0];
  257. *pal++ = oldpal[off + 1];
  258. *pal++ = oldpal[off + 2];
  259. sz++;
  260. off += 3;
  261. }
  262. } else { /* new entries */
  263. *pal++ = smk_pal[t];
  264. *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
  265. *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
  266. sz++;
  267. }
  268. }
  269. avio_seek(s->pb, pos, 0);
  270. palchange |= 1;
  271. }
  272. flags >>= 1;
  273. smk->curstream = -1;
  274. /* if audio chunks are present, put them to stack and retrieve later */
  275. for(i = 0; i < 7; i++) {
  276. if(flags & 1) {
  277. uint32_t size;
  278. uint8_t *tmpbuf;
  279. size = avio_rl32(s->pb) - 4;
  280. if (!size || size + 4LL > frame_size) {
  281. av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
  282. return AVERROR_INVALIDDATA;
  283. }
  284. frame_size -= size;
  285. frame_size -= 4;
  286. smk->curstream++;
  287. tmpbuf = av_realloc(smk->bufs[smk->curstream], size);
  288. if (!tmpbuf)
  289. return AVERROR(ENOMEM);
  290. smk->bufs[smk->curstream] = tmpbuf;
  291. smk->buf_sizes[smk->curstream] = size;
  292. ret = avio_read(s->pb, smk->bufs[smk->curstream], size);
  293. if(ret != size)
  294. return AVERROR(EIO);
  295. smk->stream_id[smk->curstream] = smk->indexes[i];
  296. }
  297. flags >>= 1;
  298. }
  299. if (frame_size < 0 || frame_size >= INT_MAX/2)
  300. return AVERROR_INVALIDDATA;
  301. if (av_new_packet(pkt, frame_size + 769))
  302. return AVERROR(ENOMEM);
  303. if(smk->frm_size[smk->cur_frame] & 1)
  304. palchange |= 2;
  305. pkt->data[0] = palchange;
  306. memcpy(pkt->data + 1, smk->pal, 768);
  307. ret = avio_read(s->pb, pkt->data + 769, frame_size);
  308. if(ret != frame_size)
  309. return AVERROR(EIO);
  310. pkt->stream_index = smk->videoindex;
  311. pkt->size = ret + 769;
  312. smk->cur_frame++;
  313. smk->nextpos = avio_tell(s->pb);
  314. } else {
  315. if (smk->stream_id[smk->curstream] < 0)
  316. return AVERROR_INVALIDDATA;
  317. if (av_new_packet(pkt, smk->buf_sizes[smk->curstream]))
  318. return AVERROR(ENOMEM);
  319. memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]);
  320. pkt->size = smk->buf_sizes[smk->curstream];
  321. pkt->stream_index = smk->stream_id[smk->curstream];
  322. pkt->pts = smk->aud_pts[smk->curstream];
  323. smk->aud_pts[smk->curstream] += AV_RL32(pkt->data);
  324. smk->curstream--;
  325. }
  326. return 0;
  327. }
  328. static int smacker_read_close(AVFormatContext *s)
  329. {
  330. SmackerContext *smk = s->priv_data;
  331. int i;
  332. for(i = 0; i < 7; i++)
  333. av_free(smk->bufs[i]);
  334. av_free(smk->frm_size);
  335. av_free(smk->frm_flags);
  336. return 0;
  337. }
  338. AVInputFormat ff_smacker_demuxer = {
  339. .name = "smk",
  340. .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
  341. .priv_data_size = sizeof(SmackerContext),
  342. .read_probe = smacker_probe,
  343. .read_header = smacker_read_header,
  344. .read_packet = smacker_read_packet,
  345. .read_close = smacker_read_close,
  346. };