dfa.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Chronomaster DFA Format Demuxer
  3. * Copyright (c) 2011 Konstantin Shishkov
  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. #include "libavutil/intreadwrite.h"
  22. #include "avformat.h"
  23. static int dfa_probe(AVProbeData *p)
  24. {
  25. if (p->buf_size < 4 || AV_RL32(p->buf) != MKTAG('D', 'F', 'I', 'A'))
  26. return 0;
  27. return AVPROBE_SCORE_MAX;
  28. }
  29. static int dfa_read_header(AVFormatContext *s,
  30. AVFormatParameters *ap)
  31. {
  32. AVIOContext *pb = s->pb;
  33. AVStream *st;
  34. int frames;
  35. uint32_t mspf;
  36. if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) {
  37. av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n");
  38. return AVERROR_INVALIDDATA;
  39. }
  40. avio_skip(pb, 2); // unused
  41. frames = avio_rl16(pb);
  42. st = av_new_stream(s, 0);
  43. if (!st)
  44. return AVERROR(ENOMEM);
  45. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  46. st->codec->codec_id = CODEC_ID_DFA;
  47. st->codec->width = avio_rl16(pb);
  48. st->codec->height = avio_rl16(pb);
  49. mspf = avio_rl32(pb);
  50. if (!mspf) {
  51. av_log(s, AV_LOG_WARNING, "Zero FPS reported, defaulting to 10\n");
  52. mspf = 100;
  53. }
  54. av_set_pts_info(st, 24, mspf, 1000);
  55. avio_skip(pb, 128 - 16); // padding
  56. st->duration = frames;
  57. return 0;
  58. }
  59. static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
  60. {
  61. AVIOContext *pb = s->pb;
  62. uint32_t frame_size;
  63. int ret, first = 1;
  64. if (pb->eof_reached)
  65. return AVERROR_EOF;
  66. if (av_get_packet(pb, pkt, 12) != 12)
  67. return AVERROR(EIO);
  68. while (!pb->eof_reached) {
  69. if (!first) {
  70. ret = av_append_packet(pb, pkt, 12);
  71. if (ret < 0) {
  72. av_free_packet(pkt);
  73. return ret;
  74. }
  75. } else
  76. first = 0;
  77. frame_size = AV_RL32(pkt->data + pkt->size - 8);
  78. if (frame_size > INT_MAX - 4) {
  79. av_log(s, AV_LOG_ERROR, "Too large chunk size: %d\n", frame_size);
  80. return AVERROR(EIO);
  81. }
  82. if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
  83. if (frame_size) {
  84. av_log(s, AV_LOG_WARNING, "skipping %d bytes of end-of-frame marker chunk\n",
  85. frame_size);
  86. avio_skip(pb, frame_size);
  87. }
  88. return 0;
  89. }
  90. ret = av_append_packet(pb, pkt, frame_size);
  91. if (ret < 0) {
  92. av_free_packet(pkt);
  93. return ret;
  94. }
  95. }
  96. return 0;
  97. }
  98. AVInputFormat ff_dfa_demuxer = {
  99. .name = "dfa",
  100. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  101. .read_probe = dfa_probe,
  102. .read_header = dfa_read_header,
  103. .read_packet = dfa_read_packet,
  104. .flags = AVFMT_GENERIC_INDEX,
  105. };