[go: up one dir, main page]

File: es_defns.h

package info (click to toggle)
tstools 1.11-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 2,268 kB
  • ctags: 2,040
  • sloc: ansic: 33,549; java: 2,243; makefile: 343; python: 263; sh: 5
file content (151 lines) | stat: -rw-r--r-- 5,838 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Datastructures for handling H.264 elementary streams.
 *
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the MPEG TS, PS and ES tools.
 *
 * The Initial Developer of the Original Code is Amino Communications Ltd.
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Amino Communications Ltd, Swavesey, Cambridge UK
 *
 * ***** END LICENSE BLOCK *****
 */

#ifndef _es_defns
#define _es_defns

#include <stdio.h>
#include "compat.h"
#include "pes_defns.h"

// ------------------------------------------------------------
// A "file" offset in an ES stream, suitable for seeking to
// For an ES based on a "bare" file, the `infile` value is all that is needed
// For an ES based on a PES, the file offset of the PES packet and the byte
// offset within that packet's ES data are needed.
struct _ES_offset
{
  offset_t  infile;   // as used by lseek
  int32_t   inpacket;
};
typedef struct _ES_offset  ES_offset;
typedef struct _ES_offset *ES_offset_p;
#define SIZEOF_ES_OFFSET sizeof(struct _ES_offset)

// The number of bytes to "read ahead" when reading directly from an
// elementary stream
#define ES_READ_AHEAD_SIZE  1000

// ------------------------------------------------------------
// A datastructure to represent our input elementary stream (ES)
// (*output* elementary streams shouldn't need any particular housekeeping)
struct elementary_stream
{
  int       reading_ES;  // TRUE if we're reading ES data direct, FALSE if PES

  // If we're reading from an elementary data stream directly, then
  // we use the input directly
  int       input;
  // And maintain a buffer of "read ahead" bytes
  byte      read_ahead[ES_READ_AHEAD_SIZE];
  offset_t  read_ahead_posn;   // location of this data in the file
  int32_t   read_ahead_len;    // actual number of bytes in the buffer
  
  // And the next byte to be read is specified by its offset in said
  // data stream. For "bare" ES data, the `infile` value is used to
  // remember the next bytes actual position in the file, and for PES
  // based ES data, the `inpacket` value is used to remember the next
  // bytes offset in the current PES packet. In both cases, the "unused"
  // quantity in the ES_offset is undefined.
  // (this is, in fact, more used by tsserve than by anything else)
  ES_offset  posn_of_next_byte;

  // If we're reading from PES packets (from either a PS or TS file),
  // then we need to remember our PES reader
  PES_reader_p  reader;

  byte      *data;               // Where we're reading our bytes from
  byte      *data_end;           // How to tell we've read them all
  byte      *data_ptr;           // And which byte we're interested in

  offset_t   last_packet_posn;   // Where the last PES packet was in the file
  int32_t    last_packet_es_data_len;  // And its number of ES bytes
  
  // Regardless, our triple byte memory is the same
  byte      cur_byte;    // The current (last read) byte
  byte      prev1_byte;  // The previous byte
  byte      prev2_byte;  // The byte before *that*
};
typedef struct elementary_stream *ES_p;
#define SIZEOF_ES sizeof(struct elementary_stream)

// ------------------------------------------------------------
// And a representation of a single unit from the elementary stream
// (whether an MPEG-2 (H.262) item, or an MPEG-4/AVC (H.264) NAL unit)
// - basically, the thing that starts with a 00 00 01 prefix, and continues
// to end of file or before the next 00 00 01 prefix (so note that it
// contains any "trailing" 00 bytes).
//
// The normal way to acquire such a datastructure is via `build_ES_unit()`,
// or `find_and_build_next_ES_unit()`. If instead you want to use the
// address of a `struct ES_unit`, it is imperative that it be set up
// correctly with `setup_ES_unit()` before it is passed to any of the
// functions that use it, otherwise the contents will not be valid (and,
// particularly, the "data" pointer will reference random memory).
struct ES_unit
{
  ES_offset start_posn;   // The start of the current data unit
  byte     *data;         // Its data, including the leading 00 00 01
  uint32_t  data_len;     // Its length
  uint32_t  data_size;    // The total buffer size

  byte      start_code;   // The byte after the 00 00 01 prefix

  // Something of a hack - if we were reading PES, did any of the PES packets
  // we read to make this ES unit contain a PTS?
  byte      PES_had_PTS;
};
typedef struct ES_unit *ES_unit_p;
#define SIZEOF_ES_UNIT sizeof(struct ES_unit)

// Start and increment sizes for the es_unit/data array.
#define ES_UNIT_DATA_START_SIZE  1000  // was 500
#define ES_UNIT_DATA_INCREMENT    500  // was 100

// ------------------------------------------------------------
// An expandable list of ES units
struct ES_unit_list
{
  struct ES_unit *array;   // The current array of ES units
  int             length;  // How many there are
  int             size;    // How big the array is
};
typedef struct ES_unit_list *ES_unit_list_p;
#define SIZEOF_ES_UNIT_LIST sizeof(struct ES_unit_list)

#define ES_UNIT_LIST_START_SIZE  20
#define ES_UNIT_LIST_INCREMENT   20

#endif // _es_defns

// Local Variables:
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 2
// End:
// vim: set tabstop=8 shiftwidth=2 expandtab: