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.

268 lines
7.9KB

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