1 /**
2 
3         \file ADM_eac3info
4         \brief extract info from EAC3/A52B/DD+ streams
5         Author: mean <fixounet@free.fr>, (C) 2009
6         Code very derived from ffmpeg (tables etc...)
7 
8 */
9 
10 #include "ADM_default.h"
11 #include "ADM_eac3info.h"
12 #define ADM_LAV_NO_CONFIG
13 extern "C"
14 {
15 #define sign_extend
16 #include "libavcodec/ac3.h"
17 #include "libavcodec/ac3_parser_internal.h"
18 #include "libavutil/mem.h"
19 };
20 
21 /**
22     \fn ADM_EAC3GetInfo
23 */
ADM_EAC3GetInfo(const uint8_t * data,uint32_t len,uint32_t * syncoff,ADM_EAC3_INFO * info,bool plainAC3)24 bool ADM_EAC3GetInfo(const uint8_t *data, uint32_t len, uint32_t *syncoff, ADM_EAC3_INFO *info, bool plainAC3)
25 {
26     uint32_t of=0;
27     *syncoff=0;
28     uint8_t *buf=new uint8_t[len+AV_INPUT_BUFFER_PADDING_SIZE];
29     memset(buf,0,len+AV_INPUT_BUFFER_PADDING_SIZE);
30     memcpy(buf,data,len);
31 #define CLEANUP delete [] buf; buf=NULL; av_free(hdr); hdr=NULL;
32     //	printf("\n Syncing on %d \n",len);
33     // Search for startcode
34     // 0x0b 0x77
35     while(1)
36     {
37         if(len<7)
38         {
39             ADM_warning("Not enough info to find a52 syncword\n");
40             delete [] buf;
41             buf=NULL;
42             return false;
43         }
44         if(*(buf+of)!=0x0b || *(buf+of+1)!=0x77)
45         {
46             len--;
47             of++;
48             continue;
49         }
50         AC3HeaderInfo *hdr=NULL;
51         if(avpriv_ac3_parse_header(&hdr,buf+of,len)<0)
52         {
53             len--;
54             of++;
55             ADM_info("Sync failed... continuing\n");
56             continue;
57         }
58         if(!plainAC3 && hdr->bitstream_id<=10) // this is not EAC3 but plain ac3
59         {
60             ADM_info("Bitstream ID = %d: not EAC3\n",hdr->bitstream_id);
61             CLEANUP
62             return false;
63         }
64         if(plainAC3 && hdr->bitstream_id>10) // this is not AC3 but EAC3
65         {
66             ADM_info("Bitstream ID = %d: not AC3\n",hdr->bitstream_id);
67             CLEANUP
68             return false;
69         }
70 //            printf("Sync found at offset %"PRIu32"\n",of);
71         *syncoff=of;
72         info->frequency=(uint32_t)hdr->sample_rate;
73         info->byterate=(uint32_t)hdr->bit_rate>>3;
74         info->channels=hdr->channels;
75         info->frameSizeInBytes=hdr->frame_size;
76         info->samples=265*6; // ??
77         CLEANUP
78         return true;
79     }
80     delete [] buf;
81     buf=NULL;
82     return true;
83 }
84