]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/tta.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / tta.c
index f361ba5188235f760af538e945d7f3d7726d638a..9c31432b7b45be51a2c3b7a3bb691003669677cb 100644 (file)
@@ -2,20 +2,20 @@
  * TTA demuxer
  * Copyright (c) 2006 Alex Beregszaszi
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -27,6 +27,8 @@
 
 typedef struct {
     int totalframes, currentframe;
+    int frame_size;
+    int last_frame_size;
 } TTAContext;
 
 static int tta_probe(AVProbeData *p)
@@ -38,11 +40,11 @@ static int tta_probe(AVProbeData *p)
     return 0;
 }
 
-static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
+static int tta_read_header(AVFormatContext *s)
 {
     TTAContext *c = s->priv_data;
     AVStream *st;
-    int i, channels, bps, samplerate, datalen, framelen;
+    int i, channels, bps, samplerate, datalen;
     uint64_t framepos, start_offset;
 
     if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
@@ -69,12 +71,15 @@ static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
 
     avio_skip(s->pb, 4); // header crc
 
-    framelen = samplerate*256/245;
-    c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
+    c->frame_size      = samplerate * 256 / 245;
+    c->last_frame_size = datalen % c->frame_size;
+    if (!c->last_frame_size)
+        c->last_frame_size = c->frame_size;
+    c->totalframes = datalen / c->frame_size + (c->last_frame_size < c->frame_size);
     c->currentframe = 0;
 
-    if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
-        av_log(s, AV_LOG_ERROR, "totalframes too large\n");
+    if(c->totalframes >= UINT_MAX/sizeof(uint32_t) || c->totalframes <= 0){
+        av_log(s, AV_LOG_ERROR, "totalframes %d invalid\n", c->totalframes);
         return -1;
     }
 
@@ -90,7 +95,8 @@ static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
 
     for (i = 0; i < c->totalframes; i++) {
         uint32_t size = avio_rl32(s->pb);
-        av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
+        av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
+                           AVINDEX_KEYFRAME);
         framepos += size;
     }
     avio_skip(s->pb, 4); // seektable crc
@@ -126,12 +132,14 @@ static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     // FIXME!
     if (c->currentframe >= c->totalframes)
-        return -1;
+        return AVERROR_EOF;
 
     size = st->index_entries[c->currentframe].size;
 
     ret = av_get_packet(s->pb, pkt, size);
     pkt->dts = st->index_entries[c->currentframe++].timestamp;
+    pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
+                                                        c->frame_size;
     return ret;
 }
 
@@ -142,9 +150,10 @@ static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
     int index = av_index_search_timestamp(st, timestamp, flags);
     if (index < 0)
         return -1;
+    if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
+        return -1;
 
     c->currentframe = index;
-    avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
 
     return 0;
 }