/libavformat/vc1test.c

http://github.com/FFmpeg/FFmpeg · C · 127 lines · 81 code · 19 blank · 27 comment · 21 complexity · 9cd38297ddee22f1ae2a64b727531bbe MD5 · raw file

  1. /*
  2. * VC1 Test Bitstreams Format Demuxer
  3. * Copyright (c) 2006, 2008 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. /**
  22. * @file
  23. * VC1 test bitstream file demuxer
  24. * by Konstantin Shishkov
  25. * Format specified in SMPTE standard 421 Annex L
  26. */
  27. #include "libavutil/intreadwrite.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #define VC1_EXTRADATA_SIZE 4
  31. static int vc1t_probe(const AVProbeData *p)
  32. {
  33. uint32_t size;
  34. if (p->buf_size < 24)
  35. return 0;
  36. size = AV_RL32(&p->buf[4]);
  37. if (p->buf[3] != 0xC5 || size < 4 || size > p->buf_size - 20 ||
  38. AV_RL32(&p->buf[size+16]) != 0xC)
  39. return 0;
  40. return AVPROBE_SCORE_EXTENSION;
  41. }
  42. static int vc1t_read_header(AVFormatContext *s)
  43. {
  44. AVIOContext *pb = s->pb;
  45. AVStream *st;
  46. int frames, ret;
  47. uint32_t fps;
  48. uint32_t size;
  49. frames = avio_rl24(pb);
  50. if (avio_r8(pb) != 0xC5 || ((size = avio_rl32(pb)) < 4))
  51. return AVERROR_INVALIDDATA;
  52. /* init video codec */
  53. st = avformat_new_stream(s, NULL);
  54. if (!st)
  55. return AVERROR(ENOMEM);
  56. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  57. st->codecpar->codec_id = AV_CODEC_ID_WMV3;
  58. if ((ret = ff_get_extradata(s, st->codecpar, pb, VC1_EXTRADATA_SIZE)) < 0)
  59. return ret;
  60. avio_skip(pb, size - 4);
  61. st->codecpar->height = avio_rl32(pb);
  62. st->codecpar->width = avio_rl32(pb);
  63. if(avio_rl32(pb) != 0xC)
  64. return AVERROR_INVALIDDATA;
  65. avio_skip(pb, 8);
  66. fps = avio_rl32(pb);
  67. if(fps == 0xFFFFFFFF)
  68. avpriv_set_pts_info(st, 32, 1, 1000);
  69. else{
  70. if (!fps) {
  71. av_log(s, AV_LOG_ERROR, "Zero FPS specified, defaulting to 1 FPS\n");
  72. fps = 1;
  73. }
  74. avpriv_set_pts_info(st, 24, 1, fps);
  75. st->duration = frames;
  76. }
  77. return 0;
  78. }
  79. static int vc1t_read_packet(AVFormatContext *s,
  80. AVPacket *pkt)
  81. {
  82. AVIOContext *pb = s->pb;
  83. int frame_size;
  84. int keyframe = 0;
  85. uint32_t pts;
  86. if(avio_feof(pb))
  87. return AVERROR(EIO);
  88. frame_size = avio_rl24(pb);
  89. if(avio_r8(pb) & 0x80)
  90. keyframe = 1;
  91. pts = avio_rl32(pb);
  92. if(av_get_packet(pb, pkt, frame_size) < 0)
  93. return AVERROR(EIO);
  94. if(s->streams[0]->time_base.den == 1000)
  95. pkt->pts = pts;
  96. pkt->flags |= keyframe ? AV_PKT_FLAG_KEY : 0;
  97. pkt->pos -= 8;
  98. return pkt->size;
  99. }
  100. AVInputFormat ff_vc1t_demuxer = {
  101. .name = "vc1test",
  102. .long_name = NULL_IF_CONFIG_SMALL("VC-1 test bitstream"),
  103. .read_probe = vc1t_probe,
  104. .read_header = vc1t_read_header,
  105. .read_packet = vc1t_read_packet,
  106. .extensions = "rcv",
  107. .flags = AVFMT_GENERIC_INDEX,
  108. };