FFmpeg
Loading...
Searching...
No Matches
mpegaudiodecheader.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19/**
20 * Walks the MPEG audio frames of a file, checks that every frame header
21 * encodes back to its original bits, and prints each distinct header.
22 */
23
24#include <inttypes.h>
25#include <stdint.h>
26#include <stdio.h>
27#include <string.h>
28
29#include "libavutil/file.h"
30#include "libavutil/mem.h"
32
34
35#define PADDING_BIT (1U << 9)
36
37static size_t id3v2_size(const uint8_t *buf, size_t size)
38{
39 if (size < 10 || memcmp(buf, "ID3", 3))
40 return 0;
41
42 return 10 + (buf[5] & 0x10 ? 10 : 0) +
43 ((buf[6] & 0x7f) << 21 | (buf[7] & 0x7f) << 14 |
44 (buf[8] & 0x7f) << 7 | (buf[9] & 0x7f));
45}
46
47static void print_header(size_t offset, unsigned frames, uint32_t bits,
49{
50 printf("offset=%zu frames=%u header=%08"PRIX32" layer=%d sample_rate=%d "
51 "bit_rate=%d crc=%d mode=%d mode_ext=%d copyright=%d original=%d "
52 "emphasis=%d private_bit=%d\n",
53 offset, frames, bits, header->layer, header->sample_rate,
54 header->bit_rate, header->error_protection, header->mode,
55 header->mode_ext, header->copyright, header->original,
56 header->emphasis, header->private_bit);
57}
58
59int main(int argc, char *argv[])
60{
61 MPADecodeHeader2 run_header, *header = NULL;
62 uint8_t *buf;
63 size_t size, pos, run_offset = 0;
64 uint32_t run_bits = 0;
65 unsigned run_frames = 0, total_frames = 0;
66 int ret = 1;
67
68 if (argc != 2) {
69 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
70 return 1;
71 }
72
73 if (av_file_map(argv[1], &buf, &size, 0, NULL) < 0) {
74 fprintf(stderr, "Failed to open '%s'\n", argv[1]);
75 return 1;
76 }
77
78 pos = id3v2_size(buf, size);
79 while (pos + 4 <= size) {
80 uint32_t bits = AV_RB32(buf + pos);
81 uint32_t encoded;
82 int decoded;
83
84 if (ff_mpa_check_header(bits) < 0) {
85 pos++;
86 continue;
87 }
88
89 if (header)
90 memset(header, 0, sizeof(*header));
91
93 if (decoded < 0)
94 goto end;
95
97 if (encoded != bits) {
98 printf("offset=%zu header=%08"PRIX32" encoded back as %08"PRIX32"\n",
99 pos, bits, encoded);
100 goto end;
101 }
102
103 if (run_frames && (bits & ~PADDING_BIT) != (run_bits & ~PADDING_BIT)) {
104 print_header(run_offset, run_frames, run_bits, &run_header);
105 run_frames = 0;
106 }
107 if (!run_frames) {
108 run_offset = pos;
109 run_bits = bits;
110 run_header = *header;
111 }
112 run_frames++;
113 total_frames++;
114
115 pos += decoded == 0 ? header->frame_size : 1;
116 }
117 if (run_frames)
118 print_header(run_offset, run_frames, run_bits, &run_header);
119
120 printf("frames=%u\n", total_frames);
121 ret = !total_frames;
122
123end:
125 av_file_unmap(buf, size);
126 return ret;
127}
static int frames
#define NULL
Definition coverity.c:32
__device__ int printf(const char *,...)
int main
Definition dovi_rpuenc.c:38
static const uint8_t bits[8]
Definition fastaudio.c:100
Misc file utilities.
static const uint8_t run_bits[7][16]
Definition h264_cavlc.c:227
#define run_offset(r)
Definition intrax8.c:123
#define AV_RB32(p)
unsigned offset
Definition libaomenc.c:763
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition file.c:142
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition file.c:55
Memory handling functions.
int avpriv_mpegaudio_decode_header2(MPADecodeHeader2 **phdr, uint32_t header)
Decode an MPEG audio header into *phdr, which is allocated if it is NULL.
MPEG Audio header decoder.
static uint32_t ff_mpa_encode_header(const MPADecodeHeader2 *s)
static int ff_mpa_check_header(uint32_t header)
static const uint8_t header[24]
Definition sdr2.c:68
unsigned int pos
Definition spdifenc.c:431
Same as MPADecodeHeader, plus the header fields that carry no decoding information.
#define av_freep(p)
static void print_header(size_t offset, unsigned frames, uint32_t bits, const MPADecodeHeader2 *header)
#define PADDING_BIT
Walks the MPEG audio frames of a file, checks that every frame header encodes back to its original bi...
static size_t id3v2_size(const uint8_t *buf, size_t size)
int size