| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2005-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: mkbtpfile.c $ |
|---|
| 11 | * $brcm_Revision: 3 $ |
|---|
| 12 | * $brcm_Date: 7/13/09 10:26a $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: Transport Stream Index Player |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /BSEAV/lib/bcmplayer/utils/mkbtpfile.c $ |
|---|
| 19 | * |
|---|
| 20 | * 3 7/13/09 10:26a erickson |
|---|
| 21 | * PR56423: clarify eBpPlayDecoderGOPTrick, remove unused |
|---|
| 22 | * eBpPlaySTCFastForward |
|---|
| 23 | * |
|---|
| 24 | * 2 4/30/07 1:10p erickson |
|---|
| 25 | * PR30310: expanded supported playmodes |
|---|
| 26 | * |
|---|
| 27 | * Irvine_BSEAVSW_Devel/7 1/6/06 10:16a erickson |
|---|
| 28 | * PR17108: updated for magnum basemodules and 64 bit cpus |
|---|
| 29 | * |
|---|
| 30 | * Irvine_BSEAVSW_Devel/6 8/9/05 2:33p erickson |
|---|
| 31 | * PR16208: uclibc gets doesn't overwrite n with 0 |
|---|
| 32 | * |
|---|
| 33 | * Irvine_BSEAVSW_Devel/5 6/14/05 7:50a erickson |
|---|
| 34 | * PR15861: added host trick mode support |
|---|
| 35 | * |
|---|
| 36 | * Irvine_BSEAVSW_Devel/4 3/17/05 9:52a erickson |
|---|
| 37 | * PR13202: added speed and start location prompts |
|---|
| 38 | * |
|---|
| 39 | * Irvine_BSEAVSW_Devel/3 3/9/05 12:28p erickson |
|---|
| 40 | * PR13202: added zeroByteCountEnd logic |
|---|
| 41 | * |
|---|
| 42 | ***********************************************************/ |
|---|
| 43 | #include "bstd.h" |
|---|
| 44 | #include "bkni.h" |
|---|
| 45 | #include "bcmplayer.h" |
|---|
| 46 | #include <stdio.h> |
|---|
| 47 | #include <assert.h> |
|---|
| 48 | #include <string.h> |
|---|
| 49 | #include <stdlib.h> |
|---|
| 50 | |
|---|
| 51 | static struct { |
|---|
| 52 | eBpPlayModeParam playMode; |
|---|
| 53 | const char *name; |
|---|
| 54 | } g_playModes[] = { |
|---|
| 55 | {eBpPlayNormal, "normal"}, |
|---|
| 56 | {eBpPlayNormalByFrames, "normalbyframes"}, |
|---|
| 57 | {eBpPlayI, "i"}, |
|---|
| 58 | {eBpPlaySkipB, "skipb"}, |
|---|
| 59 | {eBpPlayIP, "ip"}, |
|---|
| 60 | {eBpPlayBrcm, "brcm"}, |
|---|
| 61 | {eBpPlayDecoderGOPTrick, "gop"}, |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | static eBpPlayModeParam g_convert_playModeStr(const char *name) |
|---|
| 65 | { |
|---|
| 66 | unsigned i = 0; |
|---|
| 67 | for (i=0;i<sizeof(g_playModes)/sizeof(g_playModes[0]);i++) { |
|---|
| 68 | if (!strcasecmp(name, g_playModes[i].name)) { |
|---|
| 69 | return g_playModes[i].playMode; |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | printf("invalid mode: %s\n", name); |
|---|
| 73 | return eBpPlayNormal; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | static const char *g_convert_playMode(eBpPlayModeParam playMode) |
|---|
| 77 | { |
|---|
| 78 | unsigned i = 0; |
|---|
| 79 | for (i=0;i<sizeof(g_playModes)/sizeof(g_playModes[0]);i++) { |
|---|
| 80 | if (playMode == g_playModes[i].playMode) { |
|---|
| 81 | return g_playModes[i].name; |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | return "unknown"; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | int main(int argc, char **argv) |
|---|
| 88 | { |
|---|
| 89 | BNAV_Player_Handle player; |
|---|
| 90 | BNAV_Player_Settings cfg; |
|---|
| 91 | BNAV_Player_PlayMode playMode; |
|---|
| 92 | BNAV_Player_PlayEntry bcmEntry; |
|---|
| 93 | FILE *mpegfile, *indexfile, *outfile; |
|---|
| 94 | int rc; |
|---|
| 95 | unsigned char packetBuffer[188]; |
|---|
| 96 | unsigned short pid; |
|---|
| 97 | char buf[256]; |
|---|
| 98 | int speed = -1, startindex = 200; |
|---|
| 99 | |
|---|
| 100 | BKNI_Init(); |
|---|
| 101 | BDBG_Init(); |
|---|
| 102 | BDBG_SetModuleLevel("bcmplayer", BDBG_eMsg); |
|---|
| 103 | |
|---|
| 104 | if (argc < 5) { |
|---|
| 105 | printf("usage: mkbtpfile datafile indexfile pid outputfile\n"); |
|---|
| 106 | return 1; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | mpegfile = fopen(argv[1], "r"); |
|---|
| 110 | indexfile = fopen(argv[2], "r"); |
|---|
| 111 | assert(mpegfile); |
|---|
| 112 | assert(indexfile); |
|---|
| 113 | |
|---|
| 114 | pid = strtoul(argv[3], NULL, 0); |
|---|
| 115 | outfile = fopen(argv[4], "w+"); |
|---|
| 116 | |
|---|
| 117 | printf("Select play mode ([Brcm], I, IP, SkipB, GOP, Normal, NormalByFrames):\n"); |
|---|
| 118 | fgets(buf, 256, stdin); |
|---|
| 119 | playMode.playMode = eBpPlayBrcm; |
|---|
| 120 | if (*buf && *buf != '\n') { |
|---|
| 121 | buf[strlen(buf)-1] = 0; |
|---|
| 122 | playMode.playMode = g_convert_playModeStr(buf); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | printf("Enter mode modifier (default: -1):\n"); |
|---|
| 126 | fgets(buf, 256, stdin); |
|---|
| 127 | if (*buf && *buf != '\n') |
|---|
| 128 | speed = atoi(buf); |
|---|
| 129 | |
|---|
| 130 | printf("Enter start index (default: 200):\n"); |
|---|
| 131 | fgets(buf, 256, stdin); |
|---|
| 132 | if (*buf && *buf != '\n') |
|---|
| 133 | startindex = atoi(buf); |
|---|
| 134 | |
|---|
| 135 | printf("Trick mode: %s, mode %d, start %d\n", |
|---|
| 136 | g_convert_playMode(playMode.playMode), speed, startindex); |
|---|
| 137 | |
|---|
| 138 | BNAV_Player_GetDefaultSettings(&cfg); |
|---|
| 139 | cfg.videoPid = pid; |
|---|
| 140 | cfg.filePointer = indexfile; |
|---|
| 141 | cfg.readCb = (BP_READ_CB)fread; |
|---|
| 142 | cfg.tellCb = (BP_TELL_CB)ftell; |
|---|
| 143 | cfg.seekCb = (BP_SEEK_CB)fseek; |
|---|
| 144 | |
|---|
| 145 | rc = BNAV_Player_Open(&player, &cfg); |
|---|
| 146 | assert(!rc); |
|---|
| 147 | |
|---|
| 148 | playMode.playModeModifier = speed; |
|---|
| 149 | playMode.loopMode = eBpSinglePlay; |
|---|
| 150 | playMode.disableExtraBOptimization = 0; |
|---|
| 151 | BNAV_Player_SetPlayMode(player, &playMode); |
|---|
| 152 | |
|---|
| 153 | BNAV_Player_SetCurrentIndex(player, startindex); |
|---|
| 154 | |
|---|
| 155 | while (!BNAV_Player_GetNextPlayEntry(player, &bcmEntry, packetBuffer)) { |
|---|
| 156 | if (bcmEntry.isInsertedPacket) { |
|---|
| 157 | fwrite(packetBuffer, 1, 188, outfile); |
|---|
| 158 | } |
|---|
| 159 | else { |
|---|
| 160 | #define BUFSIZE (64*1024) |
|---|
| 161 | char buf[BUFSIZE]; |
|---|
| 162 | int read = 0; |
|---|
| 163 | int zero_at = bcmEntry.byteCount - bcmEntry.zeroByteCountEnd; /* index |
|---|
| 164 | at which we start zeroing */ |
|---|
| 165 | |
|---|
| 166 | if (bcmEntry.zeroByteCountEnd) |
|---|
| 167 | printf("zero_at %d\n", zero_at); |
|---|
| 168 | |
|---|
| 169 | fseek(mpegfile, bcmEntry.startOffset, SEEK_SET); |
|---|
| 170 | while ((unsigned long)read < bcmEntry.byteCount) { |
|---|
| 171 | int n = bcmEntry.byteCount - read; /* amount we can/should read */ |
|---|
| 172 | if (n > BUFSIZE) |
|---|
| 173 | n = BUFSIZE; |
|---|
| 174 | if (fread(buf, 1, n, mpegfile) != (size_t)n) |
|---|
| 175 | return -1; |
|---|
| 176 | |
|---|
| 177 | /* zero out unwanted data */ |
|---|
| 178 | if (read+n >= (int)zero_at && bcmEntry.zeroByteCountEnd) { |
|---|
| 179 | if (read > zero_at) { |
|---|
| 180 | printf("zeroing %d, %d\n", read, n); |
|---|
| 181 | memset(&buf[read], 0, n); |
|---|
| 182 | } |
|---|
| 183 | else { |
|---|
| 184 | printf("zeroing %d, %d\n", zero_at-read, n-(zero_at-read)); |
|---|
| 185 | memset(&buf[zero_at-read], 0, n - (zero_at-read)); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | fwrite(buf, 1, n, outfile); |
|---|
| 190 | read += n; |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | fclose(mpegfile); |
|---|
| 196 | fclose(indexfile); |
|---|
| 197 | fclose(outfile); |
|---|
| 198 | return 0; |
|---|
| 199 | } |
|---|