anm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Deluxe Paint Animation demuxer
  3. * Copyright (c) 2009 Peter Ross
  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. * @file
  23. * Deluxe Paint Animation demuxer
  24. */
  25. #include "libavutil/intreadwrite.h"
  26. #include "avformat.h"
  27. typedef struct {
  28. int base_record;
  29. unsigned int nb_records;
  30. int size;
  31. } Page;
  32. typedef struct {
  33. unsigned int nb_pages; /**< total pages in file */
  34. unsigned int nb_records; /**< total records in file */
  35. int page_table_offset;
  36. #define MAX_PAGES 256 /**< Deluxe Paint hardcoded value */
  37. Page pt[MAX_PAGES]; /**< page table */
  38. int page; /**< current page (or AVERROR_xxx code) */
  39. int record; /**< current record (with in page) */
  40. } AnmDemuxContext;
  41. #define LPF_TAG MKTAG('L','P','F',' ')
  42. #define ANIM_TAG MKTAG('A','N','I','M')
  43. static int probe(AVProbeData *p)
  44. {
  45. /* verify tags and video dimensions */
  46. if (AV_RL32(&p->buf[0]) == LPF_TAG &&
  47. AV_RL32(&p->buf[16]) == ANIM_TAG &&
  48. AV_RL16(&p->buf[20]) && AV_RL16(&p->buf[22]))
  49. return AVPROBE_SCORE_MAX;
  50. return 0;
  51. }
  52. /**
  53. * @return page containing the requested record or AVERROR_XXX
  54. */
  55. static int find_record(const AnmDemuxContext *anm, int record)
  56. {
  57. int i;
  58. if (record >= anm->nb_records)
  59. return AVERROR_EOF;
  60. for (i = 0; i < MAX_PAGES; i++) {
  61. const Page *p = &anm->pt[i];
  62. if (p->nb_records > 0 && record >= p->base_record && record < p->base_record + p->nb_records)
  63. return i;
  64. }
  65. return AVERROR_INVALIDDATA;
  66. }
  67. static int read_header(AVFormatContext *s,
  68. AVFormatParameters *ap)
  69. {
  70. AnmDemuxContext *anm = s->priv_data;
  71. AVIOContext *pb = s->pb;
  72. AVStream *st;
  73. int i, ret;
  74. avio_skip(pb, 4); /* magic number */
  75. if (avio_rl16(pb) != MAX_PAGES) {
  76. av_log_ask_for_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES) "\n");
  77. return AVERROR_INVALIDDATA;
  78. }
  79. anm->nb_pages = avio_rl16(pb);
  80. anm->nb_records = avio_rl32(pb);
  81. avio_skip(pb, 2); /* max records per page */
  82. anm->page_table_offset = avio_rl16(pb);
  83. if (avio_rl32(pb) != ANIM_TAG)
  84. return AVERROR_INVALIDDATA;
  85. /* video stream */
  86. st = av_new_stream(s, 0);
  87. if (!st)
  88. return AVERROR(ENOMEM);
  89. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  90. st->codec->codec_id = CODEC_ID_ANM;
  91. st->codec->codec_tag = 0; /* no fourcc */
  92. st->codec->width = avio_rl16(pb);
  93. st->codec->height = avio_rl16(pb);
  94. if (avio_r8(pb) != 0)
  95. goto invalid;
  96. avio_skip(pb, 1); /* frame rate multiplier info */
  97. /* ignore last delta record (used for looping) */
  98. if (avio_r8(pb)) /* has_last_delta */
  99. anm->nb_records = FFMAX(anm->nb_records - 1, 0);
  100. avio_skip(pb, 1); /* last_delta_valid */
  101. if (avio_r8(pb) != 0)
  102. goto invalid;
  103. if (avio_r8(pb) != 1)
  104. goto invalid;
  105. avio_skip(pb, 1); /* other recs per frame */
  106. if (avio_r8(pb) != 1)
  107. goto invalid;
  108. avio_skip(pb, 32); /* record_types */
  109. st->nb_frames = avio_rl32(pb);
  110. av_set_pts_info(st, 64, 1, avio_rl16(pb));
  111. avio_skip(pb, 58);
  112. /* color cycling and palette data */
  113. st->codec->extradata_size = 16*8 + 4*256;
  114. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  115. if (!st->codec->extradata)
  116. return AVERROR(ENOMEM);
  117. ret = avio_read(pb, st->codec->extradata, st->codec->extradata_size);
  118. if (ret < 0)
  119. return ret;
  120. /* read page table */
  121. ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
  122. if (ret < 0)
  123. return ret;
  124. for (i = 0; i < MAX_PAGES; i++) {
  125. Page *p = &anm->pt[i];
  126. p->base_record = avio_rl16(pb);
  127. p->nb_records = avio_rl16(pb);
  128. p->size = avio_rl16(pb);
  129. }
  130. /* find page of first frame */
  131. anm->page = find_record(anm, 0);
  132. if (anm->page < 0)
  133. return anm->page;
  134. anm->record = -1;
  135. return 0;
  136. invalid:
  137. av_log_ask_for_sample(s, NULL);
  138. return AVERROR_INVALIDDATA;
  139. }
  140. static int read_packet(AVFormatContext *s,
  141. AVPacket *pkt)
  142. {
  143. AnmDemuxContext *anm = s->priv_data;
  144. AVIOContext *pb = s->pb;
  145. Page *p;
  146. int tmp, record_size;
  147. if (url_feof(s->pb))
  148. return AVERROR(EIO);
  149. if (anm->page < 0)
  150. return anm->page;
  151. repeat:
  152. p = &anm->pt[anm->page];
  153. /* parse page header */
  154. if (anm->record < 0) {
  155. avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
  156. avio_skip(pb, 8 + 2*p->nb_records);
  157. anm->record = 0;
  158. }
  159. /* if we have fetched all records in this page, then find the
  160. next page and repeat */
  161. if (anm->record >= p->nb_records) {
  162. anm->page = find_record(anm, p->base_record + p->nb_records);
  163. if (anm->page < 0)
  164. return anm->page;
  165. anm->record = -1;
  166. goto repeat;
  167. }
  168. /* fetch record size */
  169. tmp = avio_tell(pb);
  170. avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
  171. 8 + anm->record * 2, SEEK_SET);
  172. record_size = avio_rl16(pb);
  173. avio_seek(pb, tmp, SEEK_SET);
  174. /* fetch record */
  175. pkt->size = av_get_packet(s->pb, pkt, record_size);
  176. if (pkt->size < 0)
  177. return pkt->size;
  178. if (p->base_record + anm->record == 0)
  179. pkt->flags |= AV_PKT_FLAG_KEY;
  180. anm->record++;
  181. return 0;
  182. }
  183. AVInputFormat ff_anm_demuxer = {
  184. .name = "anm",
  185. .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
  186. .priv_data_size = sizeof(AnmDemuxContext),
  187. .read_probe = probe,
  188. .read_header = read_header,
  189. .read_packet = read_packet,
  190. };