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.

236 lines
6.4KB

  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. #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. ret = AVERROR(ENOMEM);
  118. goto fail;
  119. }
  120. ret = avio_read(pb, st->codec->extradata, st->codec->extradata_size);
  121. if (ret < 0)
  122. goto fail;
  123. /* read page table */
  124. ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
  125. if (ret < 0)
  126. goto fail;
  127. for (i = 0; i < MAX_PAGES; i++) {
  128. Page *p = &anm->pt[i];
  129. p->base_record = avio_rl16(pb);
  130. p->nb_records = avio_rl16(pb);
  131. p->size = avio_rl16(pb);
  132. }
  133. /* find page of first frame */
  134. anm->page = find_record(anm, 0);
  135. if (anm->page < 0) {
  136. ret = anm->page;
  137. goto fail;
  138. }
  139. anm->record = -1;
  140. return 0;
  141. invalid:
  142. av_log_ask_for_sample(s, NULL);
  143. ret = AVERROR_INVALIDDATA;
  144. fail:
  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. .name = "anm",
  192. .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
  193. .priv_data_size = sizeof(AnmDemuxContext),
  194. .read_probe = probe,
  195. .read_header = read_header,
  196. .read_packet = read_packet,
  197. };