]> git.sesse.net Git - ffmpeg/blob - libavformat/dtshddec.c
Merge commit '56b17a33f231859cbccbd741b4763617cb4ecf03'
[ffmpeg] / libavformat / dtshddec.c
1 /*
2  * Raw DTS-HD 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 "libavutil/dict.h"
24 #include "libavcodec/dca.h"
25 #include "avformat.h"
26 #include "internal.h"
27
28 #define AUPR_HDR 0x415550522D484452
29 #define AUPRINFO 0x41555052494E464F
30 #define BITSHVTB 0x4249545348565442
31 #define BLACKOUT 0x424C41434B4F5554
32 #define BRANCHPT 0x4252414E43485054
33 #define BUILDVER 0x4255494C44564552
34 #define CORESSMD 0x434F524553534D44
35 #define DTSHDHDR 0x4454534844484452
36 #define EXTSS_MD 0x45585453535f4d44
37 #define FILEINFO 0x46494C45494E464F
38 #define NAVI_TBL 0x4E4156492D54424C
39 #define STRMDATA 0x5354524D44415441
40 #define TIMECODE 0x54494D45434F4445
41
42 typedef struct DTSHDDemuxContext {
43     uint64_t    data_start;
44     uint64_t    data_end;
45 } DTSHDDemuxContext;
46
47 static int dtshd_probe(AVProbeData *p)
48 {
49     if (AV_RB64(p->buf) == DTSHDHDR)
50         return AVPROBE_SCORE_MAX;
51     return 0;
52 }
53
54 static int dtshd_read_header(AVFormatContext *s)
55 {
56     DTSHDDemuxContext *dtshd = s->priv_data;
57     AVIOContext *pb = s->pb;
58     uint64_t chunk_type, chunk_size;
59     int64_t duration;
60     AVStream *st;
61     int ret;
62     char *value;
63
64     st = avformat_new_stream(s, NULL);
65     if (!st)
66         return AVERROR(ENOMEM);
67     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
68     st->codecpar->codec_id   = AV_CODEC_ID_DTS;
69     st->need_parsing         = AVSTREAM_PARSE_FULL_RAW;
70
71     for (;;) {
72         chunk_type = avio_rb64(pb);
73         chunk_size = avio_rb64(pb);
74
75         if (avio_feof(pb))
76             break;
77
78         if (chunk_size < 4) {
79             av_log(s, AV_LOG_ERROR, "chunk size too small\n");
80             return AVERROR_INVALIDDATA;
81         }
82         if (chunk_size > ((uint64_t)1 << 61)) {
83             av_log(s, AV_LOG_ERROR, "chunk size too big\n");
84             return AVERROR_INVALIDDATA;
85         }
86
87         switch (chunk_type) {
88         case STRMDATA:
89             dtshd->data_start = avio_tell(pb);
90             dtshd->data_end = dtshd->data_start + chunk_size;
91             if (dtshd->data_end <= chunk_size)
92                 return AVERROR_INVALIDDATA;
93             if (!pb->seekable)
94                 goto break_loop;
95             goto skip;
96             break;
97         case AUPR_HDR:
98             if (chunk_size < 21)
99                 return AVERROR_INVALIDDATA;
100             avio_skip(pb, 3);
101             st->codecpar->sample_rate = avio_rb24(pb);
102             if (!st->codecpar->sample_rate)
103                 return AVERROR_INVALIDDATA;
104             duration  = avio_rb32(pb); // num_frames
105             duration *= avio_rb16(pb); // samples_per_frames
106             st->duration = duration;
107             avio_skip(pb, 5);
108             st->codecpar->channels = ff_dca_count_chs_for_mask(avio_rb16(pb));
109             st->codecpar->initial_padding = avio_rb16(pb);
110             avio_skip(pb, chunk_size - 21);
111             break;
112         case FILEINFO:
113             if (chunk_size > INT_MAX)
114                 goto skip;
115             value = av_malloc(chunk_size);
116             if (!value)
117                 goto skip;
118             avio_read(pb, value, chunk_size);
119             value[chunk_size - 1] = 0;
120             av_dict_set(&s->metadata, "fileinfo", value,
121                         AV_DICT_DONT_STRDUP_VAL);
122             break;
123         default:
124 skip:
125             ret = avio_skip(pb, chunk_size);
126             if (ret < 0)
127                 return ret;
128         };
129     }
130
131     if (!dtshd->data_end)
132         return AVERROR_EOF;
133
134     avio_seek(pb, dtshd->data_start, SEEK_SET);
135
136 break_loop:
137     if (st->codecpar->sample_rate)
138         avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
139
140     return 0;
141 }
142
143 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
144 {
145     DTSHDDemuxContext *dtshd = s->priv_data;
146     int64_t size, left;
147     int ret;
148
149     left = dtshd->data_end - avio_tell(s->pb);
150     size = FFMIN(left, 1024);
151     if (size <= 0)
152         return AVERROR_EOF;
153
154     ret = av_get_packet(s->pb, pkt, size);
155     if (ret < 0)
156         return ret;
157
158     pkt->stream_index = 0;
159
160     return ret;
161 }
162
163 AVInputFormat ff_dtshd_demuxer = {
164     .name           = "dtshd",
165     .long_name      = NULL_IF_CONFIG_SMALL("raw DTS-HD"),
166     .priv_data_size = sizeof(DTSHDDemuxContext),
167     .read_probe     = dtshd_probe,
168     .read_header    = dtshd_read_header,
169     .read_packet    = raw_read_packet,
170     .flags          = AVFMT_GENERIC_INDEX,
171     .extensions     = "dtshd",
172     .raw_codec_id   = AV_CODEC_ID_DTS,
173 };