filmstripdec.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Adobe Filmstrip demuxer
  3. * Copyright (c) 2010 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. * Adobe Filmstrip demuxer
  24. */
  25. #include "libavutil/intreadwrite.h"
  26. #include "avformat.h"
  27. #define RAND_TAG MKBETAG('R','a','n','d')
  28. typedef struct {
  29. int leading;
  30. } FilmstripDemuxContext;
  31. static int read_header(AVFormatContext *s,
  32. AVFormatParameters *ap)
  33. {
  34. FilmstripDemuxContext *film = s->priv_data;
  35. AVIOContext *pb = s->pb;
  36. AVStream *st;
  37. if (!s->pb->seekable)
  38. return AVERROR(EIO);
  39. avio_seek(pb, avio_size(pb) - 36, SEEK_SET);
  40. if (avio_rb32(pb) != RAND_TAG) {
  41. av_log(s, AV_LOG_ERROR, "magic number not found");
  42. return AVERROR_INVALIDDATA;
  43. }
  44. st = av_new_stream(s, 0);
  45. if (!st)
  46. return AVERROR(ENOMEM);
  47. st->nb_frames = avio_rb32(pb);
  48. if (avio_rb16(pb) != 0) {
  49. av_log_ask_for_sample(s, "unsupported packing method\n");
  50. return AVERROR_INVALIDDATA;
  51. }
  52. avio_skip(pb, 2);
  53. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  54. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  55. st->codec->pix_fmt = PIX_FMT_RGBA;
  56. st->codec->codec_tag = 0; /* no fourcc */
  57. st->codec->width = avio_rb16(pb);
  58. st->codec->height = avio_rb16(pb);
  59. film->leading = avio_rb16(pb);
  60. av_set_pts_info(st, 64, 1, avio_rb16(pb));
  61. avio_seek(pb, 0, SEEK_SET);
  62. return 0;
  63. }
  64. static int read_packet(AVFormatContext *s,
  65. AVPacket *pkt)
  66. {
  67. FilmstripDemuxContext *film = s->priv_data;
  68. AVStream *st = s->streams[0];
  69. if (url_feof(s->pb))
  70. return AVERROR(EIO);
  71. pkt->dts = avio_tell(s->pb) / (st->codec->width * (st->codec->height + film->leading) * 4);
  72. pkt->size = av_get_packet(s->pb, pkt, st->codec->width * st->codec->height * 4);
  73. avio_skip(s->pb, st->codec->width * film->leading * 4);
  74. if (pkt->size < 0)
  75. return pkt->size;
  76. pkt->flags |= AV_PKT_FLAG_KEY;
  77. return 0;
  78. }
  79. static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  80. {
  81. AVStream *st = s->streams[stream_index];
  82. avio_seek(s->pb, FFMAX(timestamp, 0) * st->codec->width * st->codec->height * 4, SEEK_SET);
  83. return 0;
  84. }
  85. AVInputFormat ff_filmstrip_demuxer = {
  86. .name = "filmstrip",
  87. .long_name = NULL_IF_CONFIG_SMALL("Adobe Filmstrip"),
  88. .priv_data_size = sizeof(FilmstripDemuxContext),
  89. .read_header = read_header,
  90. .read_packet = read_packet,
  91. .read_seek = read_seek,
  92. .extensions = "flm",
  93. };