| 1 | /***************************************************************** |
| 2 | | |
| 3 | | AP4 - HEVC Bitstream Stream Info |
| 4 | | |
| 5 | | Copyright 2002-2014 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 "Ap4HevcParser.h" |
| 37 | #include "Ap4BitStream.h" |
| 38 | |
| 39 | /*---------------------------------------------------------------------- |
| 40 | | constants |
| 41 | +---------------------------------------------------------------------*/ |
| 42 | #define BANNER "HEVC Bitstream Info - Version 1.0\n"\ |
| 43 | "(Bento4 Version " AP4_VERSION_STRING ")\n"\ |
| 44 | "(c) 2002-2014 Axiomatic Systems, LLC" |
| 45 | |
| 46 | /*---------------------------------------------------------------------- |
| 47 | | PrintUsageAndExit |
| 48 | +---------------------------------------------------------------------*/ |
| 49 | static void |
| 50 | PrintUsageAndExit() |
| 51 | { |
| 52 | fprintf(stderr, |
| 53 | BANNER |
| 54 | "\n\nusage: hevcinfo [options] <input>\n"); |
| 55 | exit(1); |
| 56 | } |
| 57 | |
| 58 | /*---------------------------------------------------------------------- |
| 59 | | main |
| 60 | +---------------------------------------------------------------------*/ |
| 61 | int |
| 62 | main(int argc, char** argv) |
| 63 | { |
| 64 | if (argc < 2) { |
| 65 | PrintUsageAndExit(); |
| 66 | } |
| 67 | const char* filename = NULL; |
| 68 | |
| 69 | while (char* arg = *++argv) { |
| 70 | if (filename == NULL) { |
| 71 | filename = arg; |
| 72 | } else { |
| 73 | fprintf(stderr, "ERROR: unexpected argument '%s'\n", arg); |
| 74 | return 1; |
| 75 | } |
| 76 | } |
| 77 | if (filename == NULL) { |
| 78 | fprintf(stderr, "ERROR: filename missing\n"); |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | AP4_ByteStream* input = NULL; |
| 83 | AP4_Result result = AP4_FileByteStream::Create(filename, |
| 84 | AP4_FileByteStream::STREAM_MODE_READ, |
| 85 | input); |
| 86 | if (AP4_FAILED(result)) { |
| 87 | fprintf(stderr, "ERROR: cannot open input file %s (%d)\n", filename, result); |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | AP4_HevcNalParser parser; |
| 92 | unsigned int nalu_count = 0; |
| 93 | for (;;) { |
| 94 | bool eos; |
| 95 | unsigned char input_buffer[4096]; |
| 96 | AP4_Size bytes_in_buffer = 0; |
| 97 | result = input->ReadPartial(input_buffer, sizeof(input_buffer), bytes_in_buffer); |
| 98 | if (AP4_SUCCEEDED(result)) { |
| 99 | eos = false; |
| 100 | } else if (result == AP4_ERROR_EOS) { |
| 101 | eos = true; |
| 102 | } else { |
| 103 | fprintf(stderr, "ERROR: failed to read from input file\n"); |
| 104 | break; |
| 105 | } |
| 106 | AP4_Size offset = 0; |
| 107 | do { |
| 108 | const AP4_DataBuffer* nalu = NULL; |
| 109 | AP4_Size bytes_consumed = 0; |
| 110 | result = parser.Feed(&input_buffer[offset], bytes_in_buffer, bytes_consumed, nalu, eos); |
| 111 | if (AP4_FAILED(result)) { |
| 112 | fprintf(stderr, "ERROR: Feed() failed (%d)\n", result); |
| 113 | break; |
| 114 | } |
| 115 | if (nalu) { |
| 116 | const unsigned char* nalu_payload = (const unsigned char*)nalu->GetData(); |
| 117 | unsigned int nalu_type = (nalu_payload[0] >> 1) & 0x3F; |
| 118 | const char* nalu_type_name = AP4_HevcNalParser::NaluTypeName(nalu_type); |
| 119 | if (nalu_type_name == NULL) nalu_type_name = "UNKNOWN"; |
| 120 | printf("NALU %5d: size=%5d, type=%02d (%s)", |
| 121 | nalu_count, |
| 122 | (int)nalu->GetDataSize(), |
| 123 | nalu_type, |
| 124 | nalu_type_name); |
| 125 | if (nalu_type == AP4_HEVC_NALU_TYPE_AUD_NUT && nalu->GetDataSize() > 1) { |
| 126 | // Access Unit Delimiter |
| 127 | unsigned int pic_type = (nalu_payload[1]>>5); |
| 128 | const char* pic_type_name = AP4_HevcNalParser::PicTypeName(pic_type); |
| 129 | if (pic_type_name == NULL) pic_type_name = "UNKNOWN"; |
| 130 | printf(" [%d:%s]\n", pic_type, pic_type_name); |
| 131 | } else { |
| 132 | printf("\n"); |
| 133 | } |
| 134 | nalu_count++; |
| 135 | } |
| 136 | offset += bytes_consumed; |
| 137 | bytes_in_buffer -= bytes_consumed; |
| 138 | } while (bytes_in_buffer); |
| 139 | if (eos) break; |
| 140 | } |
| 141 | |
| 142 | input->Release(); |
| 143 | |
| 144 | return 0; |
| 145 | } |