anm.c 6.3 KB

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