Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * LVF demuxer
  3.  * Copyright (c) 2012 Paul B Mahol
  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. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "riff.h"
  25.  
  26. static int lvf_probe(AVProbeData *p)
  27. {
  28.     if (AV_RL32(p->buf) == MKTAG('L', 'V', 'F', 'F'))
  29.         return AVPROBE_SCORE_EXTENSION;
  30.     return 0;
  31. }
  32.  
  33. static int lvf_read_header(AVFormatContext *s)
  34. {
  35.     AVStream *st;
  36.     int64_t next_offset;
  37.     unsigned size, nb_streams, id;
  38.  
  39.     avio_skip(s->pb, 16);
  40.     nb_streams = avio_rl32(s->pb);
  41.     if (!nb_streams)
  42.         return AVERROR_INVALIDDATA;
  43.     if (nb_streams > 2) {
  44.         avpriv_request_sample(s, "%d streams", nb_streams);
  45.         return AVERROR_PATCHWELCOME;
  46.     }
  47.  
  48.     avio_skip(s->pb, 1012);
  49.  
  50.     while (!url_feof(s->pb)) {
  51.         id          = avio_rl32(s->pb);
  52.         size        = avio_rl32(s->pb);
  53.         next_offset = avio_tell(s->pb) + size;
  54.  
  55.         switch (id) {
  56.         case MKTAG('0', '0', 'f', 'm'):
  57.             st = avformat_new_stream(s, 0);
  58.             if (!st)
  59.                 return AVERROR(ENOMEM);
  60.  
  61.             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  62.             avio_skip(s->pb, 4);
  63.             st->codec->width      = avio_rl32(s->pb);
  64.             st->codec->height     = avio_rl32(s->pb);
  65.             avio_skip(s->pb, 4);
  66.             st->codec->codec_tag  = avio_rl32(s->pb);
  67.             st->codec->codec_id   = ff_codec_get_id(ff_codec_bmp_tags,
  68.                                                     st->codec->codec_tag);
  69.             avpriv_set_pts_info(st, 32, 1, 1000);
  70.             break;
  71.         case MKTAG('0', '1', 'f', 'm'):
  72.             st = avformat_new_stream(s, 0);
  73.             if (!st)
  74.                 return AVERROR(ENOMEM);
  75.  
  76.             st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
  77.             st->codec->codec_tag   = avio_rl16(s->pb);
  78.             st->codec->channels    = avio_rl16(s->pb);
  79.             st->codec->sample_rate = avio_rl16(s->pb);
  80.             avio_skip(s->pb, 8);
  81.             st->codec->bits_per_coded_sample = avio_r8(s->pb);
  82.             st->codec->codec_id    = ff_codec_get_id(ff_codec_wav_tags,
  83.                                                      st->codec->codec_tag);
  84.             avpriv_set_pts_info(st, 32, 1, 1000);
  85.             break;
  86.         case 0:
  87.             avio_seek(s->pb, 2048 + 8, SEEK_SET);
  88.             return 0;
  89.         default:
  90.             avpriv_request_sample(s, "id %d", id);
  91.             return AVERROR_PATCHWELCOME;
  92.         }
  93.  
  94.         avio_seek(s->pb, next_offset, SEEK_SET);
  95.     }
  96.  
  97.     return AVERROR_EOF;
  98. }
  99.  
  100. static int lvf_read_packet(AVFormatContext *s, AVPacket *pkt)
  101. {
  102.     unsigned size, flags, timestamp, id;
  103.     int64_t pos;
  104.     int ret, is_video = 0;
  105.  
  106.     pos = avio_tell(s->pb);
  107.     while (!url_feof(s->pb)) {
  108.         id    = avio_rl32(s->pb);
  109.         size  = avio_rl32(s->pb);
  110.  
  111.         if (size == 0xFFFFFFFFu)
  112.             return AVERROR_EOF;
  113.  
  114.         switch (id) {
  115.         case MKTAG('0', '0', 'd', 'c'):
  116.             is_video = 1;
  117.         case MKTAG('0', '1', 'w', 'b'):
  118.             if (size < 8)
  119.                 return AVERROR_INVALIDDATA;
  120.             timestamp = avio_rl32(s->pb);
  121.             flags = avio_rl32(s->pb);
  122.             ret = av_get_packet(s->pb, pkt, size - 8);
  123.             if (flags & (1 << 12))
  124.                 pkt->flags |= AV_PKT_FLAG_KEY;
  125.             pkt->stream_index = is_video ? 0 : 1;
  126.             pkt->pts          = timestamp;
  127.             pkt->pos          = pos;
  128.             return ret;
  129.         default:
  130.             ret = avio_skip(s->pb, size);
  131.         }
  132.  
  133.         if (ret < 0)
  134.             return ret;
  135.     }
  136.  
  137.     return AVERROR_EOF;
  138. }
  139.  
  140. AVInputFormat ff_lvf_demuxer = {
  141.     .name        = "lvf",
  142.     .long_name   = NULL_IF_CONFIG_SMALL("LVF"),
  143.     .read_probe  = lvf_probe,
  144.     .read_header = lvf_read_header,
  145.     .read_packet = lvf_read_packet,
  146.     .extensions  = "lvf",
  147.     .flags       = AVFMT_GENERIC_INDEX,
  148. };
  149.