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.

408 lines
14KB

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