annotate ffmpeg/libavformat/thp.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * THP Demuxer
yading@11 3 * Copyright (c) 2007 Marco Gerards
yading@11 4 *
yading@11 5 * This file is part of FFmpeg.
yading@11 6 *
yading@11 7 * FFmpeg is free software; you can redistribute it and/or
yading@11 8 * modify it under the terms of the GNU Lesser General Public
yading@11 9 * License as published by the Free Software Foundation; either
yading@11 10 * version 2.1 of the License, or (at your option) any later version.
yading@11 11 *
yading@11 12 * FFmpeg is distributed in the hope that it will be useful,
yading@11 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 15 * Lesser General Public License for more details.
yading@11 16 *
yading@11 17 * You should have received a copy of the GNU Lesser General Public
yading@11 18 * License along with FFmpeg; if not, write to the Free Software
yading@11 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 20 */
yading@11 21
yading@11 22 #include "libavutil/intreadwrite.h"
yading@11 23 #include "libavutil/intfloat.h"
yading@11 24 #include "avformat.h"
yading@11 25 #include "internal.h"
yading@11 26
yading@11 27 typedef struct ThpDemuxContext {
yading@11 28 int version;
yading@11 29 int first_frame;
yading@11 30 int first_framesz;
yading@11 31 int last_frame;
yading@11 32 int compoff;
yading@11 33 int framecnt;
yading@11 34 AVRational fps;
yading@11 35 int frame;
yading@11 36 int next_frame;
yading@11 37 int next_framesz;
yading@11 38 int video_stream_index;
yading@11 39 int audio_stream_index;
yading@11 40 int compcount;
yading@11 41 unsigned char components[16];
yading@11 42 AVStream* vst;
yading@11 43 int has_audio;
yading@11 44 unsigned audiosize;
yading@11 45 } ThpDemuxContext;
yading@11 46
yading@11 47
yading@11 48 static int thp_probe(AVProbeData *p)
yading@11 49 {
yading@11 50 /* check file header */
yading@11 51 if (AV_RL32(p->buf) == MKTAG('T', 'H', 'P', '\0'))
yading@11 52 return AVPROBE_SCORE_MAX;
yading@11 53 else
yading@11 54 return 0;
yading@11 55 }
yading@11 56
yading@11 57 static int thp_read_header(AVFormatContext *s)
yading@11 58 {
yading@11 59 ThpDemuxContext *thp = s->priv_data;
yading@11 60 AVStream *st;
yading@11 61 AVIOContext *pb = s->pb;
yading@11 62 int64_t fsize= avio_size(pb);
yading@11 63 int i;
yading@11 64
yading@11 65 /* Read the file header. */
yading@11 66 avio_rb32(pb); /* Skip Magic. */
yading@11 67 thp->version = avio_rb32(pb);
yading@11 68
yading@11 69 avio_rb32(pb); /* Max buf size. */
yading@11 70 avio_rb32(pb); /* Max samples. */
yading@11 71
yading@11 72 thp->fps = av_d2q(av_int2float(avio_rb32(pb)), INT_MAX);
yading@11 73 thp->framecnt = avio_rb32(pb);
yading@11 74 thp->first_framesz = avio_rb32(pb);
yading@11 75 pb->maxsize = avio_rb32(pb);
yading@11 76 if(fsize>0 && (!pb->maxsize || fsize < pb->maxsize))
yading@11 77 pb->maxsize= fsize;
yading@11 78
yading@11 79 thp->compoff = avio_rb32(pb);
yading@11 80 avio_rb32(pb); /* offsetDataOffset. */
yading@11 81 thp->first_frame = avio_rb32(pb);
yading@11 82 thp->last_frame = avio_rb32(pb);
yading@11 83
yading@11 84 thp->next_framesz = thp->first_framesz;
yading@11 85 thp->next_frame = thp->first_frame;
yading@11 86
yading@11 87 /* Read the component structure. */
yading@11 88 avio_seek (pb, thp->compoff, SEEK_SET);
yading@11 89 thp->compcount = avio_rb32(pb);
yading@11 90
yading@11 91 /* Read the list of component types. */
yading@11 92 avio_read(pb, thp->components, 16);
yading@11 93
yading@11 94 for (i = 0; i < thp->compcount; i++) {
yading@11 95 if (thp->components[i] == 0) {
yading@11 96 if (thp->vst != 0)
yading@11 97 break;
yading@11 98
yading@11 99 /* Video component. */
yading@11 100 st = avformat_new_stream(s, NULL);
yading@11 101 if (!st)
yading@11 102 return AVERROR(ENOMEM);
yading@11 103
yading@11 104 /* The denominator and numerator are switched because 1/fps
yading@11 105 is required. */
yading@11 106 avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
yading@11 107 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
yading@11 108 st->codec->codec_id = AV_CODEC_ID_THP;
yading@11 109 st->codec->codec_tag = 0; /* no fourcc */
yading@11 110 st->codec->width = avio_rb32(pb);
yading@11 111 st->codec->height = avio_rb32(pb);
yading@11 112 st->codec->sample_rate = av_q2d(thp->fps);
yading@11 113 st->nb_frames =
yading@11 114 st->duration = thp->framecnt;
yading@11 115 thp->vst = st;
yading@11 116 thp->video_stream_index = st->index;
yading@11 117
yading@11 118 if (thp->version == 0x11000)
yading@11 119 avio_rb32(pb); /* Unknown. */
yading@11 120 } else if (thp->components[i] == 1) {
yading@11 121 if (thp->has_audio != 0)
yading@11 122 break;
yading@11 123
yading@11 124 /* Audio component. */
yading@11 125 st = avformat_new_stream(s, NULL);
yading@11 126 if (!st)
yading@11 127 return AVERROR(ENOMEM);
yading@11 128
yading@11 129 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
yading@11 130 st->codec->codec_id = AV_CODEC_ID_ADPCM_THP;
yading@11 131 st->codec->codec_tag = 0; /* no fourcc */
yading@11 132 st->codec->channels = avio_rb32(pb); /* numChannels. */
yading@11 133 st->codec->sample_rate = avio_rb32(pb); /* Frequency. */
yading@11 134
yading@11 135 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
yading@11 136
yading@11 137 thp->audio_stream_index = st->index;
yading@11 138 thp->has_audio = 1;
yading@11 139 }
yading@11 140 }
yading@11 141
yading@11 142 return 0;
yading@11 143 }
yading@11 144
yading@11 145 static int thp_read_packet(AVFormatContext *s,
yading@11 146 AVPacket *pkt)
yading@11 147 {
yading@11 148 ThpDemuxContext *thp = s->priv_data;
yading@11 149 AVIOContext *pb = s->pb;
yading@11 150 unsigned int size;
yading@11 151 int ret;
yading@11 152
yading@11 153 if (thp->audiosize == 0) {
yading@11 154 /* Terminate when last frame is reached. */
yading@11 155 if (thp->frame >= thp->framecnt)
yading@11 156 return AVERROR_EOF;
yading@11 157
yading@11 158 avio_seek(pb, thp->next_frame, SEEK_SET);
yading@11 159
yading@11 160 /* Locate the next frame and read out its size. */
yading@11 161 thp->next_frame += thp->next_framesz;
yading@11 162 thp->next_framesz = avio_rb32(pb);
yading@11 163
yading@11 164 avio_rb32(pb); /* Previous total size. */
yading@11 165 size = avio_rb32(pb); /* Total size of this frame. */
yading@11 166
yading@11 167 /* Store the audiosize so the next time this function is called,
yading@11 168 the audio can be read. */
yading@11 169 if (thp->has_audio)
yading@11 170 thp->audiosize = avio_rb32(pb); /* Audio size. */
yading@11 171 else
yading@11 172 thp->frame++;
yading@11 173
yading@11 174 ret = av_get_packet(pb, pkt, size);
yading@11 175 if (ret != size) {
yading@11 176 av_free_packet(pkt);
yading@11 177 return AVERROR(EIO);
yading@11 178 }
yading@11 179
yading@11 180 pkt->stream_index = thp->video_stream_index;
yading@11 181 } else {
yading@11 182 ret = av_get_packet(pb, pkt, thp->audiosize);
yading@11 183 if (ret != thp->audiosize) {
yading@11 184 av_free_packet(pkt);
yading@11 185 return AVERROR(EIO);
yading@11 186 }
yading@11 187
yading@11 188 pkt->stream_index = thp->audio_stream_index;
yading@11 189 if (thp->audiosize >= 8)
yading@11 190 pkt->duration = AV_RB32(&pkt->data[4]);
yading@11 191
yading@11 192 thp->audiosize = 0;
yading@11 193 thp->frame++;
yading@11 194 }
yading@11 195
yading@11 196 return 0;
yading@11 197 }
yading@11 198
yading@11 199 AVInputFormat ff_thp_demuxer = {
yading@11 200 .name = "thp",
yading@11 201 .long_name = NULL_IF_CONFIG_SMALL("THP"),
yading@11 202 .priv_data_size = sizeof(ThpDemuxContext),
yading@11 203 .read_probe = thp_probe,
yading@11 204 .read_header = thp_read_header,
yading@11 205 .read_packet = thp_read_packet
yading@11 206 };