| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2002-2009, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: printpes.c $ |
|---|
| 11 | * $brcm_Revision: 3 $ |
|---|
| 12 | * $brcm_Date: 10/28/09 1:30p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: print out contents of PES stream |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /BSEAV/lib/bcmplayer/utils/printpes.c $ |
|---|
| 19 | * |
|---|
| 20 | * 3 10/28/09 1:30p erickson |
|---|
| 21 | * SW7405-3287: move common code to ts_utils |
|---|
| 22 | * |
|---|
| 23 | * 2 3/14/07 11:25a erickson |
|---|
| 24 | * PR28320: improve MPEG2 PES debug support |
|---|
| 25 | * |
|---|
| 26 | * Irvine_BSEAVSW_Devel/5 6/30/06 9:39a erickson |
|---|
| 27 | * PR21941: fix start code detection algorithm, and eliminate duplication |
|---|
| 28 | * by moving to ts_utils |
|---|
| 29 | * |
|---|
| 30 | * Irvine_BSEAVSW_Devel/4 1/6/06 10:16a erickson |
|---|
| 31 | * PR17108: updated for magnum basemodules and 64 bit cpus |
|---|
| 32 | * |
|---|
| 33 | * Irvine_BSEAVSW_Devel/3 12/6/05 4:34p erickson |
|---|
| 34 | * PR17108: fixed bitshift, print only most significant 32 bits, which is |
|---|
| 35 | * what we use |
|---|
| 36 | * |
|---|
| 37 | * Irvine_BSEAVSW_Devel/2 8/19/05 4:00p erickson |
|---|
| 38 | * PR16208: fixed cmdline |
|---|
| 39 | * |
|---|
| 40 | * Irvine_BSEAVSW_Devel/1 8/8/05 3:57p erickson |
|---|
| 41 | * PR16138: added printpes util |
|---|
| 42 | * |
|---|
| 43 | *************************************************************************/ |
|---|
| 44 | #include <stdlib.h> |
|---|
| 45 | #include <stdio.h> |
|---|
| 46 | #include <string.h> |
|---|
| 47 | #include <errno.h> |
|---|
| 48 | #include <stdbool.h> |
|---|
| 49 | #include <stdint.h> |
|---|
| 50 | #include "ts_utils.h" |
|---|
| 51 | |
|---|
| 52 | /* decode and print the pes header. buf points to the stream_id and is guaranteed |
|---|
| 53 | to point to whole pes header. */ |
|---|
| 54 | void print_pes_header(unsigned char *buf) |
|---|
| 55 | { |
|---|
| 56 | b_pes_header pes_header; |
|---|
| 57 | |
|---|
| 58 | b_get_pes_header(buf, &pes_header); |
|---|
| 59 | |
|---|
| 60 | printf(" packet_length %d\n", pes_header.packet_length); |
|---|
| 61 | if (pes_header.pes_type == b_pes_packet_type_pes) { |
|---|
| 62 | printf(" header_data_length %d\n", pes_header.header_data_length); |
|---|
| 63 | if (pes_header.pts_dts_flags == 2) { |
|---|
| 64 | printf(" pts 0x%08x\n", pes_header.pts); |
|---|
| 65 | } |
|---|
| 66 | else if (pes_header.pts_dts_flags == 3) { |
|---|
| 67 | printf(" pts 0x%08x\n", pes_header.pts); |
|---|
| 68 | printf(" dts 0x%08x\n", pes_header.dts); |
|---|
| 69 | } |
|---|
| 70 | else if (pes_header.pts_dts_flags == 0) { |
|---|
| 71 | /* no pts or dts */ |
|---|
| 72 | } |
|---|
| 73 | else { |
|---|
| 74 | printf("### forbidden pts_dts_flags value\n"); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /* TODO: ESCR flag, ES rate flag, DSM trick mode flag, etc. */ |
|---|
| 78 | } |
|---|
| 79 | else if (pes_header.pes_type == b_pes_packet_type_data) { |
|---|
| 80 | printf(" data\n"); |
|---|
| 81 | } |
|---|
| 82 | else if (pes_header.pes_type == b_pes_packet_type_padding) { |
|---|
| 83 | /* padding stream */ |
|---|
| 84 | printf(" padding\n"); |
|---|
| 85 | } |
|---|
| 86 | else { |
|---|
| 87 | /* not a PES stream_id */ |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | int main(int argc, char **argv) { |
|---|
| 92 | FILE *fin = stdin; |
|---|
| 93 | #define BUFSIZE 4096 |
|---|
| 94 | unsigned char buf[BUFSIZE]; |
|---|
| 95 | int sccount = 0; |
|---|
| 96 | int curarg = 1; |
|---|
| 97 | uint64_t fileoffset = 0; |
|---|
| 98 | |
|---|
| 99 | if (argc > 1 && !strcmp(argv[curarg], "--help")) { |
|---|
| 100 | printf("Usage: printpes [FILENAME]\n"); |
|---|
| 101 | exit(0); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | if (curarg < argc) { |
|---|
| 105 | fin = fopen(argv[curarg], "r"); |
|---|
| 106 | if (!fin) |
|---|
| 107 | return printf("Unable to open %s: %d\n", argv[curarg], errno); |
|---|
| 108 | } |
|---|
| 109 | else { |
|---|
| 110 | printf("Reading stdin\n"); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | while (!feof(fin)) { |
|---|
| 114 | int i; |
|---|
| 115 | int n; |
|---|
| 116 | |
|---|
| 117 | n = fread(buf, 1, BUFSIZE, fin); |
|---|
| 118 | if (n == -1) break; |
|---|
| 119 | |
|---|
| 120 | /* search for start codes */ |
|---|
| 121 | for (i=0; i<n; i++) { |
|---|
| 122 | if (sccount == 3) { |
|---|
| 123 | if (b_is_pes_stream_id(buf[i])) { |
|---|
| 124 | /* TODO: don't know actual max */ |
|---|
| 125 | #define MAX_HEADER_SIZE 100 |
|---|
| 126 | /* check if we have enough to process the header */ |
|---|
| 127 | if (n - i < MAX_HEADER_SIZE) { |
|---|
| 128 | /* move the remainder of the old read to the head of the buffer */ |
|---|
| 129 | memcpy(buf, &buf[i], n-i); |
|---|
| 130 | fileoffset += i; |
|---|
| 131 | n -= i; |
|---|
| 132 | i = 0; |
|---|
| 133 | |
|---|
| 134 | /* read more */ |
|---|
| 135 | n += fread(&buf[n], 1, BUFSIZE-n, fin); |
|---|
| 136 | if (n < MAX_HEADER_SIZE) |
|---|
| 137 | break; |
|---|
| 138 | } |
|---|
| 139 | printf("stream_id %02x at %#lx\n", buf[i], (unsigned long)(fileoffset+i)); |
|---|
| 140 | print_pes_header(&buf[i]); |
|---|
| 141 | } |
|---|
| 142 | sccount = 0; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | sccount = b_check_for_start_code(buf[i], sccount); |
|---|
| 146 | } |
|---|
| 147 | fileoffset += n; |
|---|
| 148 | } |
|---|
| 149 | return 0; |
|---|
| 150 | } |
|---|