2 * Copyright (C) <2007> Jan Schmidt <thaytan@mad.scientist.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 #ifndef __MPEGPACKETISER_H__
20 #define __MPEGPACKETISER_H__
23 #include <gst/base/gstadapter.h>
25 typedef struct MPEGPacketiser MPEGPacketiser;
26 typedef struct MPEGBlockInfo MPEGBlockInfo;
27 typedef struct MPEGSeqHdr MPEGSeqHdr;
28 typedef struct MPEGPictureHdr MPEGPictureHdr;
30 /* Packet ID codes for different packet types we
32 #define MPEG_PACKET_PICTURE 0x00
33 #define MPEG_PACKET_SLICE_MIN 0x01
34 #define MPEG_PACKET_SLICE_MAX 0xaf
35 #define MPEG_PACKET_SEQUENCE 0xb3
36 #define MPEG_PACKET_EXTENSION 0xb5
37 #define MPEG_PACKET_SEQUENCE_END 0xb7
38 #define MPEG_PACKET_GOP 0xb8
39 #define MPEG_PACKET_NONE 0xff
41 /* Extension codes we care about */
42 #define MPEG_PACKET_EXT_SEQUENCE 0x01
43 #define MPEG_PACKET_EXT_SEQUENCE_DISPLAY 0x02
44 #define MPEG_PACKET_EXT_QUANT_MATRIX 0x03
46 /* Flags indicating what type of packets are in this block, some are mutually
47 * exclusive though - ie, sequence packs are accumulated separately. GOP &
48 * Picture may occur together or separately */
49 #define MPEG_BLOCK_FLAG_SEQUENCE 0x01
50 #define MPEG_BLOCK_FLAG_PICTURE 0x02
51 #define MPEG_BLOCK_FLAG_GOP 0x04
53 #define MPEG_PICTURE_TYPE_I 0x01
54 #define MPEG_PICTURE_TYPE_P 0x02
55 #define MPEG_PICTURE_TYPE_B 0x03
56 #define MPEG_PICTURE_TYPE_D 0x04
58 struct MPEGBlockInfo {
59 guint8 first_pack_type;
70 /* 0 for unknown, else 1 or 2 */
73 /* Pixel-Aspect Ratio from DAR code via set_par_from_dar */
75 /* Width and Height of the video */
81 /* Profile and level */
92 struct MPEGPacketiser {
94 /* position in the adapter */
95 guint64 adapter_offset;
97 /* Sync word accumulator */
100 /* Offset since the last flush (unrelated to incoming buffer offsets) */
101 guint64 tracked_offset;
103 /* Number of picture packets currently collected */
106 /* 2 sets of timestamps + offsets used to mark picture blocks
107 * The first is used when a sync word overlaps packet boundaries
108 * and comes from some buffer in the past. The next one comes from current
109 * buffer. These are only ever valid when handling streams from a demuxer,
111 GstClockTime prev_buf_ts;
112 GstClockTime cur_buf_ts;
114 /* MPEG id of the previous SEQUENCE, PICTURE or GOP packet.
115 MPEG_PACKET_NONE after a flush */
116 guint8 prev_sync_packet;
118 /* Indices into the blocks array. cur_block_idx is where we're writing and
119 indicates the end of the populated block entries.
120 first_block_idx is the read ptr. It may be -1 to indicate there are no
121 complete blocks available */
123 gint first_block_idx;
125 /* An array of MPEGBlockInfo entries, used as a growable circular buffer
126 * indexed by cur_block_idx and bounded by last_block_idx */
128 MPEGBlockInfo *blocks;
131 void mpeg_packetiser_init (MPEGPacketiser *p);
132 void mpeg_packetiser_free (MPEGPacketiser *p);
134 void mpeg_packetiser_add_buf (MPEGPacketiser *p, GstBuffer *buf);
135 void mpeg_packetiser_handle_eos (MPEGPacketiser *p);
137 void mpeg_packetiser_flush (MPEGPacketiser *p);
139 /* Get the blockinfo and buffer for the block at the head of the queue */
140 MPEGBlockInfo *mpeg_packetiser_get_block (MPEGPacketiser *p, GstBuffer **buf);
142 /* Advance to the next data block */
143 void mpeg_packetiser_next_block (MPEGPacketiser *p);
145 /* Utility functions for parsing MPEG packets */
146 guint8 *mpeg_util_find_start_code (guint32 *sync_word,
147 guint8 *cur, guint8 *end);
148 gboolean mpeg_util_parse_sequence_hdr (MPEGSeqHdr *hdr,
149 guint8 *data, guint8 *end);
150 gboolean mpeg_util_parse_picture_hdr (MPEGPictureHdr *hdr,
151 guint8 *data, guint8 *end);