anm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Deluxe Paint Animation demuxer
  3. * Copyright (c) 2009 Peter Ross
  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. /**
  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. ret = AVERROR(ENOMEM);
  117. goto close_and_return;
  118. }
  119. ret = avio_read(pb, st->codec->extradata, st->codec->extradata_size);
  120. if (ret < 0)
  121. goto close_and_return;
  122. /* read page table */
  123. ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
  124. if (ret < 0)
  125. goto close_and_return;
  126. for (i = 0; i < MAX_PAGES; i++) {
  127. Page *p = &anm->pt[i];
  128. p->base_record = avio_rl16(pb);
  129. p->nb_records = avio_rl16(pb);
  130. p->size = avio_rl16(pb);
  131. }
  132. /* find page of first frame */
  133. anm->page = find_record(anm, 0);
  134. if (anm->page < 0) {
  135. ret = anm->page;
  136. goto close_and_return;
  137. }
  138. anm->record = -1;
  139. return 0;
  140. invalid:
  141. av_log_ask_for_sample(s, NULL);
  142. ret = AVERROR_INVALIDDATA;
  143. close_and_return:
  144. av_close_input_stream(s);
  145. return ret;
  146. }
  147. static int read_packet(AVFormatContext *s,
  148. AVPacket *pkt)
  149. {
  150. AnmDemuxContext *anm = s->priv_data;
  151. AVIOContext *pb = s->pb;
  152. Page *p;
  153. int tmp, record_size;
  154. if (s->pb->eof_reached)
  155. return AVERROR(EIO);
  156. if (anm->page < 0)
  157. return anm->page;
  158. repeat:
  159. p = &anm->pt[anm->page];
  160. /* parse page header */
  161. if (anm->record < 0) {
  162. avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
  163. avio_skip(pb, 8 + 2*p->nb_records);
  164. anm->record = 0;
  165. }
  166. /* if we have fetched all records in this page, then find the
  167. next page and repeat */
  168. if (anm->record >= p->nb_records) {
  169. anm->page = find_record(anm, p->base_record + p->nb_records);
  170. if (anm->page < 0)
  171. return anm->page;
  172. anm->record = -1;
  173. goto repeat;
  174. }
  175. /* fetch record size */
  176. tmp = avio_tell(pb);
  177. avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
  178. 8 + anm->record * 2, SEEK_SET);
  179. record_size = avio_rl16(pb);
  180. avio_seek(pb, tmp, SEEK_SET);
  181. /* fetch record */
  182. pkt->size = av_get_packet(s->pb, pkt, record_size);
  183. if (pkt->size < 0)
  184. return pkt->size;
  185. if (p->base_record + anm->record == 0)
  186. pkt->flags |= AV_PKT_FLAG_KEY;
  187. anm->record++;
  188. return 0;
  189. }
  190. AVInputFormat ff_anm_demuxer = {
  191. "anm",
  192. NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
  193. sizeof(AnmDemuxContext),
  194. probe,
  195. read_header,
  196. read_packet,
  197. };