paf.c 7.6 KB

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