annotate libmpdemux/demux_audio.c @ 27194:fd1872d2b2d4

avoid unnecessary strdup(); patch by Aurel
author nicodvb
date Sun, 06 Jul 2008 18:04:14 +0000
parents 4899f42d1c94
children e2f7de85ebfb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
1
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
2 #include "config.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16877
diff changeset
3 #include "mp_msg.h"
18176
f72bc5754209 Part3 of Otvos Attila's oattila AT chello-hu mp_msg changes, with lots of modifications as usual
reynaldo
parents: 18117
diff changeset
4 #include "help_mp.h"
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
5
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
6 #include <stdlib.h>
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
7 #include <stdio.h>
22605
4d81dbdf46b9 Add explicit location for headers from the stream/ directory.
diego
parents: 22586
diff changeset
8 #include "stream/stream.h"
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
9 #include "demuxer.h"
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
10 #include "stheader.h"
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
11 #include "genres.h"
6763
e29f95ed5d36 Fix vbr muxing and win32 codec crash on init
albeu
parents: 5812
diff changeset
12 #include "mp3_hdr.h"
23627
980d734e102b Use AV_RL32
reimar
parents: 22605
diff changeset
13 #include "libavutil/intreadwrite.h"
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
14
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
15 #include <string.h>
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
16
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
17 #define MP3 1
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
18 #define WAV 2
11004
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents: 10891
diff changeset
19 #define fLaC 3
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
20
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
21
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
22 #define HDR_SIZE 4
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
23
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
24 typedef struct da_priv {
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
25 int frmt;
24589
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
26 double next_pts;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
27 } da_priv_t;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
28
18074
a491d7628cdc check wav header length against upper limit, should protect against
reimar
parents: 17977
diff changeset
29 //! rather arbitrary value for maximum length of wav-format headers
a491d7628cdc check wav header length against upper limit, should protect against
reimar
parents: 17977
diff changeset
30 #define MAX_WAVHDR_LEN (1 * 1024 * 1024)
a491d7628cdc check wav header length against upper limit, should protect against
reimar
parents: 17977
diff changeset
31
21651
54aef84a6b50 doxify a comment
reimar
parents: 21650
diff changeset
32 //! how many valid frames in a row we need before accepting as valid MP3
21650
0463916369ed Require 12 consecutive MPEG-audio headers before detecting as audio.
reimar
parents: 20334
diff changeset
33 #define MIN_MP3_HDRS 12
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
34
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
35 //! Used to describe a potential (chain of) MP3 headers we found
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
36 typedef struct mp3_hdr {
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
37 off_t frame_pos; // start of first frame in this "chain" of headers
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
38 off_t next_frame_pos; // here we expect the next header with same parameters
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
39 int mp3_chans;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
40 int mp3_freq;
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
41 int mpa_spf;
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
42 int mpa_layer;
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
43 int mpa_br;
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
44 int cons_hdrs; // if this reaches MIN_MP3_HDRS we accept as MP3 file
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
45 struct mp3_hdr *next;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
46 } mp3_hdr_t;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
47
17977
f70772d02eaa Convert printfs in aviprint.c to mp_msg and give the information printing
diego
parents: 17932
diff changeset
48 extern void print_wave_header(WAVEFORMATEX *h, int verbose_level);
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
49
7867
3dc0b71630ff cleanup config option handling in libmpdemux.
arpi
parents: 7847
diff changeset
50 int hr_mp3_seek = 0;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
51
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
52 /**
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
53 * \brief free a list of MP3 header descriptions
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
54 * \param list pointer to the head-of-list pointer
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
55 */
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
56 static void free_mp3_hdrs(mp3_hdr_t **list) {
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
57 mp3_hdr_t *tmp;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
58 while (*list) {
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
59 tmp = (*list)->next;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
60 free(*list);
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
61 *list = tmp;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
62 }
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
63 }
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
64
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
65 /**
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
66 * \brief add another potential MP3 header to our list
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
67 * If it fits into an existing chain this one is expanded otherwise
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
68 * a new one is created.
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
69 * All entries that expected a MP3 header before the current position
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
70 * are discarded.
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
71 * The list is expected to be and will be kept sorted by next_frame_pos
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
72 * and when those are equal by frame_pos.
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
73 * \param list pointer to the head-of-list pointer
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
74 * \param st_pos stream position where the described header starts
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
75 * \param mp3_chans number of channels as specified by the header (*)
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
76 * \param mp3_freq sampling frequency as specified by the header (*)
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
77 * \param mpa_spf frame size as specified by the header
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
78 * \param mpa_layer layer type ("version") as specified by the header (*)
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
79 * \param mpa_br bitrate as specified by the header
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
80 * \param mp3_flen length of the frame as specified by the header
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
81 * \return If non-null the current file is accepted as MP3 and the
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
82 * mp3_hdr struct describing the valid chain is returned. Must be
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
83 * freed independent of the list.
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
84 *
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
85 * parameters marked by (*) must be the same for all headers in the same chain
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
86 */
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
87 static mp3_hdr_t *add_mp3_hdr(mp3_hdr_t **list, off_t st_pos,
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
88 int mp3_chans, int mp3_freq, int mpa_spf,
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
89 int mpa_layer, int mpa_br, int mp3_flen) {
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
90 mp3_hdr_t *tmp;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
91 int in_list = 0;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
92 while (*list && (*list)->next_frame_pos <= st_pos) {
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
93 if (((*list)->next_frame_pos < st_pos) || ((*list)->mp3_chans != mp3_chans)
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
94 || ((*list)->mp3_freq != mp3_freq) || ((*list)->mpa_layer != mpa_layer) ) {
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
95 // wasn't valid!
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
96 tmp = (*list)->next;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
97 free(*list);
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
98 *list = tmp;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
99 } else {
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
100 (*list)->cons_hdrs++;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
101 (*list)->next_frame_pos = st_pos + mp3_flen;
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
102 (*list)->mpa_spf = mpa_spf;
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
103 (*list)->mpa_br = mpa_br;
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
104 if ((*list)->cons_hdrs >= MIN_MP3_HDRS) {
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
105 // copy the valid entry, so that the list can be easily freed
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
106 tmp = malloc(sizeof(mp3_hdr_t));
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
107 memcpy(tmp, *list, sizeof(mp3_hdr_t));
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
108 tmp->next = NULL;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
109 return tmp;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
110 }
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
111 in_list = 1;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
112 list = &((*list)->next);
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
113 }
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
114 }
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
115 if (!in_list) { // does not belong into an existing chain, insert
15215
db6286839af7 Fix MP3 detection (list of found MP3 headers was not kep sorted).
reimar
parents: 15205
diff changeset
116 // find right position to insert to keep sorting
db6286839af7 Fix MP3 detection (list of found MP3 headers was not kep sorted).
reimar
parents: 15205
diff changeset
117 while (*list && (*list)->next_frame_pos <= st_pos + mp3_flen)
db6286839af7 Fix MP3 detection (list of found MP3 headers was not kep sorted).
reimar
parents: 15205
diff changeset
118 list = &((*list)->next);
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
119 tmp = malloc(sizeof(mp3_hdr_t));
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
120 tmp->frame_pos = st_pos;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
121 tmp->next_frame_pos = st_pos + mp3_flen;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
122 tmp->mp3_chans = mp3_chans;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
123 tmp->mp3_freq = mp3_freq;
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
124 tmp->mpa_spf = mpa_spf;
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
125 tmp->mpa_layer = mpa_layer;
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
126 tmp->mpa_br = mpa_br;
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
127 tmp->cons_hdrs = 1;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
128 tmp->next = *list;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
129 *list = tmp;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
130 }
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
131 return NULL;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
132 }
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
133
25896
5745272a5636 Add a comment to the #if 0
reimar
parents: 25890
diff changeset
134 #if 0 /* this code is a mess, clean it up before reenabling */
17898
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
135 #define FLAC_SIGNATURE_SIZE 4
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
136 #define FLAC_STREAMINFO_SIZE 34
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
137 #define FLAC_SEEKPOINT_SIZE 18
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
138
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
139 enum {
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
140 FLAC_STREAMINFO = 0,
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
141 FLAC_PADDING,
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
142 FLAC_APPLICATION,
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
143 FLAC_SEEKTABLE,
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
144 FLAC_VORBIS_COMMENT,
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
145 FLAC_CUESHEET
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
146 } flac_preamble_t;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
147
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
148 static void
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
149 get_flac_metadata (demuxer_t* demuxer)
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
150 {
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
151 uint8_t preamble[4];
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
152 unsigned int blk_len;
26374
268f87e940ab Remove useless checks
reimar
parents: 26373
diff changeset
153 stream_t *s = demuxer->stream;
17898
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
154
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
155 /* file is qualified; skip over the signature bytes in the stream */
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
156 stream_seek (s, 4);
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
157
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
158 /* loop through the metadata blocks; use a do-while construct since there
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
159 * will always be 1 metadata block */
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
160 do {
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
161 int r;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
162
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
163 r = stream_read (s, (char *) preamble, FLAC_SIGNATURE_SIZE);
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
164 if (r != FLAC_SIGNATURE_SIZE)
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
165 return;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
166
26375
4319a0b14666 Simplify: use AV_RB24
reimar
parents: 26374
diff changeset
167 blk_len = AV_RB24(preamble + 1);
17898
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
168
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
169 switch (preamble[0] & 0x7F)
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
170 {
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
171 case FLAC_VORBIS_COMMENT:
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
172 {
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
173 /* For a description of the format please have a look at */
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
174 /* http://www.xiph.org/vorbis/doc/v-comment.html */
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
175
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
176 uint32_t length, comment_list_len;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
177 char comments[blk_len];
23628
d4f96ae7081c Get rid of: useless variable, useless cast and void * arithmetic
reimar
parents: 23627
diff changeset
178 uint8_t *ptr = comments;
17898
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
179 char *comment;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
180 int cn;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
181 char c;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
182
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
183 if (stream_read (s, comments, blk_len) == blk_len)
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
184 {
23628
d4f96ae7081c Get rid of: useless variable, useless cast and void * arithmetic
reimar
parents: 23627
diff changeset
185 length = AV_RL32(ptr);
17898
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
186 ptr += 4 + length;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
187
23628
d4f96ae7081c Get rid of: useless variable, useless cast and void * arithmetic
reimar
parents: 23627
diff changeset
188 comment_list_len = AV_RL32(ptr);
17898
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
189 ptr += 4;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
190
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
191 cn = 0;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
192 for (; cn < comment_list_len; cn++)
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
193 {
23628
d4f96ae7081c Get rid of: useless variable, useless cast and void * arithmetic
reimar
parents: 23627
diff changeset
194 length = AV_RL32(ptr);
17898
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
195 ptr += 4;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
196
23628
d4f96ae7081c Get rid of: useless variable, useless cast and void * arithmetic
reimar
parents: 23627
diff changeset
197 comment = ptr;
25889
264ca4cd0c8e Properly check length of flac metadata.
reimar
parents: 25883
diff changeset
198 if (&comment[length] < comments || &comment[length] >= &comments[blk_len])
264ca4cd0c8e Properly check length of flac metadata.
reimar
parents: 25883
diff changeset
199 return;
17898
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
200 c = comment[length];
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
201 comment[length] = 0;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
202
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
203 if (!strncasecmp ("TITLE=", comment, 6) && (length - 6 > 0))
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
204 demux_info_add (demuxer, "Title", comment + 6);
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
205 else if (!strncasecmp ("ARTIST=", comment, 7) && (length - 7 > 0))
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
206 demux_info_add (demuxer, "Artist", comment + 7);
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
207 else if (!strncasecmp ("ALBUM=", comment, 6) && (length - 6 > 0))
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
208 demux_info_add (demuxer, "Album", comment + 6);
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
209 else if (!strncasecmp ("DATE=", comment, 5) && (length - 5 > 0))
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
210 demux_info_add (demuxer, "Year", comment + 5);
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
211 else if (!strncasecmp ("GENRE=", comment, 6) && (length - 6 > 0))
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
212 demux_info_add (demuxer, "Genre", comment + 6);
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
213 else if (!strncasecmp ("Comment=", comment, 8) && (length - 8 > 0))
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
214 demux_info_add (demuxer, "Comment", comment + 8);
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
215 else if (!strncasecmp ("TRACKNUMBER=", comment, 12)
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
216 && (length - 12 > 0))
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
217 {
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
218 char buf[31];
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
219 buf[30] = '\0';
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
220 sprintf (buf, "%d", atoi (comment + 12));
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
221 demux_info_add(demuxer, "Track", buf);
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
222 }
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
223 comment[length] = c;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
224
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
225 ptr += length;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
226 }
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
227 }
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
228 break;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
229 }
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
230
26376
68302d38e153 Remove another two useless special-case from flac metadata reading function
reimar
parents: 26375
diff changeset
231 case FLAC_STREAMINFO:
26373
700728d4ad3d Simplify (currently disabled) get_flac_metadata
reimar
parents: 25896
diff changeset
232 case FLAC_PADDING:
700728d4ad3d Simplify (currently disabled) get_flac_metadata
reimar
parents: 25896
diff changeset
233 case FLAC_APPLICATION:
26376
68302d38e153 Remove another two useless special-case from flac metadata reading function
reimar
parents: 26375
diff changeset
234 case FLAC_SEEKTABLE:
17898
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
235 case FLAC_CUESHEET:
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
236 default:
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
237 /* 6-127 are presently reserved */
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
238 stream_skip (s, blk_len);
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
239 break;
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
240 }
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
241 } while ((preamble[0] & 0x80) == 0);
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
242 }
25890
374816d4d565 Disable reading of flac metadata, mere metadata is not worth such a mess.
reimar
parents: 25889
diff changeset
243 #endif
17898
a2b85171b4de retrieve metadata from FLAC files (patch by Benjamin Zores < ben _at_ geexbox.org >)
aurel
parents: 17790
diff changeset
244
16175
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
245 static int demux_audio_open(demuxer_t* demuxer) {
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
246 stream_t *s;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
247 sh_audio_t* sh_audio;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
248 uint8_t hdr[HDR_SIZE];
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
249 int frmt = 0, n = 0, step;
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
250 off_t st_pos = 0, next_frame_pos = 0;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
251 // mp3_hdrs list is sorted first by next_frame_pos and then by frame_pos
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
252 mp3_hdr_t *mp3_hdrs = NULL, *mp3_found = NULL;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
253 da_priv_t* priv;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
254
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
255 s = demuxer->stream;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
256
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
257 stream_read(s, hdr, HDR_SIZE);
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
258 while(n < 30000 && !s->eof) {
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
259 int mp3_freq, mp3_chans, mp3_flen, mpa_layer, mpa_spf, mpa_br;
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
260 st_pos = stream_tell(s) - HDR_SIZE;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
261 step = 1;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
262
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
263 if( hdr[0] == 'R' && hdr[1] == 'I' && hdr[2] == 'F' && hdr[3] == 'F' ) {
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
264 stream_skip(s,4);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
265 if(s->eof)
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
266 break;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
267 stream_read(s,hdr,4);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
268 if(s->eof)
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
269 break;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
270 if(hdr[0] != 'W' || hdr[1] != 'A' || hdr[2] != 'V' || hdr[3] != 'E' )
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
271 stream_skip(s,-8);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
272 else
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
273 // We found wav header. Now we can have 'fmt ' or a mp3 header
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
274 // empty the buffer
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
275 step = 4;
8116
3757669b825b skip id3v2 junk when looking for mp3
rfelker
parents: 8110
diff changeset
276 } else if( hdr[0] == 'I' && hdr[1] == 'D' && hdr[2] == '3' && (hdr[3] >= 2)) {
3757669b825b skip id3v2 junk when looking for mp3
rfelker
parents: 8110
diff changeset
277 int len;
3757669b825b skip id3v2 junk when looking for mp3
rfelker
parents: 8110
diff changeset
278 stream_skip(s,2);
3757669b825b skip id3v2 junk when looking for mp3
rfelker
parents: 8110
diff changeset
279 stream_read(s,hdr,4);
3757669b825b skip id3v2 junk when looking for mp3
rfelker
parents: 8110
diff changeset
280 len = (hdr[0]<<21) | (hdr[1]<<14) | (hdr[2]<<7) | hdr[3];
3757669b825b skip id3v2 junk when looking for mp3
rfelker
parents: 8110
diff changeset
281 stream_skip(s,len);
3757669b825b skip id3v2 junk when looking for mp3
rfelker
parents: 8110
diff changeset
282 step = 4;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
283 } else if( hdr[0] == 'f' && hdr[1] == 'm' && hdr[2] == 't' && hdr[3] == ' ' ) {
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
284 frmt = WAV;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
285 break;
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
286 } else if((mp3_flen = mp_get_mp3_header(hdr, &mp3_chans, &mp3_freq,
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
287 &mpa_spf, &mpa_layer, &mpa_br)) > 0) {
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
288 mp3_found = add_mp3_hdr(&mp3_hdrs, st_pos, mp3_chans, mp3_freq,
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
289 mpa_spf, mpa_layer, mpa_br, mp3_flen);
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
290 if (mp3_found) {
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
291 frmt = MP3;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
292 break;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
293 }
11004
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents: 10891
diff changeset
294 } else if( hdr[0] == 'f' && hdr[1] == 'L' && hdr[2] == 'a' && hdr[3] == 'C' ) {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents: 10891
diff changeset
295 frmt = fLaC;
26812
811d717b722e Continue detection if it is not clear if we have a MP3 or flac file.
reimar
parents: 26376
diff changeset
296 if (!mp3_hdrs || mp3_hdrs->cons_hdrs < 3)
811d717b722e Continue detection if it is not clear if we have a MP3 or flac file.
reimar
parents: 26376
diff changeset
297 break;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
298 }
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
299 // Add here some other audio format detection
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
300 if(step < HDR_SIZE)
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
301 memmove(hdr,&hdr[step],HDR_SIZE-step);
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
302 stream_read(s, &hdr[HDR_SIZE - step], step);
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
303 n++;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
304 }
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
305
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
306 free_mp3_hdrs(&mp3_hdrs);
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
307
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
308 if(!frmt)
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
309 return 0;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
310
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
311 sh_audio = new_sh_audio(demuxer,0);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
312
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
313 switch(frmt) {
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
314 case MP3:
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
315 sh_audio->format = (mp3_found->mpa_layer < 3 ? 0x50 : 0x55);
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
316 demuxer->movi_start = mp3_found->frame_pos;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
317 next_frame_pos = mp3_found->next_frame_pos;
6763
e29f95ed5d36 Fix vbr muxing and win32 codec crash on init
albeu
parents: 5812
diff changeset
318 sh_audio->audio.dwSampleSize= 0;
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
319 sh_audio->audio.dwScale = mp3_found->mpa_spf;
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
320 sh_audio->audio.dwRate = mp3_found->mp3_freq;
6763
e29f95ed5d36 Fix vbr muxing and win32 codec crash on init
albeu
parents: 5812
diff changeset
321 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
e29f95ed5d36 Fix vbr muxing and win32 codec crash on init
albeu
parents: 5812
diff changeset
322 sh_audio->wf->wFormatTag = sh_audio->format;
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
323 sh_audio->wf->nChannels = mp3_found->mp3_chans;
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
324 sh_audio->wf->nSamplesPerSec = mp3_found->mp3_freq;
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
325 sh_audio->wf->nAvgBytesPerSec = mp3_found->mpa_br * (1000 / 8);
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
326 sh_audio->wf->nBlockAlign = mp3_found->mpa_spf;
6763
e29f95ed5d36 Fix vbr muxing and win32 codec crash on init
albeu
parents: 5812
diff changeset
327 sh_audio->wf->wBitsPerSample = 16;
e29f95ed5d36 Fix vbr muxing and win32 codec crash on init
albeu
parents: 5812
diff changeset
328 sh_audio->wf->cbSize = 0;
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
329 sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
330 free(mp3_found);
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
331 mp3_found = NULL;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
332 if(s->end_pos) {
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
333 char tag[4];
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
334 stream_seek(s,s->end_pos-128);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
335 stream_read(s,tag,3);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
336 tag[3] = '\0';
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
337 if(strcmp(tag,"TAG"))
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
338 demuxer->movi_end = s->end_pos;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
339 else {
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
340 char buf[31];
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
341 uint8_t g;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
342 demuxer->movi_end = stream_tell(s)-3;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
343 stream_read(s,buf,30);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
344 buf[30] = '\0';
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
345 demux_info_add(demuxer,"Title",buf);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
346 stream_read(s,buf,30);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
347 buf[30] = '\0';
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
348 demux_info_add(demuxer,"Artist",buf);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
349 stream_read(s,buf,30);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
350 buf[30] = '\0';
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
351 demux_info_add(demuxer,"Album",buf);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
352 stream_read(s,buf,4);
4764
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
353 buf[4] = '\0';
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
354 demux_info_add(demuxer,"Year",buf);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
355 stream_read(s,buf,30);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
356 buf[30] = '\0';
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
357 demux_info_add(demuxer,"Comment",buf);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
358 if(buf[28] == 0 && buf[29] != 0) {
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
359 uint8_t trk = (uint8_t)buf[29];
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
360 sprintf(buf,"%d",trk);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
361 demux_info_add(demuxer,"Track",buf);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
362 }
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
363 g = stream_read_char(s);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
364 demux_info_add(demuxer,"Genre",genres[g]);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
365 }
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
366 }
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
367 break;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
368 case WAV: {
4720
10ad330c1733 fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents: 4717
diff changeset
369 unsigned int chunk_type;
10ad330c1733 fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents: 4717
diff changeset
370 unsigned int chunk_size;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
371 WAVEFORMATEX* w;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
372 int l;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
373 l = stream_read_dword_le(s);
9596
7b4a17c415fb Same behaviour as old code for short headers
rtognimp
parents: 9591
diff changeset
374 if(l < 16) {
9591
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
375 mp_msg(MSGT_DEMUX,MSGL_ERR,"[demux_audio] Bad wav header length: too short (%d)!!!\n",l);
20334
5fcc8a3b1e88 Try playing files with wrong wav header length, fixes riff_broken_hrdlen.wav
reimar
parents: 19062
diff changeset
376 l = 16;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
377 }
18074
a491d7628cdc check wav header length against upper limit, should protect against
reimar
parents: 17977
diff changeset
378 if(l > MAX_WAVHDR_LEN) {
a491d7628cdc check wav header length against upper limit, should protect against
reimar
parents: 17977
diff changeset
379 mp_msg(MSGT_DEMUX,MSGL_ERR,"[demux_audio] Bad wav header length: too long (%d)!!!\n",l);
20334
5fcc8a3b1e88 Try playing files with wrong wav header length, fixes riff_broken_hrdlen.wav
reimar
parents: 19062
diff changeset
380 l = 16;
18074
a491d7628cdc check wav header length against upper limit, should protect against
reimar
parents: 17977
diff changeset
381 }
19062
83c3afeab35d drops casts from void * on malloc/calloc from libmpdemux code
reynaldo
parents: 18710
diff changeset
382 sh_audio->wf = w = malloc(l > sizeof(WAVEFORMATEX) ? l : sizeof(WAVEFORMATEX));
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
383 w->wFormatTag = sh_audio->format = stream_read_word_le(s);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
384 w->nChannels = sh_audio->channels = stream_read_word_le(s);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
385 w->nSamplesPerSec = sh_audio->samplerate = stream_read_dword_le(s);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
386 w->nAvgBytesPerSec = stream_read_dword_le(s);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
387 w->nBlockAlign = stream_read_word_le(s);
22419
4cfb8def78c1 Fix wrong setting of samplesize (must be bytes per sample, not bits)
reimar
parents: 22103
diff changeset
388 w->wBitsPerSample = stream_read_word_le(s);
4cfb8def78c1 Fix wrong setting of samplesize (must be bytes per sample, not bits)
reimar
parents: 22103
diff changeset
389 sh_audio->samplesize = (w->wBitsPerSample + 7) / 8;
9616
a6bbab17a87f 10l cbSize not inited for l=16.\n Could give problem in print_wave_header
rtognimp
parents: 9596
diff changeset
390 w->cbSize = 0;
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15215
diff changeset
391 sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
9596
7b4a17c415fb Same behaviour as old code for short headers
rtognimp
parents: 9591
diff changeset
392 l -= 16;
7b4a17c415fb Same behaviour as old code for short headers
rtognimp
parents: 9591
diff changeset
393 if (l > 0) {
9591
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
394 w->cbSize = stream_read_word_le(s);
9596
7b4a17c415fb Same behaviour as old code for short headers
rtognimp
parents: 9591
diff changeset
395 l -= 2;
9655
8809e2be5cea Warning fix, patch by Dominik Mierzejewsky
rtognimp
parents: 9616
diff changeset
396 if (w->cbSize > 0) {
9591
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
397 if (l < w->cbSize) {
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
398 mp_msg(MSGT_DEMUX,MSGL_ERR,"[demux_audio] truncated extradata (%d < %d)\n",
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
399 l,w->cbSize);
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
400 stream_read(s,(char*)((char*)(w)+sizeof(WAVEFORMATEX)),l);
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
401 l = 0;
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
402 } else {
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
403 stream_read(s,(char*)((char*)(w)+sizeof(WAVEFORMATEX)),w->cbSize);
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
404 l -= w->cbSize;
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
405 }
9655
8809e2be5cea Warning fix, patch by Dominik Mierzejewsky
rtognimp
parents: 9616
diff changeset
406 }
9596
7b4a17c415fb Same behaviour as old code for short headers
rtognimp
parents: 9591
diff changeset
407 }
9591
3904b95fce8f Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents: 8359
diff changeset
408
17977
f70772d02eaa Convert printfs in aviprint.c to mp_msg and give the information printing
diego
parents: 17932
diff changeset
409 if( mp_msg_test(MSGT_DEMUX,MSGL_V) ) print_wave_header(w, MSGL_V);
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
410 if(l)
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
411 stream_skip(s,l);
4720
10ad330c1733 fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents: 4717
diff changeset
412 do
10ad330c1733 fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents: 4717
diff changeset
413 {
10ad330c1733 fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents: 4717
diff changeset
414 chunk_type = stream_read_fourcc(demuxer->stream);
10ad330c1733 fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents: 4717
diff changeset
415 chunk_size = stream_read_dword_le(demuxer->stream);
10ad330c1733 fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents: 4717
diff changeset
416 if (chunk_type != mmioFOURCC('d', 'a', 't', 'a'))
10ad330c1733 fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents: 4717
diff changeset
417 stream_skip(demuxer->stream, chunk_size);
22586
93589138bb33 Check for eof in loop searching for data chunk.
reimar
parents: 22419
diff changeset
418 } while (!s->eof && chunk_type != mmioFOURCC('d', 'a', 't', 'a'));
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
419 demuxer->movi_start = stream_tell(s);
22103
0427f8190a12 Do not read beyond end of data chunk if chunk_size is set.
reimar
parents: 21651
diff changeset
420 demuxer->movi_end = chunk_size ? demuxer->movi_start + chunk_size : s->end_pos;
7847
f1e6a68a42f1 dump wav header
arpi
parents: 6763
diff changeset
421 // printf("wav: %X .. %X\n",(int)demuxer->movi_start,(int)demuxer->movi_end);
13006
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
422 // Check if it contains dts audio
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
423 if((w->wFormatTag == 0x01) && (w->nChannels == 2) && (w->nSamplesPerSec == 44100)) {
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
424 unsigned char buf[16384]; // vlc uses 16384*4 (4 dts frames)
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
425 unsigned int i;
27111
4899f42d1c94 Make sure we do not use uninitialized data in case of a short read.
reimar
parents: 26812
diff changeset
426 memset(buf, 0, sizeof(buf));
13006
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
427 stream_read(s, buf, sizeof(buf));
17775
28c2108f1cfa "i < sizeof(buf)" must be "i < sizeof(buf) - 5", since later buf[i + 5] is used.
reimar
parents: 17636
diff changeset
428 for (i = 0; i < sizeof(buf) - 5; i += 2) {
13006
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
429 // DTS, 14 bit, LE
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
430 if((buf[i] == 0xff) && (buf[i+1] == 0x1f) && (buf[i+2] == 0x00) &&
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
431 (buf[i+3] == 0xe8) && ((buf[i+4] & 0xfe) == 0xf0) && (buf[i+5] == 0x07)) {
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
432 sh_audio->format = 0x2001;
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
433 mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 14 bit, LE\n");
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
434 break;
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
435 }
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
436 // DTS, 14 bit, BE
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
437 if((buf[i] == 0x1f) && (buf[i+1] == 0xff) && (buf[i+2] == 0xe8) &&
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
438 (buf[i+3] == 0x00) && (buf[i+4] == 0x07) && ((buf[i+5] & 0xfe) == 0xf0)) {
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
439 sh_audio->format = 0x2001;
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
440 mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 14 bit, BE\n");
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
441 break;
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
442 }
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
443 // DTS, 16 bit, BE
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
444 if((buf[i] == 0x7f) && (buf[i+1] == 0xfe) && (buf[i+2] == 0x80) &&
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
445 (buf[i+3] == 0x01)) {
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
446 sh_audio->format = 0x2001;
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
447 mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 16 bit, BE\n");
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
448 break;
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
449 }
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
450 // DTS, 16 bit, LE
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
451 if((buf[i] == 0xfe) && (buf[i+1] == 0x7f) && (buf[i+2] == 0x01) &&
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
452 (buf[i+3] == 0x80)) {
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
453 sh_audio->format = 0x2001;
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
454 mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 16 bit, LE\n");
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
455 break;
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
456 }
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
457 }
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
458 if (sh_audio->format == 0x2001)
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
459 mp_msg(MSGT_DEMUX,MSGL_DBG2,"[demux_audio] DTS sync offset = %u\n", i);
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
460
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
461 }
8639d064d3a1 DTS support via lavc and libdts
rtognimp
parents: 12213
diff changeset
462 stream_seek(s,demuxer->movi_start);
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
463 } break;
11004
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents: 10891
diff changeset
464 case fLaC:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents: 10891
diff changeset
465 sh_audio->format = mmioFOURCC('f', 'L', 'a', 'C');
18075
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
466 demuxer->movi_start = stream_tell(s) - 4;
11004
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents: 10891
diff changeset
467 demuxer->movi_end = s->end_pos;
18075
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
468 if (demuxer->movi_end > demuxer->movi_start) {
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
469 // try to find out approx. bitrate
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
470 int64_t size = demuxer->movi_end - demuxer->movi_start;
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
471 int64_t num_samples = 0;
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
472 int32_t srate = 0;
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
473 stream_skip(s, 14);
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
474 stream_read(s, (char *)&srate, 3);
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
475 srate = be2me_32(srate) >> 12;
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
476 stream_read(s, (char *)&num_samples, 5);
18117
0b13bab3f237 Use ULL for > 32 bits constant.
reimar
parents: 18075
diff changeset
477 num_samples = (be2me_64(num_samples) >> 24) & 0xfffffffffULL;
18075
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
478 if (num_samples && srate)
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
479 sh_audio->i_bps = size * srate / num_samples;
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
480 }
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
481 if (sh_audio->i_bps < 1) // guess value to prevent crash
2373ed2ee546 try to detect flac bitrate early on to avoid division by zero problems.
reimar
parents: 18074
diff changeset
482 sh_audio->i_bps = 64 * 1024;
25890
374816d4d565 Disable reading of flac metadata, mere metadata is not worth such a mess.
reimar
parents: 25889
diff changeset
483 // get_flac_metadata (demuxer);
11004
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents: 10891
diff changeset
484 break;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
485 }
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
486
19062
83c3afeab35d drops casts from void * on malloc/calloc from libmpdemux code
reynaldo
parents: 18710
diff changeset
487 priv = malloc(sizeof(da_priv_t));
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
488 priv->frmt = frmt;
24589
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
489 priv->next_pts = 0;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
490 demuxer->priv = priv;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
491 demuxer->audio->id = 0;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
492 demuxer->audio->sh = sh_audio;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
493 sh_audio->ds = demuxer->audio;
10244
a084597140af don't set audio pts to infinity after seeking in mp3 files :)
rfelker
parents: 9655
diff changeset
494 sh_audio->samplerate = sh_audio->audio.dwRate;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
495
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
496 if(stream_tell(s) != demuxer->movi_start)
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
497 {
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
498 mp_msg(MSGT_DEMUX, MSGL_V, "demux_audio: seeking from 0x%X to start pos 0x%X\n",
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
499 (int)stream_tell(s), (int)demuxer->movi_start);
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
500 stream_seek(s,demuxer->movi_start);
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
501 if (stream_tell(s) != demuxer->movi_start) {
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
502 mp_msg(MSGT_DEMUX, MSGL_V, "demux_audio: seeking failed, now at 0x%X!\n",
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
503 (int)stream_tell(s));
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
504 if (next_frame_pos) {
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
505 mp_msg(MSGT_DEMUX, MSGL_V, "demux_audio: seeking to 0x%X instead\n",
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
506 (int)next_frame_pos);
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
507 stream_seek(s, next_frame_pos);
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
508 }
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
509 }
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
510 }
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
511
13503
f8347c521898 better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents: 13357
diff changeset
512 mp_msg(MSGT_DEMUX,MSGL_V,"demux_audio: audio data 0x%X - 0x%X \n",(int)demuxer->movi_start,(int)demuxer->movi_end);
4712
9262f4dc332f mp3 position fixed
arpi
parents: 4700
diff changeset
513
16175
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
514 return DEMUXER_TYPE_AUDIO;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
515 }
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
516
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
517
16175
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
518 static int demux_audio_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds) {
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
519 int l;
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
520 demux_packet_t* dp;
24592
664366177d29 Simplify: initialize at declaration at the start of the function
reimar
parents: 24591
diff changeset
521 sh_audio_t* sh_audio = ds->sh;
664366177d29 Simplify: initialize at declaration at the start of the function
reimar
parents: 24591
diff changeset
522 demuxer_t* demux = ds->demuxer;
664366177d29 Simplify: initialize at declaration at the start of the function
reimar
parents: 24591
diff changeset
523 da_priv_t* priv = demux->priv;
664366177d29 Simplify: initialize at declaration at the start of the function
reimar
parents: 24591
diff changeset
524 double this_pts = priv->next_pts;
664366177d29 Simplify: initialize at declaration at the start of the function
reimar
parents: 24591
diff changeset
525 stream_t* s = demux->stream;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
526
16438
871e94131840 Ignore movi_end (except on error) to allow playing growing files.
reimar
parents: 16346
diff changeset
527 if(s->eof)
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
528 return 0;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
529
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
530 switch(priv->frmt) {
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
531 case MP3 :
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
532 while(1) {
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
533 uint8_t hdr[4];
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
534 stream_read(s,hdr,4);
16438
871e94131840 Ignore movi_end (except on error) to allow playing growing files.
reimar
parents: 16346
diff changeset
535 if (s->eof)
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
536 return 0;
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
537 l = mp_decode_mp3_header(hdr);
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
538 if(l < 0) {
16438
871e94131840 Ignore movi_end (except on error) to allow playing growing files.
reimar
parents: 16346
diff changeset
539 if (demux->movi_end && stream_tell(s) >= demux->movi_end)
871e94131840 Ignore movi_end (except on error) to allow playing growing files.
reimar
parents: 16346
diff changeset
540 return 0; // might be ID3 tag, i.e. EOF
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
541 stream_skip(s,-3);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
542 } else {
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
543 dp = new_demux_packet(l);
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
544 memcpy(dp->buffer,hdr,4);
16438
871e94131840 Ignore movi_end (except on error) to allow playing growing files.
reimar
parents: 16346
diff changeset
545 if (stream_read(s,dp->buffer + 4,l-4) != l-4)
17790
99a2481164ac fix leak on read error (forgot to free demux packet)
reimar
parents: 17775
diff changeset
546 {
99a2481164ac fix leak on read error (forgot to free demux packet)
reimar
parents: 17775
diff changeset
547 free_demux_packet(dp);
16438
871e94131840 Ignore movi_end (except on error) to allow playing growing files.
reimar
parents: 16346
diff changeset
548 return 0;
17790
99a2481164ac fix leak on read error (forgot to free demux packet)
reimar
parents: 17775
diff changeset
549 }
24589
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
550 priv->next_pts += sh_audio->audio.dwScale/(double)sh_audio->samplerate;
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
551 break;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
552 }
4700
30131db5dfbb Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents: 4694
diff changeset
553 } break;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
554 case WAV : {
17554
7084d2fbfea4 read data in nBlockAlign aligned chunks
reimar
parents: 17012
diff changeset
555 unsigned align = sh_audio->wf->nBlockAlign;
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
556 l = sh_audio->wf->nAvgBytesPerSec;
25772
cb331cd820d4 Fix endless loop if nAvgBytesPerSec is 0.
reimar
parents: 25771
diff changeset
557 if (l <= 0) l = 65536;
22103
0427f8190a12 Do not read beyond end of data chunk if chunk_size is set.
reimar
parents: 21651
diff changeset
558 if (demux->movi_end && l > demux->movi_end - stream_tell(s)) {
0427f8190a12 Do not read beyond end of data chunk if chunk_size is set.
reimar
parents: 21651
diff changeset
559 // do not read beyond end, there might be junk after data chunk
0427f8190a12 Do not read beyond end of data chunk if chunk_size is set.
reimar
parents: 21651
diff changeset
560 l = demux->movi_end - stream_tell(s);
0427f8190a12 Do not read beyond end of data chunk if chunk_size is set.
reimar
parents: 21651
diff changeset
561 if (l <= 0) return 0;
0427f8190a12 Do not read beyond end of data chunk if chunk_size is set.
reimar
parents: 21651
diff changeset
562 }
17554
7084d2fbfea4 read data in nBlockAlign aligned chunks
reimar
parents: 17012
diff changeset
563 if (align)
7084d2fbfea4 read data in nBlockAlign aligned chunks
reimar
parents: 17012
diff changeset
564 l = (l + align - 1) / align * align;
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
565 dp = new_demux_packet(l);
10891
65ed62e138fa Fixed 'reading after EOF'. demuxers didn't check, how many they've read!
lumag
parents: 10882
diff changeset
566 l = stream_read(s,dp->buffer,l);
24589
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
567 priv->next_pts += l/(double)sh_audio->i_bps;
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
568 break;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
569 }
11004
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents: 10891
diff changeset
570 case fLaC: {
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
571 l = 65535;
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
572 dp = new_demux_packet(l);
11004
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents: 10891
diff changeset
573 l = stream_read(s,dp->buffer,l);
24589
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
574 /* FLAC is not a constant-bitrate codec. These values will be wrong. */
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
575 priv->next_pts += l/(double)sh_audio->i_bps;
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
576 break;
11004
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents: 10891
diff changeset
577 }
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
578 default:
18176
f72bc5754209 Part3 of Otvos Attila's oattila AT chello-hu mp_msg changes, with lots of modifications as usual
reynaldo
parents: 18117
diff changeset
579 mp_msg(MSGT_DEMUXER,MSGL_WARN,MSGTR_MPDEMUX_AUDIO_UnknownFormat,priv->frmt);
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
580 return 0;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
581 }
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
582
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
583 resize_demux_packet(dp, l);
24589
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
584 dp->pts = this_pts;
14608
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
585 ds_add_packet(ds, dp);
9595f1de1393 Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents: 13503
diff changeset
586 return 1;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
587 }
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
588
4764
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
589 static void high_res_mp3_seek(demuxer_t *demuxer,float time) {
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
590 uint8_t hdr[4];
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
591 int len,nf;
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
592 da_priv_t* priv = demuxer->priv;
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
593 sh_audio_t* sh = (sh_audio_t*)demuxer->audio->sh;
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
594
15199
980910eb6f0c assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents: 14910
diff changeset
595 nf = time*sh->samplerate/sh->audio.dwScale;
4764
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
596 while(nf > 0) {
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
597 stream_read(demuxer->stream,hdr,4);
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
598 len = mp_decode_mp3_header(hdr);
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
599 if(len < 0) {
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
600 stream_skip(demuxer->stream,-3);
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
601 continue;
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
602 }
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
603 stream_skip(demuxer->stream,len-4);
24589
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
604 priv->next_pts += sh->audio.dwScale/(double)sh->samplerate;
4764
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
605 nf--;
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
606 }
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
607 }
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
608
17636
b849a99cdc3c Second-try commit of this patch.
corey
parents: 17554
diff changeset
609 static void demux_audio_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
610 sh_audio_t* sh_audio;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
611 stream_t* s;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
612 int base,pos;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
613 float len;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
614 da_priv_t* priv;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
615
4764
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
616 if(!(sh_audio = demuxer->audio->sh))
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
617 return;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
618 s = demuxer->stream;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
619 priv = demuxer->priv;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
620
25883
baf32110d3fc Use defines to give names to the different seek flags.
reimar
parents: 25772
diff changeset
621 if(priv->frmt == MP3 && hr_mp3_seek && !(flags & SEEK_FACTOR)) {
baf32110d3fc Use defines to give names to the different seek flags.
reimar
parents: 25772
diff changeset
622 len = (flags & SEEK_ABSOLUTE) ? rel_seek_secs - priv->next_pts : rel_seek_secs;
4764
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
623 if(len < 0) {
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
624 stream_seek(s,demuxer->movi_start);
24589
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
625 len = priv->next_pts + len;
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
626 priv->next_pts = 0;
4764
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
627 }
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
628 if(len > 0)
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
629 high_res_mp3_seek(demuxer,len);
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
630 return;
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
631 }
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
632
25883
baf32110d3fc Use defines to give names to the different seek flags.
reimar
parents: 25772
diff changeset
633 base = flags&SEEK_ABSOLUTE ? demuxer->movi_start : stream_tell(s);
baf32110d3fc Use defines to give names to the different seek flags.
reimar
parents: 25772
diff changeset
634 if(flags&SEEK_FACTOR)
5796
7a4b18b787c0 Fix % seeking
albeu
parents: 4764
diff changeset
635 pos = base + ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs);
7a4b18b787c0 Fix % seeking
albeu
parents: 4764
diff changeset
636 else
7a4b18b787c0 Fix % seeking
albeu
parents: 4764
diff changeset
637 pos = base + (rel_seek_secs*sh_audio->i_bps);
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
638
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
639 if(demuxer->movi_end && pos >= demuxer->movi_end) {
8359
a93461f7e5ef fixed mp3 seeking near end of file
arpi
parents: 8123
diff changeset
640 pos = demuxer->movi_end;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
641 } else if(pos < demuxer->movi_start)
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
642 pos = demuxer->movi_start;
4764
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
643
24589
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
644 priv->next_pts = (pos-demuxer->movi_start)/(double)sh_audio->i_bps;
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
645
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
646 switch(priv->frmt) {
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
647 case WAV:
16818
67ce689b5764 Fix seeking in wav files: align relative to start of data, not start of file
reimar
parents: 16438
diff changeset
648 pos -= (pos - demuxer->movi_start) %
67ce689b5764 Fix seeking in wav files: align relative to start of data, not start of file
reimar
parents: 16438
diff changeset
649 (sh_audio->wf->nBlockAlign ? sh_audio->wf->nBlockAlign :
67ce689b5764 Fix seeking in wav files: align relative to start of data, not start of file
reimar
parents: 16438
diff changeset
650 (sh_audio->channels * sh_audio->samplesize));
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
651 break;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
652 }
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
653
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
654 stream_seek(s,pos);
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents:
diff changeset
655 }
4764
9579496a91db Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents: 4720
diff changeset
656
16175
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
657 static void demux_close_audio(demuxer_t* demuxer) {
5812
f9d23c2aa6b7 Uninit added
albeu
parents: 5796
diff changeset
658 da_priv_t* priv = demuxer->priv;
f9d23c2aa6b7 Uninit added
albeu
parents: 5796
diff changeset
659
f9d23c2aa6b7 Uninit added
albeu
parents: 5796
diff changeset
660 if(!priv)
f9d23c2aa6b7 Uninit added
albeu
parents: 5796
diff changeset
661 return;
f9d23c2aa6b7 Uninit added
albeu
parents: 5796
diff changeset
662 free(priv);
f9d23c2aa6b7 Uninit added
albeu
parents: 5796
diff changeset
663 }
f9d23c2aa6b7 Uninit added
albeu
parents: 5796
diff changeset
664
16175
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
665 static int demux_audio_control(demuxer_t *demuxer,int cmd, void *arg){
10882
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
666 sh_audio_t *sh_audio=demuxer->audio->sh;
25771
f6c5b00976c6 Avoid a division by 0 if i_bps is 0.
reimar
parents: 25707
diff changeset
667 int audio_length = sh_audio->i_bps ? demuxer->movi_end / sh_audio->i_bps : 0;
10882
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
668 da_priv_t* priv = demuxer->priv;
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
669
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
670 switch(cmd) {
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
671 case DEMUXER_CTRL_GET_TIME_LENGTH:
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
672 if (audio_length<=0) return DEMUXER_CTRL_DONTKNOW;
16346
6ff303d2876b Make -identify's 'ID_LENGTH=' print a float and not an integer.. The
ods15
parents: 16175
diff changeset
673 *((double *)arg)=(double)audio_length;
10882
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
674 return DEMUXER_CTRL_GUESS;
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
675
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
676 case DEMUXER_CTRL_GET_PERCENT_POS:
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
677 if (audio_length<=0)
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
678 return DEMUXER_CTRL_DONTKNOW;
24589
9118be6575da demux_audio.c: Fix timestamp handling
uau
parents: 23628
diff changeset
679 *((int *)arg)=(int)( (priv->next_pts*100) / audio_length);
10882
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
680 return DEMUXER_CTRL_OK;
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
681
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
682 default:
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
683 return DEMUXER_CTRL_NOTIMPL;
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
684 }
e46e5a6c53b8 get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents: 10244
diff changeset
685 }
16175
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
686
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
687
25707
d4fe6e23283e Make all demuxer_desc_t const, thus moving them to .rodata
reimar
parents: 24592
diff changeset
688 const demuxer_desc_t demuxer_desc_audio = {
16175
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
689 "Audio demuxer",
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
690 "audio",
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
691 "Audio file",
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
692 "?",
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
693 "Audio only files",
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
694 DEMUXER_TYPE_AUDIO,
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
695 0, // unsafe autodetect
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
696 demux_audio_open,
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
697 demux_audio_fill_buffer,
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
698 NULL,
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
699 demux_close_audio,
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
700 demux_audio_seek,
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
701 demux_audio_control
6b86089c2edd Demuxer modularization
rtognimp
parents: 16162
diff changeset
702 };