Debian Codegraph @

bento4-embedded/Source/C++/Apps/AvcInfo/AvcInfo.cpp

raw
1 /*****************************************************************
2 |
3 | AP4 - AVC Bitstream Stream Info
4 |
5 | Copyright 2002-2008 Axiomatic Systems, LLC
6 |
7 |
8 | This file is part of Bento4/AP4 (MP4 Atom Processing Library).
9 |
10 | Unless you have obtained Bento4 under a difference license,
11 | this version of Bento4 is Bento4|GPL.
12 | Bento4|GPL is free software; you can redistribute it and/or modify
13 | it under the terms of the GNU General Public License as published by
14 | the Free Software Foundation; either version 2, or (at your option)
15 | any later version.
16 |
17 | Bento4|GPL is distributed in the hope that it will be useful,
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 | GNU General Public License for more details.
21 |
22 | You should have received a copy of the GNU General Public License
23 | along with Bento4|GPL; see the file COPYING. If not, write to the
24 | Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 | 02111-1307, USA.
26 |
27 ****************************************************************/
28
29 /*----------------------------------------------------------------------
30 | includes
31 +---------------------------------------------------------------------*/
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "Ap4.h"
36 #include "Ap4AvcParser.h"
37 #include "Ap4BitStream.h"
38
39 /*----------------------------------------------------------------------
40 | constants
41 +---------------------------------------------------------------------*/
42 #define BANNER "AVC Bitstream Info - Version 1.0\n"\
43 "(Bento4 Version " AP4_VERSION_STRING ")\n"\
44 "(c) 2002-2009 Axiomatic Systems, LLC"
45
46 /*----------------------------------------------------------------------
47 | PrintUsageAndExit
48 +---------------------------------------------------------------------*/
49 static void
50 PrintUsageAndExit()
51 {
52 fprintf(stderr,
53 BANNER
54 "\n\nusage: avcinfo [options] <input>\n"
55 "Options:\n"
56 " --verbose: show more details\n");
57 exit(1);
58 }
59
60 /*----------------------------------------------------------------------
61 | ReadGolomb
62 +---------------------------------------------------------------------*/
63 static unsigned int
64 ReadGolomb(AP4_BitStream& bits)
65 {
66 unsigned int leading_zeros = 0;
67 while (bits.ReadBit() == 0) {
68 leading_zeros++;
69 }
70 if (leading_zeros) {
71 return (1<<leading_zeros)-1+bits.ReadBits(leading_zeros);
72 } else {
73 return 0;
74 }
75 }
76
77 /*----------------------------------------------------------------------
78 | PrintSliceInfo
79 +---------------------------------------------------------------------*/
80 static void
81 PrintSliceInfo(const unsigned char* data)
82 {
83 AP4_BitStream bits;
84 bits.WriteBytes(data, 8);
85 ReadGolomb(bits);
86
87 unsigned int slice_type = ReadGolomb(bits);
88 const char* slice_type_name = AP4_AvcNalParser::SliceTypeName(slice_type);
89 if (slice_type_name == NULL) slice_type_name = "?";
90 printf(" slice=%d (%s)", slice_type, slice_type_name);
91 }
92
93 /*----------------------------------------------------------------------
94 | main
95 +---------------------------------------------------------------------*/
96 int
97 main(int argc, char** argv)
98 {
99 if (argc < 2) {
100 PrintUsageAndExit();
101 }
102 const char* filename = NULL;
103 //bool verbose = false;
104
105 while (char* arg = *++argv) {
106 if (!strcmp(arg, "--verbose")) {
107 //verbose = true;
108 } else {
109 if (filename == NULL) {
110 filename = arg;
111 } else {
112 fprintf(stderr, "ERROR: unexpected argument '%s'\n", arg);
113 return 1;
114 }
115 }
116 }
117 if (filename == NULL) {
118 fprintf(stderr, "ERROR: filename missing\n");
119 return 1;
120 }
121
122 AP4_ByteStream* input = NULL;
123 AP4_Result result = AP4_FileByteStream::Create(filename,
124 AP4_FileByteStream::STREAM_MODE_READ,
125 input);
126 if (AP4_FAILED(result)) {
127 fprintf(stderr, "ERROR: cannot open input file %s (%d)\n", filename, result);
128 return 1;
129 }
130
131 AP4_AvcNalParser parser;
132 unsigned int nalu_count = 0;
133 for (;;) {
134 bool eos;
135 unsigned char input_buffer[4096];
136 AP4_Size bytes_in_buffer = 0;
137 result = input->ReadPartial(input_buffer, sizeof(input_buffer), bytes_in_buffer);
138 if (AP4_SUCCEEDED(result)) {
139 eos = false;
140 } else if (result == AP4_ERROR_EOS) {
141 eos = true;
142 } else {
143 fprintf(stderr, "ERROR: failed to read from input file\n");
144 break;
145 }
146 AP4_Size offset = 0;
147 do {
148 const AP4_DataBuffer* nalu = NULL;
149 AP4_Size bytes_consumed = 0;
150 result = parser.Feed(&input_buffer[offset], bytes_in_buffer, bytes_consumed, nalu, eos);
151 if (AP4_FAILED(result)) {
152 fprintf(stderr, "ERROR: Feed() failed (%d)\n", result);
153 break;
154 }
155 if (nalu) {
156 const unsigned char* nalu_payload = (const unsigned char*)nalu->GetData();
157 unsigned int nalu_type = nalu_payload[0]&0x1F;
158 const char* nalu_type_name = AP4_AvcNalParser::NaluTypeName(nalu_type);
159 if (nalu_type_name == NULL) nalu_type_name = "UNKNOWN";
160 printf("NALU %5d: size=%5d, type=%02d (%s)",
161 nalu_count,
162 (int)nalu->GetDataSize(),
163 nalu_type,
164 nalu_type_name);
165 if (nalu_type == 9) {
166 unsigned int primary_pic_type = (nalu_payload[1]>>5);
167 const char* primary_pic_type_name = AP4_AvcNalParser::PrimaryPicTypeName(primary_pic_type);
168 if (primary_pic_type_name == NULL) primary_pic_type_name = "UNKNOWN";
169 printf(" [%d:%s]\n", primary_pic_type, primary_pic_type_name);
170 } else if ((nalu_type == 1 || nalu_type == 2 || nalu_type == 5 || nalu_type == 19)
171 && nalu->GetDataSize() > 1) {
172 PrintSliceInfo(&nalu_payload[1]);
173 printf("\n");
174 } else {
175 printf("\n");
176 }
177 nalu_count++;
178 }
179 offset += bytes_consumed;
180 bytes_in_buffer -= bytes_consumed;
181 } while (bytes_in_buffer);
182 if (eos) break;
183 }
184
185 input->Release();
186
187 return 0;
188 }