1 /*                                                     -*- linux-c -*-
2     Copyright (C) 2005 Tom Szilagyi
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18     $Id: dec_mpeg.h 1286 2014-04-27 20:50:41Z tszilagyi $
19 */
20 
21 #ifndef AQUALUNG_DEC_MPEG_H
22 #define AQUALUNG_DEC_MPEG_H
23 
24 #include <stdio.h>
25 #include <sys/stat.h>
26 #include <mad.h>
27 
28 #include "../athread.h"
29 #include "../httpc.h"
30 #include "../rb.h"
31 #include "file_decoder.h"
32 
33 
34 /* size of ringbuffer for decoded MPEG Audio data (in frames) */
35 #define RB_MAD_SIZE 262144
36 
37 /* for stream (non-mmap) decoding */
38 #define MPEG_INBUF_SIZE (5*8192)
39 
40 #define MPEG_VERSION1   0
41 #define MPEG_VERSION2   1
42 #define MPEG_VERSION2_5 2
43 
44 /* MPEG Audio subformats */
45 #define MPEG_LAYER_I     0x001
46 #define MPEG_LAYER_II    0x002
47 #define MPEG_LAYER_III   0x004
48 #define MPEG_LAYER_MASK  0x007
49 #define MPEG_MODE_SINGLE 0x010
50 #define MPEG_MODE_DUAL   0x020
51 #define MPEG_MODE_JOINT  0x040
52 #define MPEG_MODE_STEREO 0x080
53 #define MPEG_MODE_MASK   0x0F0
54 #define MPEG_EMPH_NONE   0x100
55 #define MPEG_EMPH_5015   0x200
56 #define MPEG_EMPH_J_17   0x400
57 #define MPEG_EMPH_RES    0x800
58 #define MPEG_EMPH_MASK   0xF00
59 
60 
61 typedef struct {
62 	/* Standard MP3 frame header fields */
63 	int version;
64 	int layer;
65 	int protection;
66 	int bitrate;
67 	long frequency;
68 	int padding;
69 	int channel_mode;
70 	int mode_extension;
71 	int emphasis;
72 	int frame_size;   /* Frame size in bytes */
73 	int frame_samples; /* Samples per frame */
74 	int ft_num;       /* Numerator of frametime in milliseconds */
75 	int ft_den;       /* Denominator of frametime in milliseconds */
76 
77 	int is_vbr;      /* True if the file is VBR */
78 	int has_toc;     /* True if there is a VBR header in the file */
79 	int is_xing_vbr; /* True if the VBR header is of Xing type */
80 	int is_vbri_vbr; /* True if the VBR header is of VBRI type */
81 	unsigned char toc[100];
82 	unsigned long frame_count; /* Number of frames in the file (if VBR) */
83 	unsigned long byte_count;  /* File size in bytes */
84 	unsigned long file_time;   /* Length of the whole file in milliseconds */
85 	unsigned long vbr_header_pos;
86 	int enc_delay;    /* Encoder delay, fetched from LAME header */
87 	int enc_padding;  /* Padded samples added to last frame. LAME header */
88 
89 	/* first valid(!) frame */
90 	unsigned long start_byteoffset;
91 } mp3info_t;
92 
93 /* Xing header information */
94 #define VBR_FRAMES_FLAG  0x01
95 #define VBR_BYTES_FLAG   0x02
96 #define VBR_TOC_FLAG     0x04
97 #define VBR_QUALITY_FLAG 0x08
98 
99 #define MAX_XING_HEADER_SIZE 576
100 
101 unsigned long find_next_frame(int fd, long *offset, long max_offset,
102                               unsigned long last_header, int is_ubr_allowed);
103 
104 typedef struct {
105 	int frame; /* number of mpeg frame */
106 	unsigned long long sample; /* number of audio samples since beginning of file */
107 	unsigned long offset; /* byte offset from beginning of file */
108 } mpeg_seek_table_t;
109 
110 typedef struct _mpeg_pdata_t {
111         struct mad_decoder mpeg_decoder;
112         rb_t * rb;
113         FILE * mpeg_file;
114         int channels;
115         int SR;
116         unsigned bitrate;
117         int error;
118         int is_eos;
119         struct stat mpeg_stat;
120         long long int filesize;
121 	long skip_bytes;
122        	long delay_frames;
123         int fd;
124         void * fdm;
125         unsigned long total_samples_est;
126         int mpeg_subformat; /* used as v_minor */
127 	mp3info_t mp3info;
128 	int seek_table_built;
129 	AQUALUNG_THREAD_DECLARE(seek_builder_id)
130         int builder_thread_running;
131 	mpeg_seek_table_t seek_table[100];
132 	unsigned long frame_counter;
133 	long last_frames[2]; /* [0] is the last frame's byte offset, [1] the last-but-one */
134 
135 	/* for stream decoding */
136 	http_session_t * session;
137 	unsigned char * inbuf;
138 
139         struct mad_stream mpeg_stream;
140         struct mad_frame mpeg_frame;
141         struct mad_synth mpeg_synth;
142 } mpeg_pdata_t;
143 
144 
145 decoder_t * mpeg_decoder_init(file_decoder_t * fdec);
146 void mpeg_decoder_destroy(decoder_t * dec);
147 int mpeg_decoder_open(decoder_t * dec, char * filename);
148 void mpeg_decoder_send_metadata(decoder_t * dec);
149 int mpeg_stream_decoder_open(decoder_t * dec, http_session_t * session);
150 void mpeg_decoder_close(decoder_t * dec);
151 unsigned int mpeg_decoder_read(decoder_t * dec, float * dest, int num);
152 void mpeg_decoder_seek(decoder_t * dec, unsigned long long seek_to_pos);
153 
154 
155 #endif /* AQUALUNG_DEC_MPEG_H */
156 
157 // vim: shiftwidth=8:tabstop=8:softtabstop=8 :
158