filmstripdec.c 3.3 KB

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