| [2] | 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2006, 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: rmscprevent.c $ |
|---|
| 11 | * $brcm_Revision: Irvine_BSEAVSW_Devel/1 $ |
|---|
| 12 | * $brcm_Date: 4/12/06 2:20p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: remove start code emulation prevention from ES stream |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /SetTop/bcmplayer/utils/rmscprevent.c $ |
|---|
| 19 | * |
|---|
| 20 | * Irvine_BSEAVSW_Devel/1 4/12/06 2:20p erickson |
|---|
| 21 | * PR17108: added util to remove start code emulation prevention |
|---|
| 22 | * |
|---|
| 23 | *************************************************************************/ |
|---|
| 24 | #include <stdlib.h> |
|---|
| 25 | #include <stdio.h> |
|---|
| 26 | #include <string.h> |
|---|
| 27 | #include <errno.h> |
|---|
| 28 | #include <assert.h> |
|---|
| 29 | |
|---|
| 30 | int main(int argc, char **argv) { |
|---|
| 31 | int fileoffset = 0; |
|---|
| 32 | #define BUFSIZE 4096 |
|---|
| 33 | unsigned char buf[BUFSIZE]; |
|---|
| 34 | int zero_count = 0; |
|---|
| 35 | int curarg = 1; |
|---|
| 36 | int possible_emulation = 0; |
|---|
| 37 | |
|---|
| 38 | if (curarg < argc && !strcmp(argv[curarg], "--help")) { |
|---|
| 39 | fprintf(stderr, "Usage: rmscprevent <infile >outfile\n" |
|---|
| 40 | " Removes start code emulation prevention from ES streams. Uses stdin/stdout.\n"); |
|---|
| 41 | exit(0); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | while (!feof(stdin)) { |
|---|
| 45 | int i, n; |
|---|
| 46 | |
|---|
| 47 | n = fread(&buf[possible_emulation], 1, BUFSIZE-possible_emulation, stdin); |
|---|
| 48 | if (n <= 0) break; |
|---|
| 49 | |
|---|
| 50 | for (i=possible_emulation; i<n; i++) { |
|---|
| 51 | if (possible_emulation) { |
|---|
| 52 | if (buf[i] <= 3) { |
|---|
| 53 | /* remove 0x03 */ |
|---|
| 54 | assert(buf[i-1] == 3); |
|---|
| 55 | memmove(&buf[i-1], &buf[i], n-i); |
|---|
| 56 | i--; |
|---|
| 57 | n--; |
|---|
| 58 | possible_emulation = 0; |
|---|
| 59 | continue; |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | possible_emulation = 0; |
|---|
| 64 | |
|---|
| 65 | switch (buf[i]) { |
|---|
| 66 | case 0: |
|---|
| 67 | if (zero_count >= 1) |
|---|
| 68 | zero_count = 2; |
|---|
| 69 | else |
|---|
| 70 | zero_count = 1; |
|---|
| 71 | break; |
|---|
| 72 | case 3: |
|---|
| 73 | if (zero_count == 2) { |
|---|
| 74 | possible_emulation = 1; |
|---|
| 75 | } |
|---|
| 76 | /* fall through */ |
|---|
| 77 | default: |
|---|
| 78 | zero_count = 0; |
|---|
| 79 | break; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | fileoffset += n; |
|---|
| 83 | |
|---|
| 84 | /* don't write last byte of possible emulation byte */ |
|---|
| 85 | if (possible_emulation) { |
|---|
| 86 | n--; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /* write the result out */ |
|---|
| 90 | fwrite(buf, 1, n, stdout); |
|---|
| 91 | |
|---|
| 92 | if (possible_emulation) { |
|---|
| 93 | /* move the 0x03 from the last byte to the first of the next buffer */ |
|---|
| 94 | buf[0] = buf[n]; |
|---|
| 95 | assert(buf[0] == 3); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | return 0; |
|---|
| 99 | } |
|---|