1 /*
2    Copyright (C) 2003 Commonwealth Scientific and Industrial Research
3    Organisation (CSIRO) Australia
4 
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8 
9    - Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11 
12    - Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15 
16    - Neither the name of CSIRO Australia nor the names of its
17    contributors may be used to endorse or promote products derived from
18    this software without specific prior written permission.
19 
20    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
24    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 /*
34  * oggz_auto.c
35  *
36  * Conrad Parker <conrad@annodex.net>
37  */
38 
39 #include "config.h"
40 
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "oggz_private.h"
45 #include "oggz_byteorder.h"
46 #include "dirac.h"
47 
48 #include "oggz/oggz_stream.h"
49 
50 /*#define DEBUG*/
51 
52 /* Allow use of internal metrics; ie. the user_data for these gets free'd
53  * when the metric is overwritten, or on close */
54 int oggz_set_metric_internal (OGGZ * oggz, long serialno, OggzMetric metric,
55 			      void * user_data, int internal);
56 
57 int oggz_set_metric_linear (OGGZ * oggz, long serialno,
58 			    ogg_int64_t granule_rate_numerator,
59 			    ogg_int64_t granule_rate_denominator);
60 
61 static int
oggz_stream_set_numheaders(OGGZ * oggz,long serialno,int numheaders)62 oggz_stream_set_numheaders (OGGZ * oggz, long serialno, int numheaders)
63 {
64   oggz_stream_t * stream;
65 
66   if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;
67 
68   stream = oggz_get_stream (oggz, serialno);
69   if (stream == NULL) return OGGZ_ERR_BAD_SERIALNO;
70 
71   stream->numheaders = numheaders;
72 
73   return 0;
74 }
75 
76 static int
auto_speex(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)77 auto_speex (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
78 {
79   unsigned char * header = data;
80   ogg_int64_t granule_rate = 0;
81   int numheaders;
82 
83   if (length < 68) return 0;
84 
85   granule_rate = (ogg_int64_t) int32_le_at(&header[36]);
86 #ifdef DEBUG
87   printf ("Got speex rate %d\n", (int)granule_rate);
88 #endif
89 
90   oggz_set_granulerate (oggz, serialno, granule_rate, OGGZ_AUTO_MULT);
91 
92   oggz_set_preroll (oggz, serialno, 3);
93 
94   numheaders = (ogg_int64_t) int32_le_at(&header[68]) + 2;
95   oggz_stream_set_numheaders (oggz, serialno, numheaders);
96 
97   return 1;
98 }
99 
100 static int
auto_vorbis(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)101 auto_vorbis (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
102 {
103   unsigned char * header = data;
104   ogg_int64_t granule_rate = 0;
105 
106   if (length < 30) return 0;
107 
108   granule_rate = (ogg_int64_t) int32_le_at(&header[12]);
109 #ifdef DEBUG
110   printf ("Got vorbis rate %d\n", (int)granule_rate);
111 #endif
112 
113   oggz_set_granulerate (oggz, serialno, granule_rate, OGGZ_AUTO_MULT);
114 
115   oggz_set_preroll (oggz, serialno, 2);
116 
117   oggz_stream_set_numheaders (oggz, serialno, 3);
118 
119   return 1;
120 }
121 
122 #if USE_THEORA_PRE_ALPHA_3_FORMAT
intlog(int num)123 static int intlog(int num) {
124   int ret=0;
125   while(num>0){
126     num=num/2;
127     ret=ret+1;
128   }
129   return(ret);
130 }
131 #endif
132 
133 #define THEORA_VERSION(maj,min,rev) ((maj<<16)+(min<<8)+rev)
134 
135 static int
auto_theora(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)136 auto_theora (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
137 {
138   unsigned char * header = data;
139   int version;
140   ogg_int32_t fps_numerator, fps_denominator;
141   char keyframe_granule_shift = 0;
142   int keyframe_shift;
143 
144   /* TODO: this should check against 42 for the relevant version numbers */
145   if (length < 41) return 0;
146 
147   version = THEORA_VERSION(header[7], header[8], header[9]);
148 
149   fps_numerator = int32_be_at(&header[22]);
150   fps_denominator = int32_be_at(&header[26]);
151 
152   /* Very old theora versions used a value of 0 to mean 1.
153    * Unfortunately theora hasn't incremented its version field,
154    * hence we hardcode this workaround for old or broken streams.
155    */
156   if (fps_numerator == 0) fps_numerator = 1;
157 
158 #if USE_THEORA_PRE_ALPHA_3_FORMAT
159   /* old header format, used by Theora alpha2 and earlier */
160   keyframe_granule_shift = (header[36] & 0xf8) >> 3;
161   keyframe_shift = intlog (keyframe_granule_shift - 1);
162 #else
163   keyframe_granule_shift = (char) ((header[40] & 0x03) << 3);
164   keyframe_granule_shift |= (header[41] & 0xe0) >> 5; /* see TODO above */
165   keyframe_shift = keyframe_granule_shift;
166 #endif
167 
168 #ifdef DEBUG
169   printf ("Got theora fps %d/%d, keyframe_shift %d\n",
170 	  fps_numerator, fps_denominator, keyframe_shift);
171 #endif
172 
173   oggz_set_granulerate (oggz, serialno, (ogg_int64_t)fps_numerator,
174 			OGGZ_AUTO_MULT * (ogg_int64_t)fps_denominator);
175   oggz_set_granuleshift (oggz, serialno, keyframe_shift);
176 
177   if (version > THEORA_VERSION(3,2,0))
178     oggz_set_first_granule (oggz, serialno, 1);
179 
180   oggz_stream_set_numheaders (oggz, serialno, 3);
181 
182   return 1;
183 }
184 
185 static int
auto_annodex(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)186 auto_annodex (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
187 {
188   /* Apply a zero metric */
189   oggz_set_granulerate (oggz, serialno, 0, 1);
190 
191   return 1;
192 }
193 
194 static int
auto_anxdata(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)195 auto_anxdata (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
196 {
197   unsigned char * header = data;
198   ogg_int64_t granule_rate_numerator = 0, granule_rate_denominator = 0;
199 
200   if (length < 28) return 0;
201 
202   granule_rate_numerator = int64_le_at(&header[8]);
203   granule_rate_denominator = int64_le_at(&header[16]);
204 #ifdef DEBUG
205   printf ("Got AnxData rate %lld/%lld\n", granule_rate_numerator,
206 	  granule_rate_denominator);
207 #endif
208 
209   oggz_set_granulerate (oggz, serialno,
210 			granule_rate_numerator,
211 			OGGZ_AUTO_MULT * granule_rate_denominator);
212 
213   return 1;
214 }
215 
216 static int
auto_flac0(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)217 auto_flac0 (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
218 {
219   unsigned char * header = data;
220   ogg_int64_t granule_rate = 0;
221 
222   granule_rate = (ogg_int64_t) (header[14] << 12) | (header[15] << 4) |
223             ((header[16] >> 4)&0xf);
224 #ifdef DEBUG
225     printf ("Got flac rate %d\n", (int)granule_rate);
226 #endif
227 
228   oggz_set_granulerate (oggz, serialno, granule_rate, OGGZ_AUTO_MULT);
229 
230   oggz_stream_set_numheaders (oggz, serialno, 3);
231 
232   return 1;
233 }
234 
235 static int
auto_flac(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)236 auto_flac (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
237 {
238   unsigned char * header = data;
239   ogg_int64_t granule_rate = 0;
240   int numheaders;
241 
242   if (length < 51) return 0;
243 
244   granule_rate = (ogg_int64_t) (header[27] << 12) | (header[28] << 4) |
245             ((header[29] >> 4)&0xf);
246 #ifdef DEBUG
247   printf ("Got flac rate %d\n", (int)granule_rate);
248 #endif
249 
250   oggz_set_granulerate (oggz, serialno, granule_rate, OGGZ_AUTO_MULT);
251 
252   numheaders = int16_be_at(&header[7]);
253   oggz_stream_set_numheaders (oggz, serialno, numheaders);
254 
255   return 1;
256 }
257 
258 /**
259  * Recognizer for OggPCM2:
260  * http://wiki.xiph.org/index.php/OggPCM2
261  */
262 static int
auto_oggpcm2(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)263 auto_oggpcm2 (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
264 {
265   unsigned char * header = data;
266   ogg_int64_t granule_rate;
267 
268   if (length < 28) return 0;
269 
270   granule_rate = (ogg_int64_t) int32_be_at(&header[16]);
271 #ifdef DEBUG
272   printf ("Got OggPCM2 rate %d\n", (int)granule_rate);
273 #endif
274 
275   oggz_set_granulerate (oggz, serialno, granule_rate, OGGZ_AUTO_MULT);
276 
277   oggz_stream_set_numheaders (oggz, serialno, 3);
278 
279   return 1;
280 }
281 
282 static int
auto_celt(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)283 auto_celt (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
284 {
285   unsigned char * header = data;
286   ogg_int64_t granule_rate = 0;
287   int numheaders;
288 
289   if (length < 56) return 0;
290 
291   granule_rate = (ogg_int64_t) int32_le_at(&header[40]);
292 #ifdef DEBUG
293   printf ("Got celt sample rate %d\n", (int)granule_rate);
294 #endif
295 
296   oggz_set_granulerate (oggz, serialno, granule_rate, OGGZ_AUTO_MULT);
297 
298   numheaders = (ogg_int64_t) int32_le_at(&header[52]) + 2;
299   oggz_stream_set_numheaders (oggz, serialno, numheaders);
300 
301   return 1;
302 }
303 
304 static int
auto_cmml(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)305 auto_cmml (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
306 {
307   unsigned char * header = data;
308   ogg_int64_t granule_rate_numerator = 0, granule_rate_denominator = 0;
309   int granuleshift;
310 
311   if (length < 28) return 0;
312 
313   granule_rate_numerator = int64_le_at(&header[12]);
314   granule_rate_denominator = int64_le_at(&header[20]);
315   if (length > 28)
316     granuleshift = (int)header[28];
317   else
318     granuleshift = 0;
319 
320 #ifdef DEBUG
321   printf ("Got CMML rate %lld/%lld\n", granule_rate_numerator,
322 	  granule_rate_denominator);
323 #endif
324 
325   oggz_set_granulerate (oggz, serialno,
326 			granule_rate_numerator,
327 			OGGZ_AUTO_MULT * granule_rate_denominator);
328   oggz_set_granuleshift (oggz, serialno, granuleshift);
329 
330   oggz_stream_set_numheaders (oggz, serialno, 3);
331 
332   return 1;
333 }
334 
335 static int
auto_kate(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)336 auto_kate (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
337 {
338   unsigned char * header = data;
339   ogg_int32_t gps_numerator, gps_denominator;
340   unsigned char granule_shift = 0;
341   int numheaders;
342 
343   if (length < 64) return 0;
344 
345   gps_numerator = int32_le_at(&header[24]);
346   gps_denominator = int32_le_at(&header[28]);
347 
348   granule_shift = (header[15]);
349   numheaders = (header[11]);
350 
351 #ifdef DEBUG
352   printf ("Got kate gps %d/%d, granule shift %d\n",
353 	  gps_numerator, gps_denominator, granule_shift);
354 #endif
355 
356   oggz_set_granulerate (oggz, serialno, gps_numerator,
357 			OGGZ_AUTO_MULT * gps_denominator);
358   oggz_set_granuleshift (oggz, serialno, granule_shift);
359 
360   oggz_stream_set_numheaders (oggz, serialno, numheaders);
361 
362   return 1;
363 }
364 
365 static int
auto_dirac(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)366 auto_dirac (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
367 {
368   int granule_shift = 22; /* not a typo */
369   dirac_info *info;
370 
371   info = oggz_malloc(sizeof(dirac_info));
372   if (info == NULL) return -1;
373 
374   if (dirac_parse_info(info, data, length) == -1) {
375     oggz_free (info);
376     return -1;
377   }
378 
379   /* the granulerate is twice the frame rate (in order to handle interlace) */
380   oggz_set_granulerate (oggz, serialno,
381 	2 * (ogg_int64_t)info->fps_numerator,
382 	OGGZ_AUTO_MULT * (ogg_int64_t)info->fps_denominator);
383   oggz_set_granuleshift (oggz, serialno, granule_shift);
384 
385   oggz_stream_set_numheaders (oggz, serialno, 0);
386 
387   oggz_free(info);
388   return 1;
389 }
390 
391 static int
auto_fisbone(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)392 auto_fisbone (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
393 {
394   unsigned char * header = data;
395   long fisbone_serialno; /* The serialno referred to in this fisbone */
396   ogg_int64_t granule_rate_numerator = 0, granule_rate_denominator = 0;
397   int granuleshift, numheaders;
398 
399   if (length < 48) return 0;
400 
401   fisbone_serialno = (long) int32_le_at(&header[12]);
402 
403   /* Don't override an already assigned metric */
404   if (oggz_stream_has_metric (oggz, fisbone_serialno)) return 1;
405 
406   granule_rate_numerator = int64_le_at(&header[20]);
407   granule_rate_denominator = int64_le_at(&header[28]);
408   granuleshift = (int)header[48];
409 
410 #ifdef DEBUG
411   printf ("Got fisbone granulerate %lld/%lld, granuleshift %d for serialno %010lu\n",
412 	  granule_rate_numerator, granule_rate_denominator, granuleshift,
413 	  fisbone_serialno);
414 #endif
415 
416   oggz_set_granulerate (oggz, fisbone_serialno,
417 			granule_rate_numerator,
418 			OGGZ_AUTO_MULT * granule_rate_denominator);
419   oggz_set_granuleshift (oggz, fisbone_serialno, granuleshift);
420 
421   /* Increment the number of headers for this stream */
422   numheaders = oggz_stream_get_numheaders (oggz, serialno);
423   oggz_stream_set_numheaders (oggz, serialno, numheaders+1);
424 
425   return 1;
426 }
427 
428 static int
auto_fishead(OGGZ * oggz,long serialno,unsigned char * data,long length,void * user_data)429 auto_fishead (OGGZ * oggz, long serialno, unsigned char * data, long length, void * user_data)
430 {
431   oggz_set_granulerate (oggz, serialno, 0, 1);
432 
433   /* For skeleton, numheaders will get incremented as each header is seen */
434   oggz_stream_set_numheaders (oggz, serialno, 1);
435 
436   return 1;
437 }
438 
439 /*
440  * The first two speex packets are header and comment packets (granulepos = 0)
441  */
442 
443 typedef struct {
444   int headers_encountered;
445   int packet_size;
446   int encountered_first_data_packet;
447 } auto_calc_speex_info_t;
448 
449 static ogg_int64_t
auto_calc_speex(ogg_int64_t now,oggz_stream_t * stream,ogg_packet * op)450 auto_calc_speex(ogg_int64_t now, oggz_stream_t *stream, ogg_packet *op) {
451 
452   /*
453    * on the first (b_o_s) packet, set calculate_data to be the number
454    * of speex frames per packet
455    */
456 
457   auto_calc_speex_info_t *info
458           = (auto_calc_speex_info_t *)stream->calculate_data;
459 
460   if (stream->calculate_data == NULL) {
461     stream->calculate_data = oggz_malloc(sizeof(auto_calc_speex_info_t));
462     if (stream->calculate_data == NULL) return -1;
463     info = stream->calculate_data;
464     info->encountered_first_data_packet = 0;
465     info->packet_size =
466             (*(int *)(op->packet + 64)) * (*(int *)(op->packet + 56));
467     info->headers_encountered = 1;
468     return 0;
469   }
470 
471   if (info->headers_encountered < 2) {
472     info->headers_encountered += 1;
473   } else {
474     info->encountered_first_data_packet = 1;
475   }
476 
477   if (now > -1) {
478     return now;
479   }
480 
481   if (info->encountered_first_data_packet) {
482     if (stream->last_granulepos > 0) {
483       return stream->last_granulepos + info->packet_size;
484     }
485 
486     return -1;
487   }
488 
489   return 0;
490 
491 }
492 
493 /*
494  * The first two CELT packets are header and comment packets (granulepos = 0)
495  */
496 
497 typedef struct {
498   int headers_encountered;
499   int packet_size;
500   int encountered_first_data_packet;
501 } auto_calc_celt_info_t;
502 
503 static ogg_int64_t
auto_calc_celt(ogg_int64_t now,oggz_stream_t * stream,ogg_packet * op)504 auto_calc_celt (ogg_int64_t now, oggz_stream_t *stream, ogg_packet *op) {
505 
506   /*
507    * on the first (b_o_s) packet, set calculate_data to be the number
508    * of celt frames per packet
509    */
510 
511   auto_calc_celt_info_t *info
512           = (auto_calc_celt_info_t *)stream->calculate_data;
513 
514   if (stream->calculate_data == NULL) {
515     stream->calculate_data = oggz_malloc(sizeof(auto_calc_celt_info_t));
516     if (stream->calculate_data == NULL) return -1;
517 
518     info = stream->calculate_data;
519     info->encountered_first_data_packet = 0;
520 
521     /* In general, the number of frames per packet depends on the mode.
522      * Currently (20080213) both available modes, mono and stereo, have 256
523      * frames per packet.
524      */
525     info->packet_size = 256;
526 
527     info->headers_encountered = 1;
528     return 0;
529   }
530 
531   if (info->headers_encountered < 2) {
532     info->headers_encountered += 1;
533   } else {
534     info->encountered_first_data_packet = 1;
535   }
536 
537   if (now > -1) {
538     return now;
539   }
540 
541   if (info->encountered_first_data_packet) {
542     if (stream->last_granulepos > 0) {
543       return stream->last_granulepos + info->packet_size;
544     }
545 
546     return -1;
547   }
548 
549   return 0;
550 
551 }
552 /*
553  * Header packets are marked by a set MSB in the first byte.  Inter packets
554  * are marked by a set 2MSB in the first byte.  Intra packets (keyframes)
555  * are any that are left over ;-)
556  *
557  * (see http://theora.org/doc/Theora.pdf for the theora specification)
558  */
559 
560 typedef struct {
561   int encountered_first_data_packet;
562 } auto_calc_theora_info_t;
563 
564 
565 static ogg_int64_t
auto_calc_theora(ogg_int64_t now,oggz_stream_t * stream,ogg_packet * op)566 auto_calc_theora(ogg_int64_t now, oggz_stream_t *stream, ogg_packet *op) {
567 
568   long keyframe_no;
569   int keyframe_shift;
570   unsigned char first_byte;
571   auto_calc_theora_info_t *info;
572 
573   first_byte = op->bytes == 0 ? 0x40 : op->packet[0];
574 
575   info = (auto_calc_theora_info_t *)stream->calculate_data;
576 
577   /* header packet */
578   if (first_byte & 0x80)
579   {
580     if (info == NULL) {
581       stream->calculate_data = oggz_malloc(sizeof(auto_calc_theora_info_t));
582       if (stream->calculate_data == NULL) return -1;
583       info = stream->calculate_data;
584     }
585     info->encountered_first_data_packet = 0;
586     return (ogg_int64_t)0;
587   }
588 
589   /* known granulepos */
590   if (now > (ogg_int64_t)(-1)) {
591     info->encountered_first_data_packet = 1;
592     return now;
593   }
594 
595   /* last granulepos unknown */
596   if (stream->last_granulepos == -1) {
597     info->encountered_first_data_packet = 1;
598     return (ogg_int64_t)-1;
599   }
600 
601   /*
602    * first data packet is -1 if gp not set
603    */
604   if (!info->encountered_first_data_packet) {
605     info->encountered_first_data_packet = 1;
606     return (ogg_int64_t)-1;
607   }
608 
609   /* inter-coded packet */
610   if (first_byte & 0x40)
611   {
612     return stream->last_granulepos + 1;
613   }
614 
615   keyframe_shift = stream->granuleshift;
616   /*
617    * retrieve last keyframe number
618    */
619   keyframe_no = (int)(stream->last_granulepos >> keyframe_shift);
620   /*
621    * add frames since last keyframe number
622    */
623   keyframe_no += (stream->last_granulepos & ((1 << keyframe_shift) - 1)) + 1;
624   return ((ogg_int64_t)keyframe_no) << keyframe_shift;
625 
626 
627 }
628 
629 static ogg_int64_t
auto_rcalc_theora(ogg_int64_t next_packet_gp,oggz_stream_t * stream,ogg_packet * this_packet,ogg_packet * next_packet)630 auto_rcalc_theora(ogg_int64_t next_packet_gp, oggz_stream_t *stream,
631                   ogg_packet *this_packet, ogg_packet *next_packet) {
632 
633   int keyframe = (int)(next_packet_gp >> stream->granuleshift);
634   int offset = (int)(next_packet_gp - (keyframe << stream->granuleshift));
635 
636   /* assume kf is 60 frames ago. NOTE: This is going to cause problems,
637    * but I can't think of what else to do.  The position of the last kf
638    * is fundamentally unknowable.
639    */
640   if (offset == 0) {
641     return ((keyframe - 60L) << stream->granuleshift) + 59;
642   }
643   else {
644     return (((ogg_int64_t)keyframe) << stream->granuleshift) + (offset - 1);
645   }
646 
647 }
648 
649 
650 /*
651  * Vorbis packets can be short or long, and each packet overlaps the previous
652  * and next packets.  The granulepos of a packet is always the last sample
653  * that is completely decoded at the end of decoding that packet - i.e. the
654  * last packet before the first overlapping packet.  If the sizes of packets
655  * are 's' and 'l', then the increment will depend on the previous and next
656  * packet types:
657  *  v                             prev<<1 | next
658  * lll:           l/2             3
659  * lls:           3l/4 - s/4      2
660  * lsl:           s/2
661  * lss:           s/2
662  * sll:           l/4 + s/4       1
663  * sls:           l/2             0
664  * ssl:           s/2
665  * sss:           s/2
666  *
667  * The previous and next packet types can be inferred from the current packet
668  * (additional information is not required)
669  *
670  * The two blocksizes can be determined from the first header packet, by reading
671  * byte 28.  1 << (packet[28] >> 4) == long_size.
672  * 1 << (packet[28] & 0xF) == short_size.
673  *
674  * (see http://xiph.org/vorbis/doc/Vorbis_I_spec.html for specification)
675  */
676 
677 typedef struct {
678   int nln_increments[4];
679   int nsn_increment;
680   int short_size;
681   int long_size;
682   int encountered_first_data_packet;
683   int last_was_long;
684   int log2_num_modes;
685   int mode_sizes[1];
686 } auto_calc_vorbis_info_t;
687 
688 
689 static ogg_int64_t
auto_calc_vorbis(ogg_int64_t now,oggz_stream_t * stream,ogg_packet * op)690 auto_calc_vorbis(ogg_int64_t now, oggz_stream_t *stream, ogg_packet *op) {
691 
692   auto_calc_vorbis_info_t *info;
693   int ii;
694 
695   if (stream->calculate_data == NULL) {
696     /*
697      * on the first (b_o_s) packet, determine the long and short sizes,
698      * and then calculate l/2, l/4 - s/4, 3 * l/4 - s/4, l/2 - s/2 and s/2
699      */
700     int short_size;
701     int long_size;
702 
703     long_size = 1 << (op->packet[28] >> 4);
704     short_size = 1 << (op->packet[28] & 0xF);
705 
706     stream->calculate_data = oggz_malloc(sizeof(auto_calc_vorbis_info_t));
707     if (stream->calculate_data == NULL) return -1;
708 
709     info = (auto_calc_vorbis_info_t *)stream->calculate_data;
710     info->nln_increments[3] = long_size >> 1;
711     info->nln_increments[2] = 3 * (long_size >> 2) - (short_size >> 2);
712     info->nln_increments[1] = (long_size >> 2) + (short_size >> 2);
713     info->nln_increments[0] = info->nln_increments[3];
714     info->short_size = short_size;
715     info->long_size = long_size;
716     info->nsn_increment = short_size >> 1;
717     info->encountered_first_data_packet = 0;
718 
719     /* this is a header packet */
720     return 0;
721   }
722 
723   /*
724    * marker for header packets
725    */
726   if (op->packet[0] & 0x1) {
727     /*
728      * the code pages, a whole bunch of other fairly useless stuff, AND,
729      * RIGHT AT THE END (of a bunch of variable-length compressed rubbish that
730      * basically has only one actual set of values that everyone uses BUT YOU
731      * CAN'T BE SURE OF THAT, OH NO YOU CAN'T) is the only piece of data that's
732      * actually useful to us - the packet modes (because it's inconceivable to
733      * think people might want _just that_ and nothing else, you know, for
734      * seeking and stuff).
735      *
736      * Fortunately, because of the mandate that non-used bits must be zero
737      * at the end of the packet, we might be able to sneakily work backwards
738      * and find out the information we need (namely a mapping of modes to
739      * packet sizes)
740      */
741     if (op->packet[0] == 5) {
742       unsigned char *current_pos = &op->packet[op->bytes - 1];
743       int offset;
744       int size;
745       int size_check;
746       int *mode_size_ptr;
747       int i;
748       size_t size_realloc_bytes;
749 
750       /*
751        * This is the format of the mode data at the end of the packet for all
752        * Vorbis Version 1 :
753        *
754        * [ 6:number_of_modes ]
755        * [ 1:size | 16:window_type(0) | 16:transform_type(0) | 8:mapping ]
756        * [ 1:size | 16:window_type(0) | 16:transform_type(0) | 8:mapping ]
757        * [ 1:size | 16:window_type(0) | 16:transform_type(0) | 8:mapping ]
758        * [ 1:framing(1) ]
759        *
760        * e.g.:
761        *
762        *              <-
763        * 0 0 0 0 0 1 0 0
764        * 0 0 1 0 0 0 0 0
765        * 0 0 1 0 0 0 0 0
766        * 0 0 1|0 0 0 0 0
767        * 0 0 0 0|0|0 0 0
768        * 0 0 0 0 0 0 0 0
769        * 0 0 0 0|0 0 0 0
770        * 0 0 0 0 0 0 0 0
771        * 0 0 0 0|0 0 0 0
772        * 0 0 0|1|0 0 0 0 |
773        * 0 0 0 0 0 0 0 0 V
774        * 0 0 0|0 0 0 0 0
775        * 0 0 0 0 0 0 0 0
776        * 0 0 1|0 0 0 0 0
777        * 0 0|1|0 0 0 0 0
778        *
779        *
780        * i.e. each entry is an important bit, 32 bits of 0, 8 bits of blah, a
781        * bit of 1.
782        * Let's find our last 1 bit first.
783        *
784        */
785 
786       size = 0;
787 
788       offset = 8;
789       while (!((1 << --offset) & *current_pos)) {
790         if (offset == 0) {
791           offset = 8;
792           current_pos -= 1;
793         }
794       }
795 
796       while (1)
797       {
798 
799         /*
800          * from current_pos-5:(offset+1) to current_pos-1:(offset+1) should
801          * be zero
802          */
803         offset = (offset + 7) % 8;
804         if (offset == 7)
805           current_pos -= 1;
806 
807         if
808         (
809           ((current_pos[-5] & ~((1 << (offset + 1)) - 1)) != 0)
810           ||
811           current_pos[-4] != 0
812           ||
813           current_pos[-3] != 0
814           ||
815           current_pos[-2] != 0
816           ||
817           ((current_pos[-1] & ((1 << (offset + 1)) - 1)) != 0)
818         )
819         {
820           break;
821         }
822 
823         size += 1;
824 
825         current_pos -= 5;
826 
827       }
828 
829       /* Give ourselves a chance to recover if we went back too far by using
830        * the size check. */
831       for (ii=0; ii < 2; ii++) {
832        if (offset > 4) {
833          size_check = (current_pos[0] >> (offset - 5)) & 0x3F;
834        } else {
835          /* mask part of byte from current_pos */
836          size_check = (current_pos[0] & ((1 << (offset + 1)) - 1));
837          /* shift to appropriate position */
838          size_check <<= (5 - offset);
839          /* or in part of byte from current_pos - 1 */
840          size_check |= (current_pos[-1] & ~((1 << (offset + 3)) - 1)) >>
841            (offset + 3);
842        }
843 
844        size_check += 1;
845        if (size_check == size) {
846          break;
847        }
848         offset = (offset + 1) % 8;
849         if (offset == 0)
850           current_pos += 1;
851        current_pos += 5;
852        size -= 1;
853       }
854 
855 #ifdef DEBUG
856       if (size_check != size)
857       {
858         printf("WARNING: size parsing failed for VORBIS mode packets\n");
859       }
860 #endif
861 
862       /* Check that size to be realloc'd doesn't overflow */
863       size_realloc_bytes = sizeof(auto_calc_vorbis_info_t) + (size - 1) * sizeof(int);
864       if (size_realloc_bytes < sizeof (auto_calc_vorbis_info_t)) return -1;
865 
866       /* Store mode size information in our info struct */
867       info = realloc(stream->calculate_data, size_realloc_bytes);
868       if (info == NULL) return -1;
869 
870       stream->calculate_data = info;
871 
872       i = -1;
873       while ((1 << (++i)) < size);
874       info->log2_num_modes = i;
875 
876       mode_size_ptr = info->mode_sizes;
877 
878       for(i = 0; i < size; i++)
879       {
880         offset = (offset + 1) % 8;
881         if (offset == 0)
882           current_pos += 1;
883         *mode_size_ptr++ = (current_pos[0] >> offset) & 0x1;
884         current_pos += 5;
885       }
886 
887     }
888 
889     return 0;
890   }
891 
892   info = (auto_calc_vorbis_info_t *)stream->calculate_data;
893 
894   return -1;
895 
896   {
897     /*
898      * we're in a data packet!  First we need to get the mode of the packet,
899      * and from the mode, the size
900      */
901     int mode;
902     int size;
903     ogg_int64_t result;
904 
905     mode = (op->packet[0] >> 1) & ((1 << info->log2_num_modes) - 1);
906     size = info->mode_sizes[mode];
907 
908     /*
909      * if we have a working granulepos, we use it, but only if we can't
910      * calculate a valid gp value.
911      */
912     if (now > -1 && stream->last_granulepos == -1) {
913       info->encountered_first_data_packet = 1;
914       info->last_was_long = size;
915       return now;
916     }
917 
918     if (info->encountered_first_data_packet == 0) {
919       info->encountered_first_data_packet = 1;
920       info->last_was_long = size;
921       return -1;
922     }
923 
924     /*
925      * otherwise, if we haven't yet had a working granulepos, we return
926      * -1
927      */
928     if (stream->last_granulepos == -1) {
929       info->last_was_long = size;
930       return -1;
931     }
932 
933     result = stream->last_granulepos +
934       (
935         (info->last_was_long ? info->long_size  : info->short_size)
936         +
937         (size ? info->long_size : info->short_size)
938       ) / 4;
939     info->last_was_long = size;
940 
941     return result;
942 
943   }
944 
945 }
946 
947 ogg_int64_t
auto_rcalc_vorbis(ogg_int64_t next_packet_gp,oggz_stream_t * stream,ogg_packet * this_packet,ogg_packet * next_packet)948 auto_rcalc_vorbis(ogg_int64_t next_packet_gp, oggz_stream_t *stream,
949                   ogg_packet *this_packet, ogg_packet *next_packet) {
950 
951   auto_calc_vorbis_info_t *info =
952                   (auto_calc_vorbis_info_t *)stream->calculate_data;
953 
954   int mode =
955       (this_packet->packet[0] >> 1) & ((1 << info->log2_num_modes) - 1);
956   int this_size = info->mode_sizes[mode] ? info->long_size : info->short_size;
957   int next_size;
958   ogg_int64_t r;
959 
960   mode = (next_packet->packet[0] >> 1) & ((1 << info->log2_num_modes) - 1);
961   next_size = info->mode_sizes[mode] ? info->long_size : info->short_size;
962 
963   r = next_packet_gp - ((this_size + next_size) / 4);
964   if (r < 0) return 0L;
965   return r;
966 
967 }
968 
969 /**
970  * FLAC
971  * Defined at: http://flac.sourceforge.net/ogg_mapping.html
972  *   - Native FLAC audio frames appear as subsequent packets in the stream.
973  *     Each packet corresponds to one FLAC audio frame.
974  *   - FLAC packets may span page boundaries.
975  *
976  * The frame header defines block size
977  * http://flac.sourceforge.net/format.html#frame_header
978  *
979  * Note that most FLAC packets will have a granulepos, but rare cases exist
980  * where they don't. See for example
981  * http://rafb.net/paste/results/Pkib5w72.html
982  */
983 
984 typedef struct {
985   ogg_int64_t previous_gp;
986   int encountered_first_data_packet;
987 } auto_calc_flac_info_t;
988 
989 static ogg_int64_t
auto_calc_flac(ogg_int64_t now,oggz_stream_t * stream,ogg_packet * op)990 auto_calc_flac (ogg_int64_t now, oggz_stream_t *stream, ogg_packet *op)
991 {
992   auto_calc_flac_info_t *info;
993 
994   if (stream->calculate_data == NULL) {
995     stream->calculate_data = oggz_malloc(sizeof(auto_calc_flac_info_t));
996     if (stream->calculate_data == NULL) return -1;
997 
998     info = (auto_calc_flac_info_t *)stream->calculate_data;
999     info->previous_gp = 0;
1000     info->encountered_first_data_packet = 0;
1001 
1002     /* this is a header packet */
1003     goto out;
1004   }
1005 
1006   info = (auto_calc_flac_info_t *)stream->calculate_data;
1007 
1008   /* FLAC audio frames begin with marker 0xFF */
1009   if (op->packet[0] == 0xff)
1010       info->encountered_first_data_packet = 1;
1011 
1012   if (now == -1 && op->packet[0] == 0xff && op->bytes > 2) {
1013     unsigned char bs;
1014     int block_size;
1015 
1016     bs = (op->packet[2] & 0xf0) >> 4;
1017 
1018     switch (bs) {
1019       case 0x0: /*   0000 : get from STREAMINFO metadata block */
1020         block_size = -1;
1021         break;
1022       case 0x1: /* 0001 : 192 samples */
1023         block_size = 192;
1024         break;
1025       /* 0010-0101 : 576 * (2^(n-2)) samples, i.e. 576/1152/2304/4608 */
1026       case 0x2:
1027         block_size = 576;
1028         break;
1029       case 0x3:
1030         block_size = 1152;
1031         break;
1032       case 0x4:
1033         block_size = 2304;
1034         break;
1035       case 0x5:
1036         block_size = 4608;
1037         break;
1038       case 0x6: /* 0110 : get 8 bit (blocksize-1) from end of header */
1039         block_size = -1;
1040         break;
1041       case 0x7: /* 0111 : get 16 bit (blocksize-1) from end of header */
1042         block_size = -1;
1043         break;
1044       /* 1000-1111 : 256 * (2^(n-8)) samples, i.e. 256/512/1024/2048/4096/8192/16384/32768 */
1045       case 0x8:
1046         block_size = 256;
1047         break;
1048       case 0x9:
1049         block_size = 512;
1050         break;
1051       case 0xa:
1052         block_size = 1024;
1053         break;
1054       case 0xb:
1055         block_size = 2048;
1056         break;
1057       case 0xc:
1058         block_size = 4096;
1059         break;
1060       case 0xd:
1061         block_size = 8192;
1062         break;
1063       case 0xe:
1064         block_size = 16384;
1065         break;
1066       case 0xf:
1067         block_size = 32768;
1068         break;
1069       default:
1070         block_size = -1;
1071         break;
1072     }
1073 
1074     if (block_size != -1) {
1075       now = info->previous_gp + block_size;
1076     }
1077   } else if (now == -1 && info->encountered_first_data_packet == 0) {
1078     /* this is a header packet */
1079     now = 0;
1080   }
1081 
1082 out:
1083   info->previous_gp = now;
1084   return now;
1085 }
1086 
1087 const oggz_auto_contenttype_t oggz_auto_codec_ident[] = {
1088   {"\200theora", 7, "Theora", auto_theora, auto_calc_theora, auto_rcalc_theora},
1089   {"\001vorbis", 7, "Vorbis", auto_vorbis, auto_calc_vorbis, auto_rcalc_vorbis},
1090   {"Speex", 5, "Speex", auto_speex, auto_calc_speex, NULL},
1091   {"PCM     ", 8, "PCM", auto_oggpcm2, NULL, NULL},
1092   {"CMML\0\0\0\0", 8, "CMML", auto_cmml, NULL, NULL},
1093   {"Annodex", 7, "Annodex", auto_annodex, NULL, NULL},
1094   {"fishead", 7, "Skeleton", auto_fishead, NULL, NULL},
1095   {"fLaC", 4, "Flac0", auto_flac0, auto_calc_flac, NULL},
1096   {"\177FLAC", 5, "Flac", auto_flac, auto_calc_flac, NULL},
1097   {"AnxData", 7, "AnxData", auto_anxdata, NULL, NULL},
1098   {"CELT    ", 8, "CELT", auto_celt, auto_calc_celt, NULL},
1099   {"\200kate\0\0\0", 8, "Kate", auto_kate, NULL, NULL},
1100   {"BBCD\0", 5, "Dirac", auto_dirac, NULL, NULL},
1101   {"", 0, "Unknown", NULL, NULL, NULL}
1102 };
1103 
1104 static int
oggz_auto_identify(OGGZ * oggz,long serialno,unsigned char * data,long len)1105 oggz_auto_identify (OGGZ * oggz, long serialno, unsigned char * data, long len)
1106 {
1107   int i;
1108 
1109   for (i = 0; i < OGGZ_CONTENT_UNKNOWN; i++)
1110   {
1111     const oggz_auto_contenttype_t *codec = oggz_auto_codec_ident + i;
1112 
1113     if (len >= codec->bos_str_len &&
1114         memcmp (data, codec->bos_str, codec->bos_str_len) == 0) {
1115 
1116       oggz_stream_set_content (oggz, serialno, i);
1117 
1118       return 1;
1119     }
1120   }
1121 
1122   oggz_stream_set_content (oggz, serialno, OGGZ_CONTENT_UNKNOWN);
1123   return 0;
1124 }
1125 
1126 int
oggz_auto_identify_page(OGGZ * oggz,ogg_page * og,long serialno)1127 oggz_auto_identify_page (OGGZ * oggz, ogg_page *og, long serialno)
1128 {
1129   return oggz_auto_identify (oggz, serialno, og->body, og->body_len);
1130 }
1131 
1132 int
oggz_auto_identify_packet(OGGZ * oggz,ogg_packet * op,long serialno)1133 oggz_auto_identify_packet (OGGZ * oggz, ogg_packet * op, long serialno)
1134 {
1135   return oggz_auto_identify (oggz, serialno, op->packet, op->bytes);
1136 }
1137 
1138 int
oggz_auto_read_bos_page(OGGZ * oggz,ogg_page * og,long serialno,void * user_data)1139 oggz_auto_read_bos_page (OGGZ * oggz, ogg_page * og, long serialno,
1140                          void * user_data)
1141 {
1142   int content = 0;
1143 
1144   content = oggz_stream_get_content(oggz, serialno);
1145   if (content < 0 || content >= OGGZ_CONTENT_UNKNOWN) {
1146     return 0;
1147   } else if (content == OGGZ_CONTENT_SKELETON && !ogg_page_bos(og)) {
1148     return auto_fisbone(oggz, serialno, og->body, og->body_len, user_data);
1149   } else {
1150     return oggz_auto_codec_ident[content].reader(oggz, serialno, og->body, og->body_len, user_data);
1151   }
1152 }
1153 
1154 int
oggz_auto_read_bos_packet(OGGZ * oggz,ogg_packet * op,long serialno,void * user_data)1155 oggz_auto_read_bos_packet (OGGZ * oggz, ogg_packet * op, long serialno,
1156                            void * user_data)
1157 {
1158   int content = 0;
1159 
1160   content = oggz_stream_get_content(oggz, serialno);
1161   if (content < 0 || content >= OGGZ_CONTENT_UNKNOWN) {
1162     return 0;
1163   } else if (content == OGGZ_CONTENT_SKELETON && !op->b_o_s) {
1164     return auto_fisbone(oggz, serialno, op->packet, op->bytes, user_data);
1165   } else {
1166     return oggz_auto_codec_ident[content].reader(oggz, serialno, op->packet, op->bytes, user_data);
1167   }
1168 }
1169 
1170 ogg_int64_t
oggz_auto_calculate_granulepos(int content,ogg_int64_t now,oggz_stream_t * stream,ogg_packet * op)1171 oggz_auto_calculate_granulepos(int content, ogg_int64_t now,
1172                 oggz_stream_t *stream, ogg_packet *op) {
1173   if (oggz_auto_codec_ident[content].calculator != NULL) {
1174     ogg_int64_t r = oggz_auto_codec_ident[content].calculator(now, stream, op);
1175     return r;
1176   }
1177 
1178   return now;
1179 }
1180 
1181 ogg_int64_t
oggz_auto_calculate_gp_backwards(int content,ogg_int64_t next_packet_gp,oggz_stream_t * stream,ogg_packet * this_packet,ogg_packet * next_packet)1182 oggz_auto_calculate_gp_backwards(int content, ogg_int64_t next_packet_gp,
1183       oggz_stream_t *stream, ogg_packet *this_packet, ogg_packet *next_packet) {
1184 
1185   if (oggz_auto_codec_ident[content].r_calculator != NULL) {
1186     return oggz_auto_codec_ident[content].r_calculator(next_packet_gp,
1187             stream, this_packet, next_packet);
1188   }
1189 
1190   return 0;
1191 
1192 }
1193 
1194 int
oggz_auto_read_comments(OGGZ * oggz,oggz_stream_t * stream,long serialno,ogg_packet * op)1195 oggz_auto_read_comments (OGGZ * oggz, oggz_stream_t * stream, long serialno,
1196                          ogg_packet * op)
1197 {
1198   int offset = -1;
1199   long len = -1;
1200 
1201   switch (stream->content) {
1202     case OGGZ_CONTENT_VORBIS:
1203       if (op->bytes > 7 && memcmp (op->packet, "\003vorbis", 7) == 0)
1204         offset = 7;
1205       break;
1206     case OGGZ_CONTENT_SPEEX:
1207       offset = 0; break;
1208     case OGGZ_CONTENT_THEORA:
1209       if (op->bytes > 7 && memcmp (op->packet, "\201theora", 7) == 0)
1210         offset = 7;
1211       break;
1212     case OGGZ_CONTENT_KATE:
1213       if (op->bytes > 9 && memcmp (op->packet, "\201kate\0\0\0", 8) == 0) {
1214         /* we skip the reserved 0 byte after the signature */
1215         offset = 9;
1216       }
1217       break;
1218     case OGGZ_CONTENT_FLAC:
1219       if (op->bytes > 4 && (op->packet[0] & 0x7) == 4) {
1220         len = (op->packet[1]<<16) + (op->packet[2]<<8) + op->packet[3];
1221         offset = 4;
1222       }
1223       break;
1224     default:
1225       break;
1226   }
1227 
1228   /* The length of the comments to decode is the rest of the packet,
1229    * unless otherwise determined (ie. for FLAC) */
1230   if (len == -1)
1231     len = op->bytes - offset;
1232 
1233   if (offset >= 0) {
1234     oggz_comments_decode (oggz, serialno, op->packet+offset, len);
1235   }
1236 
1237   return 0;
1238 }
1239 
1240