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-2012,  Karl Heyes <karl@xiph.org>
7  * Copyright 2012-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 #ifdef HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18 
19 #include <fcntl.h>
20 #include <string.h>
21 #include <stdlib.h>
22 
23 #include "compat.h"
24 #include "mpeg.h"
25 #include "format_mp3.h"
26 #include "global.h"
27 
28 #define CATMODULE "mpeg"
29 #include "logging.h"
30 
31 int aacp_sample_freq[] = {
32     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 0, 0, 0, 0
33 };
34 
35 int aacp_num_channels[] = {
36     0, 1, 2, 3, 4, 5, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0
37 };
38 
39 int mpeg_samplerates [4][4] = {
40     { 11025, 0, 22050, 44100 },
41     { 12000, 0, 24000, 48000 },
42     {  8000, 0, 16000, 32000 },
43     { 0,0,0 } };
44 
45 // settings is a bitmask 64
46 // codec specific
47 // MP/2/3
48 //  bit 59, 58      version
49 //  bit 57, 56      layer
50 // AAC
51 //  bit 57, 56      layer
52 //
53 // bits 52-55 (4)   number of frames to check on autodetect
54 // bits 32-51 (20)  samplerate, may use 5 bits later by use of lookup table
55 // bits 24-31 (8)   channels
56 // bits 8-23 (16)   bitrate
57 //
58 // bit 6            check for TAG frame next, avoid format resync
59 // bit 5            resize block to sample_count size
60 // bit 4            last ID3 frame cached
61 // bit 3            allow trailing tags, eg from file
62 // bit 2            skip processing
63 // bit 1            change detected
64 // bit 0            log messages
65 
66 #define SYNC_CHANGED                    (1<<1)
67 #define SYNC_RESIZE                     (1<<5)
68 #define SYNC_CHK_TAG                    (1<<6)
69 
70 #define SYNC_BITRATE_OFF                8
71 #define SYNC_CHANNELS_OFF               24
72 #define SYNC_SAMPLERATE_OFF             32
73 #define SYNC_CHKFRAMES_OFF              52
74 
75 #define SYNC_BITRATE_MASK               ((uint64_t)0xFFFF << SYNC_BITRATE_OFF)
76 #define SYNC_CHAN_MASK                  ((uint64_t)0xFF << SYNC_CHANNELS_OFF)
77 #define SYNC_RATE_MASK                  ((uint64_t)0xFFFFF << SYNC_SAMPLERATE_OFF)
78 #define SYNC_CHKFRAME_MASK              ((uint64_t)0xF << SYNC_CHKFRAMES_OFF)
79 
80 #define syncframe_set_samplerate(x,v)   do {x->settings&=~SYNC_RATE_MASK; x->settings|=((uint64_t)(v)<<SYNC_SAMPLERATE_OFF); } while(0)
81 #define syncframe_set_channels(x,v)     do {x->settings&=~SYNC_CHAN_MASK; x->settings|=((uint64_t)(v)<<SYNC_CHANNELS_OFF); } while(0)
82 #define syncframe_set_framecheck(x,v)   do {x->settings&=~SYNC_CHKFRAME_MASK; x->settings|=((uint64_t)(v)<<SYNC_CHKFRAMES_OFF); } while(0)
83 #define syncframe_set_bitrate(x,v)      do {x->settings&=~SYNC_BITRATE_MASK; x->settings|=((uint64_t)(v)<<SYNC_BITRATE_OFF); } while(0)
84 
85 // mp/2/3/aac specific things
86 #define SYNC_MPEG_LAYER_OFF     56
87 #define SYNC_MPEG_VER_OFF       58
88 
89 #define MPEG_AAC                0
90 #define MPEG_LAYER_3            0x1
91 #define MPEG_LAYER_2            0x2
92 #define MPEG_LAYER_1            0x3
93 
94 #define MPEG_VER_1              0x3
95 #define MPEG_VER_2              0x2
96 #define MPEG_VER_25             0
97 
98 #define get_mpegframe_layer(p)          ((p[1] & 0x6) >> 1)
99 #define get_mpegframe_version(p)        ((p[1] & 0x18) >> 3)
100 
101 
syncframe_bitrate(mpeg_sync * mp)102 int syncframe_bitrate (mpeg_sync *mp)
103 {
104     return (int)((mp->settings&SYNC_BITRATE_MASK) >> SYNC_BITRATE_OFF);
105 }
106 
syncframe_channels(mpeg_sync * mp)107 int syncframe_channels (mpeg_sync *mp)
108 {
109     return (int)((mp->settings&SYNC_CHAN_MASK) >> SYNC_CHANNELS_OFF);
110 }
111 
syncframe_samplerate(mpeg_sync * mp)112 int syncframe_samplerate (mpeg_sync *mp)
113 {
114     return (int)((mp->settings&SYNC_RATE_MASK) >> SYNC_SAMPLERATE_OFF);
115 }
116 
syncframe_chkframes(mpeg_sync * mp)117 static int syncframe_chkframes (mpeg_sync *mp)
118 {
119     return (int)((mp->settings&SYNC_CHKFRAME_MASK) >> SYNC_CHKFRAMES_OFF);
120 }
121 
122 
mpeg_has_changed(struct mpeg_sync * mp)123 int mpeg_has_changed (struct mpeg_sync *mp)
124 {
125     int v = mp->settings & SYNC_CHANGED;
126     if (v) mp->settings &= ~SYNC_CHANGED; // reset
127     return v ? 1 : 0;
128 }
129 
mpeg_get_channels(struct mpeg_sync * mp)130 int mpeg_get_channels (struct mpeg_sync *mp)
131 {
132     return syncframe_channels (mp); // this could go
133 }
134 
mpeg_get_samplerate(struct mpeg_sync * mp)135 int mpeg_get_samplerate (struct mpeg_sync *mp)
136 {
137     return syncframe_samplerate (mp); // could go
138 }
139 
mpeg_get_bitrate(struct mpeg_sync * mp)140 int mpeg_get_bitrate (struct mpeg_sync *mp)
141 {
142     return syncframe_bitrate (mp) * 1000; // could go
143 }
144 
mpeg_get_version(struct mpeg_sync * mp)145 int mpeg_get_version (struct mpeg_sync *mp)
146 {
147     return (mp->settings >> SYNC_MPEG_VER_OFF) & 0x3;
148 }
149 
150 
mpeg_get_layer(struct mpeg_sync * mp)151 int mpeg_get_layer (struct mpeg_sync *mp)
152 {
153     return (mp->settings >> SYNC_MPEG_LAYER_OFF) & 0x3;
154 }
155 
mpeg_set_flags(mpeg_sync * mpsync,uint64_t flags)156 void mpeg_set_flags (mpeg_sync *mpsync, uint64_t flags)
157 {
158     mpsync->settings |= flags;
159 }
160 
161 
get_aac_frame_len(unsigned char * p)162 static int get_aac_frame_len (unsigned char *p)
163 {
164     return ((p[3] & 0x3) << 11) + (p[4] << 3) + ((p[5] & 0xE0) >> 5);
165 }
166 
handle_aac_frame(struct mpeg_sync * mp,sync_callback_t * cb,unsigned char * p,int len)167 static int handle_aac_frame (struct mpeg_sync *mp, sync_callback_t *cb, unsigned char *p, int len)
168 {
169     int frame_len = get_aac_frame_len (p);
170     int blocks, header_len = 9;
171     int samplerate_idx = (p[2] & 0x3C) >> 2, samplerate;
172     if (len - frame_len < 0)
173         return 0;
174 
175     samplerate = aacp_sample_freq [samplerate_idx];
176     int cur_rate = syncframe_samplerate (mp);
177     if (samplerate != cur_rate)
178     {
179         if (mp->settings & MPEG_LOG_MESSAGES)
180             WARN3 ("detected samplerate change from %d to %d on %s", cur_rate, samplerate, mp->reference);
181         syncframe_set_samplerate (mp, cur_rate);
182     }
183 
184     blocks = (p[6] & 0x3) + 1;
185     if (p[1] & 0x1) // no crc
186         header_len -= 2;
187     mp->sample_count = (blocks * 1024);
188 
189     if (cb && cb->frame_callback)
190         if (cb->frame_callback (mp, cb, p, frame_len, header_len) < 0)
191         {
192             mp->sample_count = 0;
193             return -1;
194         }
195 
196     return frame_len;
197 }
198 
get_mpegframe_samplerate(unsigned char * p)199 static int get_mpegframe_samplerate (unsigned char *p)
200 {
201     int ver = get_mpegframe_version (p);
202     return mpeg_samplerates [(p[2]&0xC) >> 2][ver];
203 }
204 
get_mpeg_bitrate(struct mpeg_sync * mp,unsigned char * p)205 static int get_mpeg_bitrate (struct mpeg_sync *mp, unsigned char *p)
206 {
207     int bitrate = -1;
208     int bitrate_code = (p[2] & 0xF0) >> 4;
209     int layer = get_mpegframe_layer (p);
210 
211     if (get_mpegframe_version (p) == MPEG_VER_1)
212     {
213         static int bitrates [3][16] = {
214             { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, -1 },
215             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 348, -1 },
216             { 0, 32, 54, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, -1 } };
217         if (layer != MPEG_AAC)
218             bitrate = bitrates [layer-1][bitrate_code];
219     }
220     else // MPEG v2/2.5
221     {
222         static int bitrates [2][16] = {
223             { 0,  8, 16, 24, 32, 40, 48,  56,  64,  80,  96, 112, 128, 144, 160, -1 },
224             { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, -1 } };
225         if (layer == MPEG_LAYER_1)
226             bitrate = bitrates [1][bitrate_code];
227         else
228             bitrate = bitrates [0][bitrate_code];
229     }
230     return bitrate;
231 }
232 
233 
get_samples_per_mpegframe(int version,int layer)234 static int get_samples_per_mpegframe (int version, int layer)
235 {
236     int samples_per_frame [4][4] = {
237         { -1,  576, 1152, 384 },    /* v2.5 - L3, L2, L1 */
238         { -1,   -1,   -1,  -1 },
239         { -1,  576, 1152, 384 },    /* v2 - L3, L2, L1 */
240         { -1, 1152, 1152, 576 }     /* v1 - L3, L2, L1 */
241     };
242     return samples_per_frame [version] [layer];
243 }
244 
245 
get_mpeg_frame_length(struct mpeg_sync * mp,unsigned char * p)246 static int get_mpeg_frame_length (struct mpeg_sync *mp, unsigned char *p)
247 {
248     int padding = (p[2] & 0x2) >> 1;
249     int frame_len = 0;
250 
251     int64_t bitrate = get_mpeg_bitrate (mp, p);
252     int layer = get_mpegframe_layer (p);
253     int samples = get_samples_per_mpegframe (get_mpegframe_version (p), layer);
254     int samplerate = get_mpegframe_samplerate (p);
255 
256     if (samplerate == 0 || (mp->mask && syncframe_samplerate (mp) != samplerate))
257         return -1;
258     mp->sample_count = samples;
259     if (bitrate > 0 && samples > 0)
260     {
261         if (mp->type != FORMAT_TYPE_MPEG)  // detection phase
262             syncframe_set_bitrate (mp, bitrate);
263 
264         bitrate *= 1000;
265         if (layer == MPEG_LAYER_1)
266         {
267             frame_len = (int)(12 * bitrate / samplerate + padding) * 4; // ??
268         }
269         else
270         {
271             frame_len = (int)(samples / 8 * bitrate / samplerate + padding);
272         }
273     }
274     return frame_len;
275 }
276 
277 
handle_mpeg_frame(struct mpeg_sync * mp,sync_callback_t * cb,unsigned char * p,int remaining)278 static int handle_mpeg_frame (struct mpeg_sync *mp, sync_callback_t *cb, unsigned char *p, int remaining)
279 {
280     int frame_len = get_mpeg_frame_length (mp, p);
281 
282     if (frame_len <= 0)
283     {
284         if (frame_len < 0)
285         {
286             int samplerate = get_mpegframe_samplerate (p);
287             if (samplerate)
288             {
289                 if (mp->settings & MPEG_LOG_MESSAGES)
290                     WARN2 ("detected samplerate change to %d on %s", samplerate, mp->reference);
291                 syncframe_set_samplerate (mp, samplerate);
292                 return handle_mpeg_frame (mp, cb, p, remaining);
293             }
294             if (mp->settings & MPEG_LOG_MESSAGES)
295                 INFO1 ("detected invalid frame on %s, skipping", mp->reference);
296         }
297         mp->sample_count = 0;
298         return -1;
299     }
300     if (remaining - frame_len < 0)
301         return 0;
302     if (cb && cb->frame_callback)
303         if (cb->frame_callback (mp, cb, p, frame_len, (p[1] & 0x1) ? 4 : 6) < 0)
304         {
305             mp->sample_count = 0;
306             return -1;
307         }
308     return frame_len;
309 }
310 
311 
handle_ts_frame(struct mpeg_sync * mp,sync_callback_t * cb,unsigned char * p,int remaining)312 static int handle_ts_frame (struct mpeg_sync *mp, sync_callback_t *cb, unsigned char *p, int remaining)
313 {
314     int frame_len = mp->sample_count;
315 
316     if (remaining - frame_len < 0)
317         return 0;
318     if (frame_len < remaining && p[frame_len] != 0x47)
319     {
320         if (mp->settings & MPEG_LOG_MESSAGES)
321             INFO1 ("missing frame marker from %s", mp->reference);
322         mp->sample_count = 0;
323         frame_len = -1;
324     }
325     return frame_len;
326 }
327 
getMSB4(unsigned long v)328 static unsigned long getMSB4 (unsigned long v)
329 {
330     unsigned long m = v;
331     int c = 0;
332 
333     for (; m > 15; c++, m >>= 1)
334         ;
335     //DEBUG1 ("bitrate estimate mark %lu", m);
336     if (m == 0xF)  // binary 1111 is not a normal bit pattern, adjust to 1000
337         m = 0x10;
338     if (m == 0xB)  // binary 1011 is not a normal bit pattern, adjust to 1100
339         m = 0xC;
340     return c ? m << c : m;
341 }
342 
343 
344 /* return -1 for no valid frame at this specified address, 0 for more data needed */
check_for_aac(struct mpeg_sync * mp,unsigned char * p,unsigned remaining)345 static int check_for_aac (struct mpeg_sync *mp, unsigned char *p, unsigned remaining)
346 {
347     //nocrc = p[1] & 0x1;
348     if (get_mpegframe_layer (p) == MPEG_AAC && (p[1] >= 0xF0) && (p[1] <= 0xF9))
349     {
350         int samplerate_idx = (p[2] & 0x3C) >> 2,
351             channels_idx = (((p[2] << 8) + p[3]) & 0x1C0) >> 6;
352         int id =  p[1] & 0x8;
353         int checking = syncframe_chkframes (mp), channels, samplerate, aac_frames = 0;
354         unsigned char *fh = p;
355         long aac_bytes = 0;
356 
357         while (1) // check as many frames in the block
358         {
359             //DEBUG1 ("checking aac frames %d", aac_frames);
360             int frame_len = get_aac_frame_len (fh);
361             if (frame_len <= 0 || frame_len > 8192)
362                 return -1;
363             if (frame_len+5 >= remaining)
364             {
365                 if (checking > 0) return 0;
366                 break;
367             }
368             if (fh[frame_len] != 255 || fh[frame_len+1] != p[1] || fh[frame_len+2] != p[2]
369                     || (fh[frame_len+3]&0xF0) != (p[3]&0xF0))
370                 return -1;
371             int blocks = (fh[6] & 0x3) + 1;
372             aac_frames += blocks;
373             aac_bytes += (frame_len - 7);
374             remaining -= frame_len;
375             fh += frame_len;
376             checking--;
377         }
378         // profile = p[1] & 0xC0;
379         samplerate = aacp_sample_freq [samplerate_idx];
380         channels = aacp_num_channels [channels_idx];
381         if (samplerate == 0 || channels == 0)
382         {
383             if (mp->settings & MPEG_LOG_MESSAGES)
384                 DEBUG0 ("ADTS samplerate/channel setting invalid");
385             return -1;
386         }
387         mp->marker = 0xFF;
388         mp->mask = 0xFFFEFDC0; // match these bits from the marker
389         syncframe_set_samplerate (mp, samplerate);
390         syncframe_set_channels (mp, channels);
391 
392         // get 4 most significant bits for nearest common bitrates.
393         long avg_bitrate = getMSB4 ((long)((aac_bytes * 1.028 / aac_frames) * (samplerate/1000.0)) * 8/1024);
394         syncframe_set_bitrate (mp, avg_bitrate);
395 
396         if (mp->settings & MPEG_LOG_MESSAGES)
397         {
398             char attrib[40];
399             snprintf (attrib, sizeof attrib, "%dHz %d channel(s) %ld kbps", samplerate, channels, avg_bitrate);
400             INFO3 ("Detected AAC MPEG-%s, %s on %s", id ? "2" : "4", attrib, mp->reference);
401         }
402         mp->process_frame = handle_aac_frame;
403         mp->type = FORMAT_TYPE_AAC;
404         mp->settings |= SYNC_CHANGED;
405         return 1;
406     }
407     return -1;
408 }
409 
check_for_mp3(struct mpeg_sync * mp,unsigned char * p,unsigned remaining)410 static int check_for_mp3 (struct mpeg_sync *mp, unsigned char *p, unsigned remaining)
411 {
412     int layer = get_mpegframe_layer (p);
413     if (layer != MPEG_AAC && (p[1] >= 0xE0))
414     {
415         const char *version[] = { "MPEG 2.5", NULL, "MPEG 2", "MPEG 1" };
416         const char *layer_names[] = { NULL, "Layer 3", "Layer 2", "Layer 1" };
417         int ver_id = get_mpegframe_version (p);
418         if (version [ver_id] && layer_names [layer])
419         {
420             int checking = syncframe_chkframes (mp), samplerate, channels = 2, frames = 0;
421             unsigned char *fh = p;
422             unsigned long bitrate_acc = 0;
423 
424             // au.crc = (p[1] & 0x1) == 0;
425             mp->settings |= ((uint64_t)ver_id << SYNC_MPEG_VER_OFF);
426             samplerate = get_mpegframe_samplerate (p);
427             if (samplerate == 0)
428                 return -1;
429             syncframe_set_samplerate (mp, samplerate);
430             do
431             {
432                 int frame_len;
433 
434                 if (remaining <= 4)
435                     return 0;
436                 if (fh [0] != 255 || fh [1] != p[1])
437                     return -1;
438                 frame_len = get_mpeg_frame_length (mp, fh);
439                 if (frame_len <= 0 || frame_len > 3000)
440                 {
441                     //DEBUG2 ("checking frame %d, but len %d invalid", 5-checking, frame_len);
442                     return -1;
443                 }
444                 if (frame_len > remaining)
445                 {
446                     //DEBUG3 ("checking frame %d, but need more data (%d,%d)", 5-checking, frame_len, remaining);
447                     return 0;
448                 }
449                 if (samplerate != get_mpegframe_samplerate (fh))
450                     return -1;
451                 //DEBUG4 ("frame %d checked, next header codes are %x %x %x", 5-checking, fh[frame_len], fh[frame_len+1], fh[frame_len+2]);
452                 frames++;
453                 bitrate_acc += syncframe_bitrate (mp);
454                 remaining -= frame_len;
455                 fh += frame_len;
456             } while (--checking);
457             if  (((p[3] & 0xC0) >> 6) == 3)
458                 channels = 1;
459             mp->marker = 0xFF;
460             mp->mask = 0xFFFE0000;
461 
462             long avg_bitrate = bitrate_acc / frames;
463             syncframe_set_bitrate (mp, avg_bitrate);
464             if (mp->settings & MPEG_LOG_MESSAGES)
465             {
466                 char stream_attrib[30];
467                 snprintf (stream_attrib, sizeof (stream_attrib), "%d %d %ld kbps", samplerate, channels, avg_bitrate);
468 
469                 INFO4 ("%s %s Detected (%s) on %s", version [ver_id], layer_names[layer], stream_attrib, mp->reference);
470             }
471             syncframe_set_channels (mp, channels);
472             mp->type = FORMAT_TYPE_MPEG;
473             mp->process_frame = handle_mpeg_frame;
474             mp->settings |= SYNC_CHANGED;
475             return 1;
476         }
477     }
478     return -1;
479 }
480 
481 
handle_usac_frame(struct mpeg_sync * mp,sync_callback_t * cb,unsigned char * p,int remaining)482 static int handle_usac_frame (struct mpeg_sync *mp, sync_callback_t *cb, unsigned char *p, int remaining)
483 {
484     int frame_len = 3 + (((p[1]&0x1F) << 8) | p[2]);
485 
486     if (remaining - frame_len < 0)
487         return 0;
488     if (frame_len < remaining && p[frame_len] != 0x56)
489     {
490         INFO1 ("missing frame marker from %s", mp->reference);
491         mp->sample_count = 0;
492         frame_len = -1;
493     }
494     return frame_len;
495 }
496 
497 
check_for_usac(struct mpeg_sync * mp,unsigned char * p,unsigned remaining)498 static int check_for_usac (struct mpeg_sync *mp, unsigned char *p, unsigned remaining)
499 {
500     int checking = syncframe_chkframes (mp), offset = 0;
501     while (checking)
502     {
503         checking--;
504         if (offset+2 > remaining) return 0;
505         if (p[offset] == 0x56 && (p[offset+1] & 0xE0) == 0xE0)
506         {
507             int len = ((p[offset+1]&0x1F) << 8) | p[offset+2];
508             offset += (len + 3);
509             continue;
510         }
511         return -1;
512     }
513     INFO1 ("Detected USAC/LOAS on %s", mp->reference);
514     mp->process_frame = handle_usac_frame;
515     mp->type = FORMAT_TYPE_USAC;
516     mp->mask = 0xFFE00000;
517     mp->marker = 0x56;
518     mp->settings |= SYNC_CHANGED;
519     return 1;
520 }
521 
522 
check_for_ts(struct mpeg_sync * mp,unsigned char * p,unsigned remaining)523 static int check_for_ts (struct mpeg_sync *mp, unsigned char *p, unsigned remaining)
524 {
525     int pkt_len = 188, checking;
526     do
527     {
528         int offset = 0;
529         checking = 4;
530         while (checking)
531         {
532             if (offset > remaining) return 0;
533             if (p [offset] != 0x47)
534             {
535                 switch (pkt_len) {
536                     case 204: pkt_len = 208; break;
537                     case 188: pkt_len = 204; break;
538                     default:  return -1;
539                 }
540                 break;
541             }
542             //DEBUG2 ("found 0x37 checking %d (%d)", checking, pkt_len);
543             offset += pkt_len;
544             checking--;
545         }
546     } while (checking);
547     if (mp->settings & MPEG_LOG_MESSAGES)
548         INFO2 ("Detected TS (%d) on %s", pkt_len, mp->reference);
549     mp->process_frame = handle_ts_frame;
550     mp->mask = 0xFF000000;
551     mp->marker = 0x47;
552     mp->sample_count = pkt_len;
553     mp->settings |= MPEG_SKIP_SYNC | SYNC_CHANGED;
554     return 1;
555 }
556 
557 
558 // this is only really called once, we need to return a length but reset for frame check
559 //
handle_id3_frame(struct mpeg_sync * mp,sync_callback_t * cb,unsigned char * p,int remaining)560 static int handle_id3_frame (struct mpeg_sync *mp, sync_callback_t *cb, unsigned char *p, int remaining)
561 {
562     int frame_len = mp->sample_count;
563 
564     if (remaining < frame_len)
565         return 0;
566     if (mp->settings & MPEG_COPY_META)
567     {
568         DEBUG2 ("caching ID3 frame of %d on %s", frame_len, mp->reference);
569         free (mp->tag_data);
570         mp->tag_data = malloc (frame_len);
571         mp->tag_len = frame_len;
572         memcpy (mp->tag_data, p, frame_len);
573         mp->settings |= SYNC_CHANGED;
574     }
575     if (mp->settings & MPEG_KEEP_META)
576     {
577         mp->mask = 0;
578         return frame_len;
579     }
580     return -1;
581 }
582 
check_for_id3(struct mpeg_sync * mp,unsigned char * p,unsigned remaining)583 static int check_for_id3 (struct mpeg_sync *mp, unsigned char *p, unsigned remaining)
584 {
585     int ret = 0;
586 
587     do
588     {
589         if (remaining < 16) break;
590 
591         ret = 1;
592         int set_callback = (mp->mask) ? 0 : 1;
593 
594         if (memcmp (p, "APETAGEX", 8) == 0)
595         {
596             unsigned int ver = p[8], len = p[12];
597 
598             ver += (p[9] << 8);
599             ver += (p[10] << 16);
600             ver += (p[11] << 24);
601             ver /= 1000;
602 
603             len += (p[13] << 8);
604             len += (p[14] << 16);
605             len += (p[15] << 24);
606             len += 32;
607 
608             if (len > remaining)
609                 return 0;
610             if (mp->settings & MPEG_KEEP_META)
611             {
612                 if (set_callback)
613                 {
614                     mp->process_frame = handle_id3_frame;
615                     mp->mask = 0xFF000000;
616                     mp->marker = 0x41;  // match the 'A'
617                 }
618                 mp->sample_count = len;
619                 if (mp->settings & MPEG_LOG_MESSAGES)
620                     DEBUG2 ("Detected APETAG v%u, length %u", ver, len);
621             }
622             else
623             {
624                 if (mp->settings & MPEG_LOG_MESSAGES)
625                     DEBUG2 ("Detected APETAG v%u, skipping %u bytes", ver, len);
626                 memset (p, 0, len);
627             }
628             break;
629         }
630         if (memcmp (p, "TAG", 3) == 0)
631         {
632             if (remaining < 128)
633                 return 0;       // placed at the end of files.
634             if (mp->settings & MPEG_KEEP_META)
635             {
636                 mp->process_frame = handle_id3_frame;
637                 mp->mask = 0xFF000000;
638                 mp->marker = 0x54;  // match the 'T'
639                 mp->sample_count = 128;
640                 if (mp->settings & MPEG_LOG_MESSAGES)
641                     INFO0 ("Detected ID3v1, keeping");
642             }
643             else
644             {
645                 if (mp->settings & MPEG_LOG_MESSAGES)
646                     INFO0 ("Detected ID3v1, skipping 128 bytes");
647                 memset (p, 0, 128);
648             }
649             break;
650         }
651         if (memcmp (p, "ID3", 3) == 0)
652         {
653             if (p[3] < 0xFF && p[4] < 0xFF && (p[5] & 0xF) == 0)
654             {
655                 int ver = p[3], rev = p[4];
656                 size_t size = (p[6] & 0x7f);
657                 size = (size << 7) + (p[7] & 0x7f);
658                 size = (size << 7) + (p[8] & 0x7f);
659                 size = (size << 7) + (p[9] & 0x7f);
660 
661                 mp->sample_count = size + 10;
662                 if (size > remaining)
663                 {
664                     mp->settings |= SYNC_RESIZE;
665                     break;      // trigger a recheck
666                 }
667                 if (mp->settings & MPEG_LOG_MESSAGES)
668                     INFO4 ("Detected ID3v2 (%d.%d), tag size %" PRIu64 " on %s", ver, rev, (uint64_t)size, mp->reference);
669                 if (set_callback)
670                 {
671                     mp->process_frame = handle_id3_frame;
672                     mp->mask = 0xFF000000;
673                     mp->marker = 0x49;      // match the 'I'
674                 }
675                 break;
676             }
677         }
678         ret = -1;
679     } while (0);
680     return ret;
681 }
682 
683 
make_val32(unsigned char * p)684 static unsigned long make_val32 (unsigned char *p)
685 {
686     unsigned long v = *p;
687     int idx = 1;
688 
689     for (; idx < 4; idx++)
690     {
691         v <<= 8;
692         v += p [idx];
693     }
694     return v;
695 }
696 
697 
698 /* return -1 for no valid frame at this specified address, 0 for more data needed */
get_initial_frame(struct mpeg_sync * mp,unsigned char * p,unsigned remaining)699 static int get_initial_frame (struct mpeg_sync *mp, unsigned char *p, unsigned remaining)
700 {
701     int ret = -1;
702 
703     if (mp->settings & MPEG_SKIP_SYNC)
704         return 2; // we should skip processing
705 
706     // reset all but external options
707     mp->settings &= (MPEG_LOG_MESSAGES|MPEG_KEEP_META|MPEG_COPY_META|SYNC_CHKFRAME_MASK);
708 
709     mp->type = FORMAT_TYPE_UNDEFINED;
710 
711     if (p[0] == 'I' || p[0] == 'T' || p[0] == 'A')
712        ret = check_for_id3 (mp, p, remaining);
713     if (memcmp (p, "\x1A\x45\xDF\xA3", 4) == 0)
714     {
715         if (mp->settings & MPEG_LOG_MESSAGES)
716             INFO0 ("Detected Matroska, skipping");
717         mp->settings |= MPEG_SKIP_SYNC;
718         mp->type = FORMAT_TYPE_EBML;
719         return 1;
720     }
721     if (memcmp (p, "OggS", 4) == 0)
722     {
723         if (mp->settings & MPEG_LOG_MESSAGES)
724             INFO0 ("Detected possible Ogg page, skipping");
725         mp->settings |= MPEG_SKIP_SYNC;
726         mp->type = FORMAT_TYPE_OGG;
727         return -1;
728     }
729     if (ret < 0 && p[0] == 0x47)
730         ret = check_for_ts (mp, p, remaining);
731     if (ret < 0 && p[1] >= 0xE0)
732     {
733         mp->settings |= ((uint64_t)get_mpegframe_layer (p) << SYNC_MPEG_LAYER_OFF); // layer setting
734         ret = check_for_aac (mp, p, remaining);
735         if (ret < 0)
736             ret = check_for_mp3 (mp, p, remaining);
737     }
738     if (ret < 0 && p[0] == 0x56 && (p[1]&0xE0) == 0xE0)
739         ret = check_for_usac(mp, p, remaining);
740     if (ret > 0)
741     {
742         mp->resync_count = 0;
743         mp->match = make_val32 (p) & mp->mask;
744     }
745     return ret;
746 }
747 
748 
749 
match_syncbits(mpeg_sync * mp,unsigned char * p,unsigned remaining)750 static int match_syncbits (mpeg_sync *mp, unsigned char *p, unsigned remaining)
751 {
752     unsigned long v = make_val32 (p);
753 
754     if ((v & mp->mask) != mp->match)
755     {
756         int ret = check_for_id3 (mp, p, remaining);
757         if (ret < 0)
758             return -1;
759         mp->settings |= SYNC_CHK_TAG;
760     }
761     return 0;
762 }
763 
764 
765 /* return number from 0 to remaining */
find_align_sync(mpeg_sync * mp,unsigned char * start,int remaining,int prevent_move)766 static int find_align_sync (mpeg_sync *mp, unsigned char *start, int remaining, int prevent_move)
767 {
768     int skip = remaining, singlebyte = mp->mask & 0xFFFFFF ? 0 : 1;
769     unsigned char *p = NULL;
770 
771     if (mp->mask)
772     {
773         unsigned char *s = start;
774         int r = remaining;
775 
776         do
777         {
778             if (r < 9) break;
779             if (memcmp (s, "TAG", 3) == 0 || memcmp (s, "ID3", 3) == 0 || memcmp (s, "APETAGEX", 8) == 0)
780             {
781                 if (mp->settings & MPEG_LOG_MESSAGES)
782                     INFO1 ("Detected \"%.3s\" midstream", s);
783                 break;
784             }
785             p = start;
786             while (r && (p = memchr (s, mp->marker, r)))
787             {
788                 if (singlebyte)
789                     break;
790                 r = remaining - (p - start);
791                 if (r < 4)
792                     break;
793                 if (match_syncbits (mp, p, remaining) == 0)
794                     break;
795                 s = p+1;
796                 r--;
797             }
798         } while (0);
799         if (p == NULL)
800             mp->mask = 0;
801     }
802     if (p == NULL)
803     {
804         p = start;
805         if (remaining >= 8 && memcmp (p+4, "ftyp", 4) == 0)
806         {
807             mp->settings |= MPEG_SKIP_SYNC; // mp4 looks to be here, lets skip parsing
808             mp->type = FORMAT_TYPE_MP4;
809             return 0;
810         }
811         if (remaining >= 4 && memcmp (p, "\x1A\x45\xDF\xA3", 4) == 0)
812         {
813             mp->settings |= MPEG_SKIP_SYNC; // matroska looks to be here, lets skip parsing
814             mp->type = FORMAT_TYPE_EBML;
815             return 0;
816         }
817         else
818         {
819             int offset = remaining;
820             do {
821                 if (offset < 3) break;
822                 if (*p == 0x47) break;   // MPEG TS
823                 if (*p == 0x56)
824                     if ((p[1] & 0xE0) == 0xE0) break; // USAC
825                 if (*p == 0xFF)
826                     if (p[1] != 0xFF || p[2] <= 0xFB) break;
827                 if (offset > 3 && memcmp (p, "OggS", 4) == 0)
828                     break;
829                 if (memcmp (p, "ID3", 3) == 0 || memcmp (p, "TAG", 3) == 0)
830                     break;
831                 if (offset > 7 && memcmp (p, "APETAGEX", 8) == 0)
832                     break;
833                 p++;
834                 offset--;
835             } while (1);
836             if (offset == 0) p = NULL;
837         }
838     }
839     if (p)
840     {
841         skip = p - start;
842         remaining -= skip;
843         if (remaining < 20000 && prevent_move == 0 && skip)
844             memmove (start, p, remaining);
845         mp->resync_count += skip;
846     }
847     return skip;
848 }
849 
850 
mpeg_complete_frames_cb(mpeg_sync * mp,sync_callback_t * cb,refbuf_t * new_block,unsigned offset)851 int mpeg_complete_frames_cb (mpeg_sync *mp, sync_callback_t *cb, refbuf_t *new_block, unsigned offset)
852 {
853     unsigned char *start, *end;
854     int remaining, frame_len = 0, ret, loop = 50;
855     unsigned long samples = 0;
856 
857     if (mp == NULL || (mp->settings & MPEG_SKIP_SYNC))
858         return 0;  /* leave as-is */
859 
860     mp->settings &= ~SYNC_RESIZE;
861     mp->sample_count = 0;
862     if (offset == 0)
863     {
864         if (new_block->flags&REFBUF_SHARED)
865         {
866             if (syncframe_chkframes (mp) > 1)
867                 syncframe_set_framecheck (mp, 1);
868         }
869         else
870             if (syncframe_chkframes (mp) <= 1)
871                 syncframe_set_framecheck (mp, 4);
872     }
873     if (mp->surplus)
874     {
875         if (offset >= mp->surplus->len)
876             offset -= mp->surplus->len;
877         else
878         {
879             int new_len = mp->surplus->len + new_block->len;
880             unsigned char *p = realloc (mp->surplus->data, new_len);
881 
882             memcpy (p+mp->surplus->len, new_block->data, new_block->len);
883             mp->surplus->data = new_block->data;
884             new_block->data = (void*)p;
885             new_block->len = new_len;
886         }
887         refbuf_release (mp->surplus);
888         mp->surplus = NULL;
889     }
890     start = (unsigned char *)new_block->data + offset;
891     while (loop)
892     {
893         end = (unsigned char*)new_block->data + new_block->len;
894         remaining = end - start;
895         //DEBUG2 ("block size %d, remaining now %d", new_block->len, remaining);
896         if (remaining < 10) /* make sure we have some bytes to check */
897             break;
898         if (mp->mask && match_syncbits (mp, start, remaining) == 0)
899         {
900             if (mp->settings & SYNC_CHK_TAG)
901             {
902                 // tags can be injected midstream, so avoid a full resync as they would be only a frame, but handle
903                 // the possible resizing of the block.
904                 if (mp->settings & SYNC_RESIZE)
905                 {
906                     unsigned old_len = new_block->len;
907                     unsigned new_len = old_len - remaining + (mp->sample_count ? mp->sample_count : 5000);
908                     unsigned char *p = realloc (new_block->data, new_len);
909                     new_block->data = (void*)p;
910                     new_block->len = new_len;
911                     mp->settings &= ~SYNC_CHK_TAG;
912                     return old_len;
913                 }
914                 frame_len = handle_id3_frame (mp, cb, start, remaining);
915             }
916             else
917                 frame_len = mp->process_frame (mp, cb, start, remaining);
918             if (frame_len == 0)
919                 break;
920             if (frame_len > 0)
921             {
922                 samples += mp->sample_count;
923                 start += frame_len;
924                 mp->resync_count = 0;
925                 mp->settings &= ~SYNC_CHK_TAG;
926                 continue;
927             }
928             if ((mp->settings & SYNC_CHK_TAG) == 0)
929             {
930                 if (mp->sample_count == 0 || remaining < mp->sample_count)
931                     mp->sample_count = 1;
932                 mp->mask = 0;
933             }
934             if ((new_block->flags & REFBUF_SHARED) == 0)
935             {
936                 memmove (start, start+mp->sample_count, remaining-mp->sample_count);
937                 new_block->len -= mp->sample_count;
938             }
939             else
940                 start += mp->sample_count;
941             mp->sample_count = 0;
942             mp->settings &= ~SYNC_CHK_TAG;
943             continue;
944         }
945 
946         // no frame header match, let look elsewhere.
947         ret = find_align_sync (mp, start, remaining, new_block->flags&REFBUF_SHARED);
948         if (ret)
949         {
950             if (ret == remaining && ret > 800)
951                mp->mask = 0;
952             if (mp->resync_count > 100000)
953             {
954                 if (mp->settings & MPEG_LOG_MESSAGES)
955                     INFO1 ("no frame sync after 100k on %s", mp->reference);
956                 mp->settings |= MPEG_SKIP_SYNC; // lets skip parsing
957                 mp->type = FORMAT_TYPE_UNDEFINED;
958                 return 0;
959             }
960             if ((new_block->flags & REFBUF_SHARED) == 0)
961             {
962                 if (mp->settings & MPEG_LOG_MESSAGES)
963                     DEBUG3 ("no frame sync on %s, re-checking after skipping %d (%d)", mp->reference, ret, new_block->len);
964                 new_block->len -= ret;
965             }
966             samples = 0;
967             continue;
968         }
969         if (mp->mask == 0)
970         {
971             int ret = get_initial_frame (mp, start, remaining);
972             if (ret < 0)
973             {
974                 // failed to detect a complete frame, try another search
975                 *start = 0;
976                 mp->settings &= ~MPEG_SKIP_SYNC;
977                 continue;
978             }
979             if (ret == 0)
980             {
981                 if (remaining > 100000)
982                     return -1;
983                 if ((new_block->flags & REFBUF_SHARED) == 0 && (mp->settings & SYNC_RESIZE))
984                 {
985                     unsigned new_len = mp->sample_count ? mp->sample_count : new_block->len + 5000;
986                     unsigned char *p = realloc (new_block->data, new_len);
987                     new_block->data = (void*)p;
988                     new_block->len = new_len;
989                 }
990                 else
991                     new_block->len = offset;
992                 return remaining;
993             }
994             if (ret > 1) // detected case but avoid parsing
995                 return 0;
996             if ((new_block->flags & REFBUF_SHARED) == 0)
997             {
998                 if ((mp->settings & SYNC_RESIZE) && mp->sample_count < 2000000)
999                 {
1000                     unsigned new_len = mp->sample_count + (new_block->len - remaining);
1001                     unsigned char *p = realloc (new_block->data, new_len);
1002 
1003                     new_block->data = (void*)p;
1004                     new_block->len = new_len;
1005                     return remaining;
1006                 }
1007             }
1008         }
1009         loop--;
1010     }
1011     if (remaining < 0 || remaining > new_block->len)
1012     {
1013         if (mp->settings & MPEG_LOG_MESSAGES)
1014             ERROR2 ("block inconsistency (%d, %d)", remaining, new_block->len);
1015         abort();
1016     }
1017     if (remaining && (new_block->flags & REFBUF_SHARED) == 0)
1018         new_block->len -= remaining;
1019     mp->sample_count = samples;
1020     return remaining;
1021 }
1022 
1023 
mpeg_tag_found(mpeg_sync * mp,const unsigned char ** p,unsigned int * l)1024 int mpeg_tag_found (mpeg_sync *mp, const unsigned char **p, unsigned int *l)
1025 {
1026     int r = -1;
1027     if (p && l)
1028     {
1029        *p = mp->tag_data;
1030        *l = mp->tag_len;
1031        r = 0;
1032     }
1033     return r;
1034 }
1035 
1036 
mpeg_block_expanded(mpeg_sync * mp)1037 int mpeg_block_expanded (mpeg_sync *mp)
1038 {
1039     return (mp && (mp->settings & SYNC_RESIZE)) ? 1 : 0;
1040 }
1041 
mpeg_data_insert(mpeg_sync * mp,refbuf_t * inserted)1042 void mpeg_data_insert (mpeg_sync *mp, refbuf_t *inserted)
1043 {
1044     if (mp)
1045         mp->surplus = inserted;
1046 }
1047 
mpeg_setup(mpeg_sync * mpsync,const char * reference)1048 void mpeg_setup (mpeg_sync *mpsync, const char *reference)
1049 {
1050     memset (mpsync, 0, sizeof (mpeg_sync));
1051     syncframe_set_framecheck (mpsync, 4);
1052     mpsync->settings |= MPEG_LOG_MESSAGES;
1053     mpsync->reference = reference;
1054 }
1055 
mpeg_get_type(struct mpeg_sync * mp)1056 frame_type_t mpeg_get_type (struct mpeg_sync *mp)
1057 {
1058     return mp->type;
1059 }
1060 
mpeg_check_numframes(mpeg_sync * mpsync,unsigned count)1061 void mpeg_check_numframes (mpeg_sync *mpsync, unsigned count)
1062 {
1063     if (count && count < 100)
1064         syncframe_set_framecheck (mpsync, count);
1065     if (count == 1)  // client processing, reduce heavy logging
1066         mpsync->settings &= ~MPEG_LOG_MESSAGES;
1067 }
1068 
mpeg_cleanup(mpeg_sync * mpsync)1069 void mpeg_cleanup (mpeg_sync *mpsync)
1070 {
1071     if (mpsync)
1072     {
1073         free (mpsync->tag_data);
1074         refbuf_release (mpsync->surplus);
1075         mpsync->reference = NULL;
1076     }
1077 }
1078