codecparser: mpeg4 type error
[profile/ivi/gst-plugins-bad.git] / gst / mpegvideoparse / mpegpacketiser.h
1 /* GStreamer
2  * Copyright (C) <2007> Jan Schmidt <thaytan@mad.scientist.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19 #ifndef __MPEGPACKETISER_H__
20 #define __MPEGPACKETISER_H__
21
22 #include <gst/gst.h>
23 #include <gst/base/gstadapter.h>
24
25 typedef struct MPEGPacketiser MPEGPacketiser;
26 typedef struct MPEGBlockInfo MPEGBlockInfo;
27 typedef struct MPEGSeqHdr MPEGSeqHdr;
28 typedef struct MPEGPictureHdr MPEGPictureHdr;
29
30 /* Packet ID codes for different packet types we
31  * care about */
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
40
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
45
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
52
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
57
58 struct MPEGBlockInfo {
59   guint8 first_pack_type;
60   guint8 flags;
61
62   guint64 offset;
63   guint32 length;
64
65   GstClockTime ts;
66 };
67
68 struct MPEGSeqHdr
69 {
70   /* 0 for unknown, else 1 or 2 */
71   guint8 mpeg_version;
72
73   /* Pixel-Aspect Ratio from DAR code via set_par_from_dar */
74   gint par_w, par_h;
75   /* Width and Height of the video */
76   gint width, height;
77   /* Framerate */
78   gint fps_n, fps_d;
79   /* Bitrate */
80   guint bitrate;
81   /* Profile and level */
82   guint profile, level;
83
84   gboolean progressive;
85 };
86
87 struct MPEGPictureHdr
88 {
89   guint8 pic_type;
90 };
91
92 struct MPEGPacketiser {
93   GstAdapter *adapter;
94   /* position in the adapter */
95   guint64 adapter_offset;
96
97   /* Sync word accumulator */
98   guint32 sync_word;
99
100   /* Offset since the last flush (unrelated to incoming buffer offsets) */
101   guint64 tracked_offset;
102
103   /* Number of picture packets currently collected */
104   guint n_pictures;
105
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,
110    * of course. */ 
111   GstClockTime prev_buf_ts;
112   GstClockTime cur_buf_ts;
113
114   /* MPEG id of the previous SEQUENCE, PICTURE or GOP packet. 
115      MPEG_PACKET_NONE after a flush */
116   guint8  prev_sync_packet;
117
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 */
122   gint cur_block_idx;
123   gint first_block_idx;
124
125   /* An array of MPEGBlockInfo entries, used as a growable circular buffer
126    * indexed by cur_block_idx and bounded by last_block_idx */
127   gint n_blocks;
128   MPEGBlockInfo *blocks;
129 };
130
131 void mpeg_packetiser_init (MPEGPacketiser *p);
132 void mpeg_packetiser_free (MPEGPacketiser *p);
133
134 void mpeg_packetiser_add_buf (MPEGPacketiser *p, GstBuffer *buf);
135 void mpeg_packetiser_handle_eos (MPEGPacketiser *p);
136
137 void mpeg_packetiser_flush (MPEGPacketiser *p);
138
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);
141
142 /* Advance to the next data block */
143 void mpeg_packetiser_next_block (MPEGPacketiser *p);
144
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);
152
153 #endif