xine-lib: xine-lib-1.2.13/src/audio_dec/xine_a52_parser.h Source File

xine-lib 1.2.13-20230125hg15249

xine_a52_parser.h
Go to the documentation of this file.
1
2/*
3 * Copyright (C) 2000-2020 the xine project
4 *
5 * This file is part of xine, a free video player.
6 *
7 * xine is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * xine is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * a52 frame parser
22 */
23
24#ifndef _XINE_A52_PARSER_H
25#define _XINE_A52_PARSER_H
26
27#include <sys/types.h>
28
29#include <xine/xine_internal.h>
30#include <xine/xineutils.h>
31
32 static const char *a52_channel_info(int a52_flags) {
33 switch (a52_flags & A52_CHANNEL_MASK) {
34 case A52_3F2R:
35 return (a52_flags & A52_LFE) ? "A/52 5.1" : "A/52 5.0";
36 case A52_3F1R:
37 case A52_2F2R:
38 return (a52_flags & A52_LFE) ? "A/52 4.1" : "A/52 4.0";
39 case A52_2F1R:
40 case A52_3F:
41 return "A/52 3.0";
42 case A52_STEREO:
43 return "A/52 2.0 (stereo)";
44 case A52_DOLBY:
45 return "A/52 2.0 (dolby)";
46 case A52_MONO:
47 return "A/52 1.0";
48 default:
49 return "A/52";
50 }
51}
52
53 static void a52_meta_info_set(xine_stream_t *stream, int a52_flags, int bit_rate, int sample_rate) {
54 _x_meta_info_set_utf8 (stream, XINE_META_INFO_AUDIOCODEC, a52_channel_info(a52_flags));
55 _x_stream_info_set (stream, XINE_STREAM_INFO_AUDIO_BITRATE, bit_rate);
56 _x_stream_info_set (stream, XINE_STREAM_INFO_AUDIO_SAMPLERATE, sample_rate);
57}
58
59 static void do_swab(uint8_t *p, uint8_t *end) {
60 lprintf ("byte-swapping dnet\n");
61
62 while (p != end) {
63 uint8_t byte = *p++;
64 *(p - 1) = *p;
65 *p++ = byte;
66 }
67}
68
69 typedef struct {
70 uint8_t got_frame;
71 uint8_t sync_state;
72
73 int a52_flags;
74 int a52_bit_rate;
75 int a52_sample_rate;
76
77 int frame_length, frame_todo;
78
79 uint16_t syncword;
80
81 uint8_t *frame_ptr;
82 uint8_t frame_buffer[3840];
83} xine_a52_parser_t;
84
85 static void xine_a52_parser_reset(xine_a52_parser_t *this) {
86 this->syncword = 0;
87 this->sync_state = 0;
88}
89
90 static size_t xine_a52_parse_data(xine_a52_parser_t *this, xine_stream_t *stream, const uint8_t *data, size_t size) {
91
92 const uint8_t *const end = data + size;
93 const uint8_t *current = data;
94 const uint8_t *sync_start = current + 1;
95
96 this->got_frame = 0;
97
98 while (current < end) {
99 switch (this->sync_state) {
100 case 0: /* Looking for sync header */
101 this->syncword = (this->syncword << 8) | *current++;
102 if (this->syncword == 0x0b77) {
103
104 this->frame_buffer[0] = 0x0b;
105 this->frame_buffer[1] = 0x77;
106
107 this->sync_state = 1;
108 this->frame_ptr = this->frame_buffer+2;
109 }
110 break;
111
112 case 1: /* Looking for enough bytes for sync_info. */
113 sync_start = current - 1;
114 *this->frame_ptr++ = *current++;
115 if ((this->frame_ptr - this->frame_buffer) > 16) {
116 int a52_flags_old = this->a52_flags;
117 int a52_sample_rate_old = this->a52_sample_rate;
118 int a52_bit_rate_old = this->a52_bit_rate;
119
120 this->frame_length = a52_syncinfo (this->frame_buffer,
121 &this->a52_flags,
122 &this->a52_sample_rate,
123 &this->a52_bit_rate);
124
125 if (this->frame_length < 80) { /* Invalid a52 frame_length */
126 this->syncword = 0;
127 current = sync_start;
128 this->sync_state = 0;
129 break;
130 }
131
132 lprintf("Frame length = %d\n",this->frame_length);
133
134 this->frame_todo = this->frame_length - 17;
135 this->sync_state = 2;
136 if (a52_flags_old != this->a52_flags ||
137 a52_sample_rate_old != this->a52_sample_rate ||
138 a52_bit_rate_old != this->a52_bit_rate) {
139 a52_meta_info_set(stream, this->a52_flags, this->a52_bit_rate, this->a52_sample_rate);
140 }
141 }
142 break;
143
144 case 2: /* Filling frame_buffer with sync_info bytes */
145 *this->frame_ptr++ = *current++;
146 this->frame_todo--;
147 if (this->frame_todo > 0)
148 break;
149 /* Ready for decode */
150 this->syncword = 0;
151 this->sync_state = 0;
152 if (xine_crc16_ansi (0, &this->frame_buffer[2], this->frame_length - 2) != 0) { /* CRC16 failed */
153 xprintf(stream->xine, XINE_VERBOSITY_DEBUG, "liba52:a52 frame failed crc16 checksum.\n");
154 current = sync_start;
155 break;
156 }
157 this->got_frame = 1;
158 return (current - data);
159 break;
160 default: /* No come here */
161 break;
162 }
163 }
164
165 return size;
166}
167
168
169#endif /* _XINE_A52_PARSER_H */
_x_stream_info_set
void _x_stream_info_set(xine_stream_t *s, int info, int value)
Definition info_helper.c:79
_x_meta_info_set_utf8
void _x_meta_info_set_utf8(xine_stream_t *s, int info, const char *str)
Definition info_helper.c:341
xine_a52_parser_t
Definition xine_a52_parser.h:69
xine_a52_parser_t::frame_ptr
uint8_t * frame_ptr
Definition xine_a52_parser.h:81
xine_a52_parser_t::a52_sample_rate
int a52_sample_rate
Definition xine_a52_parser.h:75
xine_a52_parser_t::sync_state
uint8_t sync_state
Definition xine_a52_parser.h:71
xine_a52_parser_t::got_frame
uint8_t got_frame
Definition xine_a52_parser.h:70
xine_a52_parser_t::a52_flags
int a52_flags
Definition xine_a52_parser.h:73
xine_a52_parser_t::frame_length
int frame_length
Definition xine_a52_parser.h:77
xine_a52_parser_t::syncword
uint16_t syncword
Definition xine_a52_parser.h:79
xine_a52_parser_t::a52_bit_rate
int a52_bit_rate
Definition xine_a52_parser.h:74
xine_stream_s
Definition xine_internal.h:123
xine_stream_s::xine
xine_t * xine
Definition xine_internal.h:126
xine_crc16_ansi
uint32_t xine_crc16_ansi(uint32_t crc, const uint8_t *data, size_t len)
Definition utils.c:1071
lprintf
#define lprintf(_fmt,...)
Definition vdec_hw_h264.c:34
XINE_STREAM_INFO_AUDIO_BITRATE
#define XINE_STREAM_INFO_AUDIO_BITRATE
Definition xine.h:1036
XINE_META_INFO_AUDIOCODEC
#define XINE_META_INFO_AUDIOCODEC
Definition xine.h:1086
XINE_VERBOSITY_DEBUG
#define XINE_VERBOSITY_DEBUG
Definition xine.h:430
XINE_STREAM_INFO_AUDIO_SAMPLERATE
#define XINE_STREAM_INFO_AUDIO_SAMPLERATE
Definition xine.h:1035
xine_a52_parser_reset
static void xine_a52_parser_reset(xine_a52_parser_t *this)
Definition xine_a52_parser.h:85
do_swab
static void do_swab(uint8_t *p, uint8_t *end)
Definition xine_a52_parser.h:59
a52_meta_info_set
static void a52_meta_info_set(xine_stream_t *stream, int a52_flags, int bit_rate, int sample_rate)
Definition xine_a52_parser.h:53
xine_a52_parse_data
static size_t xine_a52_parse_data(xine_a52_parser_t *this, xine_stream_t *stream, const uint8_t *data, size_t size)
Definition xine_a52_parser.h:90
a52_channel_info
static const char * a52_channel_info(int a52_flags)
Definition xine_a52_parser.h:32
A52_3F1R
#define A52_3F1R
Definition xine_a52_spdif.c:55
A52_MONO
#define A52_MONO
Definition xine_a52_spdif.c:51
A52_3F
#define A52_3F
Definition xine_a52_spdif.c:53
A52_3F2R
#define A52_3F2R
Definition xine_a52_spdif.c:57
A52_CHANNEL_MASK
#define A52_CHANNEL_MASK
Definition xine_a52_spdif.c:59
A52_DOLBY
#define A52_DOLBY
Definition xine_a52_spdif.c:58
A52_2F1R
#define A52_2F1R
Definition xine_a52_spdif.c:54
A52_2F2R
#define A52_2F2R
Definition xine_a52_spdif.c:56
A52_STEREO
#define A52_STEREO
Definition xine_a52_spdif.c:52
A52_LFE
#define A52_LFE
Definition xine_a52_spdif.c:60
a52_syncinfo
static int a52_syncinfo(uint8_t *buf, int *flags, int *sample_rate, int *bit_rate)
Definition xine_a52_spdif.c:110
xine_internal.h
xineutils.h
xprintf
#define xprintf(xine, verbose,...)
Definition xineutils.h:685