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.

269 lines
7.9KB

  1. /*
  2. * Packed Animation File demuxer
  3. * Copyright (c) 2012 Paul B Mahol
  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. #include "libavutil/channel_layout.h"
  22. #include "libavcodec/paf.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #define MAGIC "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a"
  26. typedef struct {
  27. uint32_t buffer_size;
  28. uint32_t frame_blks;
  29. uint32_t nb_frames;
  30. uint32_t start_offset;
  31. uint32_t preload_count;
  32. uint32_t max_video_blks;
  33. uint32_t max_audio_blks;
  34. uint32_t current_frame;
  35. uint32_t current_frame_count;
  36. uint32_t current_frame_block;
  37. uint32_t *blocks_count_table;
  38. uint32_t *frames_offset_table;
  39. uint32_t *blocks_offset_table;
  40. uint8_t *video_frame;
  41. int video_size;
  42. uint8_t *audio_frame;
  43. uint8_t *temp_audio_frame;
  44. int audio_size;
  45. int got_audio;
  46. } PAFDemuxContext;
  47. static int read_probe(AVProbeData *p)
  48. {
  49. if ((p->buf_size >= strlen(MAGIC)) &&
  50. !memcmp(p->buf, MAGIC, strlen(MAGIC)))
  51. return AVPROBE_SCORE_MAX;
  52. return 0;
  53. }
  54. static int read_close(AVFormatContext *s)
  55. {
  56. PAFDemuxContext *p = s->priv_data;
  57. av_freep(&p->blocks_count_table);
  58. av_freep(&p->frames_offset_table);
  59. av_freep(&p->blocks_offset_table);
  60. av_freep(&p->video_frame);
  61. av_freep(&p->audio_frame);
  62. av_freep(&p->temp_audio_frame);
  63. return 0;
  64. }
  65. static void read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
  66. {
  67. int i;
  68. for (i = 0; i < count; i++)
  69. table[i] = avio_rl32(s->pb);
  70. avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
  71. }
  72. static int read_header(AVFormatContext *s)
  73. {
  74. PAFDemuxContext *p = s->priv_data;
  75. AVIOContext *pb = s->pb;
  76. AVStream *ast, *vst;
  77. int ret = 0;
  78. avio_skip(pb, 132);
  79. vst = avformat_new_stream(s, 0);
  80. if (!vst)
  81. return AVERROR(ENOMEM);
  82. vst->start_time = 0;
  83. vst->nb_frames =
  84. vst->duration =
  85. p->nb_frames = avio_rl32(pb);
  86. avio_skip(pb, 4);
  87. vst->codec->width = avio_rl32(pb);
  88. vst->codec->height = avio_rl32(pb);
  89. avio_skip(pb, 4);
  90. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  91. vst->codec->codec_tag = 0;
  92. vst->codec->codec_id = AV_CODEC_ID_PAF_VIDEO;
  93. avpriv_set_pts_info(vst, 64, 1, 10);
  94. ast = avformat_new_stream(s, 0);
  95. if (!ast)
  96. return AVERROR(ENOMEM);
  97. ast->start_time = 0;
  98. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  99. ast->codec->codec_tag = 0;
  100. ast->codec->codec_id = AV_CODEC_ID_PAF_AUDIO;
  101. ast->codec->channels = 2;
  102. ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  103. ast->codec->sample_rate = 22050;
  104. avpriv_set_pts_info(ast, 64, 1, 22050);
  105. p->buffer_size = avio_rl32(pb);
  106. p->preload_count = avio_rl32(pb);
  107. p->frame_blks = avio_rl32(pb);
  108. p->start_offset = avio_rl32(pb);
  109. p->max_video_blks = avio_rl32(pb);
  110. p->max_audio_blks = avio_rl32(pb);
  111. if (p->buffer_size < 175 ||
  112. p->max_audio_blks < 2 ||
  113. p->max_video_blks < 1 ||
  114. p->frame_blks < 1 ||
  115. p->nb_frames < 1 ||
  116. p->preload_count < 1 ||
  117. p->buffer_size > 2048 ||
  118. p->max_video_blks > 2048 ||
  119. p->max_audio_blks > 2048 ||
  120. p->nb_frames > INT_MAX / sizeof(uint32_t) ||
  121. p->frame_blks > INT_MAX / sizeof(uint32_t))
  122. return AVERROR_INVALIDDATA;
  123. p->blocks_count_table = av_mallocz(p->nb_frames *
  124. sizeof(*p->blocks_count_table));
  125. p->frames_offset_table = av_mallocz(p->nb_frames *
  126. sizeof(*p->frames_offset_table));
  127. p->blocks_offset_table = av_mallocz(p->frame_blks *
  128. sizeof(*p->blocks_offset_table));
  129. p->video_size = p->max_video_blks * p->buffer_size;
  130. p->video_frame = av_mallocz(p->video_size);
  131. p->audio_size = p->max_audio_blks * p->buffer_size;
  132. p->audio_frame = av_mallocz(p->audio_size);
  133. p->temp_audio_frame = av_mallocz(p->audio_size);
  134. if (!p->blocks_count_table ||
  135. !p->frames_offset_table ||
  136. !p->blocks_offset_table ||
  137. !p->video_frame ||
  138. !p->audio_frame ||
  139. !p->temp_audio_frame) {
  140. ret = AVERROR(ENOMEM);
  141. goto fail;
  142. }
  143. avio_seek(pb, p->buffer_size, SEEK_SET);
  144. read_table(s, p->blocks_count_table, p->nb_frames);
  145. read_table(s, p->frames_offset_table, p->nb_frames);
  146. read_table(s, p->blocks_offset_table, p->frame_blks);
  147. p->got_audio = 0;
  148. p->current_frame = 0;
  149. p->current_frame_block = 0;
  150. avio_seek(pb, p->start_offset, SEEK_SET);
  151. return 0;
  152. fail:
  153. read_close(s);
  154. return ret;
  155. }
  156. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  157. {
  158. PAFDemuxContext *p = s->priv_data;
  159. AVIOContext *pb = s->pb;
  160. uint32_t count, offset;
  161. int size, i;
  162. if (p->current_frame >= p->nb_frames)
  163. return AVERROR_EOF;
  164. if (url_feof(pb))
  165. return AVERROR_EOF;
  166. if (p->got_audio) {
  167. if (av_new_packet(pkt, p->audio_size) < 0)
  168. return AVERROR(ENOMEM);
  169. memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
  170. pkt->duration = PAF_SOUND_SAMPLES * (p->audio_size / PAF_SOUND_FRAME_SIZE);
  171. pkt->flags |= AV_PKT_FLAG_KEY;
  172. pkt->stream_index = 1;
  173. p->got_audio = 0;
  174. return pkt->size;
  175. }
  176. count = (p->current_frame == 0) ? p->preload_count
  177. : p->blocks_count_table[p->current_frame - 1];
  178. for (i = 0; i < count; i++) {
  179. if (p->current_frame_block >= p->frame_blks)
  180. return AVERROR_INVALIDDATA;
  181. offset = p->blocks_offset_table[p->current_frame_block] & ~(1U << 31);
  182. if (p->blocks_offset_table[p->current_frame_block] & (1U << 31)) {
  183. if (offset > p->audio_size - p->buffer_size)
  184. return AVERROR_INVALIDDATA;
  185. avio_read(pb, p->audio_frame + offset, p->buffer_size);
  186. if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
  187. memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
  188. p->got_audio = 1;
  189. }
  190. } else {
  191. if (offset > p->video_size - p->buffer_size)
  192. return AVERROR_INVALIDDATA;
  193. avio_read(pb, p->video_frame + offset, p->buffer_size);
  194. }
  195. p->current_frame_block++;
  196. }
  197. if (p->frames_offset_table[p->current_frame] >= p->video_size)
  198. return AVERROR_INVALIDDATA;
  199. size = p->video_size - p->frames_offset_table[p->current_frame];
  200. if (av_new_packet(pkt, size) < 0)
  201. return AVERROR(ENOMEM);
  202. pkt->stream_index = 0;
  203. pkt->duration = 1;
  204. memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
  205. if (pkt->data[0] & 0x20)
  206. pkt->flags |= AV_PKT_FLAG_KEY;
  207. p->current_frame++;
  208. return pkt->size;
  209. }
  210. AVInputFormat ff_paf_demuxer = {
  211. .name = "paf",
  212. .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
  213. .priv_data_size = sizeof(PAFDemuxContext),
  214. .read_probe = read_probe,
  215. .read_header = read_header,
  216. .read_packet = read_packet,
  217. .read_close = read_close,
  218. };