1 /* Icecast
2  *
3  * This program is distributed under the GNU General Public License, version 2.
4  * A copy of this license is included with this source.
5  *
6  * Copyright 2009-2010,  Karl Heyes <karl@xiph.org>
7  * Copyright 2009-2018,  Karl Heyes <karl@kheyes.plus.com>
8  */
9 
10 /* mpeg.c
11  *
12  * routines to handle locating the frame sync markers for mpeg/1/2/3/aac streams.
13  *
14  */
15 #ifndef __MPEG_SYNC_H
16 #define __MPEG_SYNC_H
17 
18 #include "refbuf.h"
19 
20 struct mpeg_sync;
21 
22 typedef uint8_t frame_type_t;
23 
24 #define FORMAT_TYPE_UNDEFINED       0   /* No format determined */
25 #define FORMAT_TYPE_OGG             1
26 #define FORMAT_TYPE_AAC             2   // for AAC/ADTS style content
27 #define FORMAT_TYPE_MPEG            3   // for MPEG1/2/ADTS type content
28 #define FORMAT_TYPE_MP4             4
29 #define FORMAT_TYPE_EBML            5
30 #define FORMAT_TYPE_USAC            6   // USAC/LOAS framed aac
31 
32 
33 typedef struct sync_callback_t
34 {
35     void *callback_key;
36     int (*frame_callback)(struct mpeg_sync *mp, struct sync_callback_t *cb, unsigned char *p, unsigned int len, unsigned int offset);
37 } sync_callback_t;
38 
39 typedef struct mpeg_sync
40 {
41     uint64_t settings;
42 
43     uint32_t mask;
44     uint32_t match;
45 
46     unsigned short resync_count;
47     unsigned char marker;
48     frame_type_t type;
49 
50     uint32_t tag_len;
51     unsigned char *tag_data;
52 
53     uint64_t sample_count;
54 
55     int (*process_frame) (struct mpeg_sync *mp, sync_callback_t *cb, unsigned char *p, int len);
56 
57     refbuf_t *surplus;
58     const char *reference;
59 } mpeg_sync;
60 
61 
62 #define MPEG_LOG_MESSAGES   (1)
63 #define MPEG_SKIP_SYNC      (1<<2)
64 #define MPEG_KEEP_META      (1<<3)
65 #define MPEG_COPY_META      (1<<4)
66 
67 void mpeg_setup (mpeg_sync *mpsync, const char *mount);
68 void mpeg_cleanup (mpeg_sync *mpsync);
69 void mpeg_check_numframes (mpeg_sync *mpsync, unsigned count);
70 void mpeg_set_flags (mpeg_sync *mpsync, uint64_t flags);
71 frame_type_t mpeg_get_type (mpeg_sync *mp);
72 
73 int  mpeg_complete_frames_cb (mpeg_sync *mp, sync_callback_t *cb, refbuf_t *new_block, unsigned offset);
74 #define mpeg_complete_frames(A,B,C)         mpeg_complete_frames_cb(A,NULL,B,C)
75 
76 void mpeg_data_insert (mpeg_sync *mp, refbuf_t *inserted);
77 
78 int  mpeg_get_bitrate (struct mpeg_sync *mp);
79 int  mpeg_get_channels (struct mpeg_sync *mp);
80 int  mpeg_get_samplerate (struct mpeg_sync *mp);
81 int  mpeg_has_changed (struct mpeg_sync *mp);
82 int  mpeg_block_expanded (struct mpeg_sync *mp);
83 int  mpeg_tag_found (mpeg_sync *mp, const unsigned char **p, unsigned int *l);
84 
85 int syncframe_bitrate (mpeg_sync *mp);
86 int syncframe_channels (mpeg_sync *mp);
87 int syncframe_samplerate (mpeg_sync *mp);
88 
89 #endif /* __MPEG_SYNC_H */
90