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.

230 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 Page {
  29. int base_record;
  30. unsigned int nb_records;
  31. int size;
  32. } Page;
  33. typedef struct AnmDemuxContext {
  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. {
  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. avpriv_request_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES));
  77. return AVERROR_PATCHWELCOME;
  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 = avformat_new_stream(s, NULL);
  87. if (!st)
  88. return AVERROR(ENOMEM);
  89. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  90. st->codecpar->codec_id = AV_CODEC_ID_ANM;
  91. st->codecpar->codec_tag = 0; /* no fourcc */
  92. st->codecpar->width = avio_rl16(pb);
  93. st->codecpar->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. avpriv_set_pts_info(st, 64, 1, avio_rl16(pb));
  111. avio_skip(pb, 58);
  112. /* color cycling and palette data */
  113. st->codecpar->extradata_size = 16*8 + 4*256;
  114. st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  115. if (!st->codecpar->extradata) {
  116. return AVERROR(ENOMEM);
  117. }
  118. ret = avio_read(pb, st->codecpar->extradata, st->codecpar->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. }
  136. anm->record = -1;
  137. return 0;
  138. invalid:
  139. avpriv_request_sample(s, "Invalid header element");
  140. return AVERROR_PATCHWELCOME;
  141. }
  142. static int read_packet(AVFormatContext *s,
  143. AVPacket *pkt)
  144. {
  145. AnmDemuxContext *anm = s->priv_data;
  146. AVIOContext *pb = s->pb;
  147. Page *p;
  148. int tmp, record_size;
  149. if (s->pb->eof_reached)
  150. return AVERROR(EIO);
  151. if (anm->page < 0)
  152. return anm->page;
  153. repeat:
  154. p = &anm->pt[anm->page];
  155. /* parse page header */
  156. if (anm->record < 0) {
  157. avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
  158. avio_skip(pb, 8 + 2*p->nb_records);
  159. anm->record = 0;
  160. }
  161. /* if we have fetched all records in this page, then find the
  162. next page and repeat */
  163. if (anm->record >= p->nb_records) {
  164. anm->page = find_record(anm, p->base_record + p->nb_records);
  165. if (anm->page < 0)
  166. return anm->page;
  167. anm->record = -1;
  168. goto repeat;
  169. }
  170. /* fetch record size */
  171. tmp = avio_tell(pb);
  172. avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
  173. 8 + anm->record * 2, SEEK_SET);
  174. record_size = avio_rl16(pb);
  175. avio_seek(pb, tmp, SEEK_SET);
  176. /* fetch record */
  177. pkt->size = av_get_packet(s->pb, pkt, record_size);
  178. if (pkt->size < 0)
  179. return pkt->size;
  180. if (p->base_record + anm->record == 0)
  181. pkt->flags |= AV_PKT_FLAG_KEY;
  182. anm->record++;
  183. return 0;
  184. }
  185. AVInputFormat ff_anm_demuxer = {
  186. .name = "anm",
  187. .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
  188. .priv_data_size = sizeof(AnmDemuxContext),
  189. .read_probe = probe,
  190. .read_header = read_header,
  191. .read_packet = read_packet,
  192. };