2 * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
3 * Copyright (C) <2008> Dejan Sakelsak <dejan.sakelsak@marand.si>
4 * Copyright (C) <2009> Janin Kolenc <janin.kolenc@marand.si>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
29 #include <gst/rtp/gstrtpbuffer.h>
31 #include "gstrtph263pay.h"
35 GST_H263_FRAME_TYPE_I = 0,
36 GST_H263_FRAME_TYPE_P = 1,
37 GST_H263_FRAME_TYPE_PB = 2
38 } GstRtpH263PayFrameType;
42 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_RES1 = 0,
43 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_SQCIF = 1,
44 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_QCIF = 2,
45 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_CIF = 3,
46 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_4CIF = 4,
47 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_16CIF = 5,
48 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_RES2 = 6,
49 GST_H263_PAYLOAD_PICTURE_FORMAT_PLUS = 7
50 } GstRtpH263PayPictureFormat;
52 static guint format_props[8][2] = { {254, 254},
63 * I-frame MCBPC table: code, mask, nbits, cb, cr, mb type -> 10 = undefined (because we have guint)
67 static guint32 mcbpc_I[9][6] = {
68 {0x8000, 0x8000, 1, 0, 0, 3},
69 {0x2000, 0xe000, 3, 0, 1, 3},
70 {0x4000, 0xe000, 3, 1, 0, 3},
71 {0x6000, 0xe000, 3, 1, 1, 3},
72 {0x1000, 0xf000, 4, 0, 0, 4},
73 {0x0400, 0xfc00, 6, 0, 1, 4},
74 {0x0800, 0xfc00, 6, 1, 0, 4},
75 {0x0c00, 0xfc00, 6, 1, 1, 4},
76 {0x0080, 0xff80, 9, 10, 10, 10}
80 * P-frame MCBPC table: code, mask, nbits, cb, cr, mb type -> 10 = undefined (because we have guint)
82 #define MCBPC_P_LEN 21
84 static guint16 mcbpc_P[21][6] = {
85 {0x8000, 0x8000, 1, 0, 0, 0},
86 {0x3000, 0xf000, 4, 0, 1, 0},
87 {0x2000, 0xf000, 4, 1, 0, 0},
88 {0x1400, 0xfc00, 6, 1, 1, 0},
89 {0x6000, 0xe000, 3, 0, 0, 1},
90 {0x0e00, 0xfe00, 7, 0, 1, 1},
91 {0x0c00, 0xfe00, 7, 1, 0, 1},
92 {0x0280, 0xff80, 9, 1, 1, 1},
93 {0x4000, 0xe000, 3, 0, 0, 2},
94 {0x0a00, 0xfe00, 7, 0, 1, 2},
95 {0x0800, 0xfe00, 7, 1, 0, 2},
96 {0x0500, 0xff00, 8, 1, 1, 2},
97 {0x1800, 0xf800, 5, 0, 0, 3},
98 {0x0400, 0xff00, 8, 0, 1, 3},
99 {0x0300, 0xff00, 8, 1, 0, 3},
100 {0x0600, 0xfe00, 7, 1, 1, 3},
101 {0x1000, 0xfc00, 6, 0, 0, 4},
102 {0x0200, 0xff80, 9, 0, 1, 4},
103 {0x0180, 0xff80, 9, 1, 0, 4},
104 {0x0100, 0xff80, 9, 1, 1, 4},
105 {0x0080, 0xff80, 9, 10, 10, 10}
109 * I-frame CBPY (code, mask, nbits, Y0, Y1, Y2, Y3)
113 static guint8 cbpy_I[16][7] = {
114 {0x30, 0xf0, 4, 0, 0, 0, 0},
115 {0x28, 0xf8, 5, 0, 0, 0, 1},
116 {0x20, 0xf8, 5, 0, 0, 1, 0},
117 {0x90, 0xf0, 4, 0, 0, 1, 1},
118 {0x18, 0xf8, 5, 0, 1, 0, 0},
119 {0x70, 0xf0, 4, 0, 1, 0, 1},
120 {0x08, 0xfc, 6, 0, 1, 1, 0},
121 {0xb0, 0xf0, 4, 0, 1, 1, 1},
122 {0x10, 0xf8, 5, 1, 0, 0, 0},
123 {0x0c, 0xfc, 6, 1, 0, 0, 1},
124 {0x50, 0xf0, 4, 1, 0, 1, 0},
125 {0xa0, 0xf0, 4, 1, 0, 1, 1},
126 {0x40, 0xf0, 4, 1, 1, 0, 0},
127 {0x80, 0xf0, 4, 1, 1, 0, 1},
128 {0x60, 0xf0, 4, 1, 1, 1, 0},
129 {0xc0, 0xc0, 2, 1, 1, 1, 1}
133 * P-frame CBPY (code, mask, nbits, Y0, Y1, Y2, Y3)
135 static guint8 cbpy_P[16][7] = {
136 {0x30, 0xf0, 4, 1, 1, 1, 1},
137 {0x28, 0xf8, 5, 1, 1, 1, 0},
138 {0x20, 0xf8, 5, 1, 1, 0, 1},
139 {0x90, 0xf0, 4, 1, 1, 0, 0},
140 {0x18, 0xf8, 5, 1, 0, 1, 1},
141 {0x70, 0xf0, 4, 1, 0, 1, 0},
142 {0x08, 0xfc, 6, 1, 0, 0, 1},
143 {0xb0, 0xf0, 4, 1, 0, 0, 0},
144 {0x10, 0xf8, 5, 0, 1, 1, 1},
145 {0x0c, 0xfc, 6, 0, 1, 1, 0},
146 {0x50, 0xf0, 4, 0, 1, 0, 1},
147 {0xa0, 0xf0, 4, 0, 1, 0, 0},
148 {0x40, 0xf0, 4, 0, 0, 1, 1},
149 {0x80, 0xf0, 4, 0, 0, 1, 0},
150 {0x60, 0xf0, 4, 0, 0, 0, 1},
151 {0xc0, 0xc0, 2, 0, 0, 0, 0}
155 * Block TCOEF table (code, mask, nbits, LAST, RUN, LEVEL)
157 #define TCOEF_LEN 103
159 static guint16 tcoef[103][6] = {
160 {0x8000, 0xc000, 3, 0, 0, 1},
161 {0xf000, 0xf000, 5, 0, 0, 2},
162 {0x5400, 0xfc00, 7, 0, 0, 3},
163 {0x2e00, 0xfe00, 8, 0, 0, 4},
164 {0x1f00, 0xff00, 9, 0, 0, 5},
165 {0x1280, 0xff80, 10, 0, 0, 6},
166 {0x1200, 0xff80, 10, 0, 0, 7},
167 {0x0840, 0xffc0, 11, 0, 0, 8},
168 {0x0800, 0xffc0, 11, 0, 0, 9},
169 {0x00e0, 0xffe0, 12, 0, 0, 10}, //10
170 {0x00c0, 0xffe0, 12, 0, 0, 11},
171 {0x0400, 0xffe0, 12, 0, 0, 12},
172 {0xc000, 0xe000, 4, 0, 1, 1},
173 {0x5000, 0xfc00, 7, 0, 1, 2},
174 {0x1e00, 0xff00, 9, 0, 1, 3},
175 {0x03c0, 0xffc0, 11, 0, 1, 4},
176 {0x0420, 0xffe0, 12, 0, 1, 5},
177 {0x0500, 0xfff0, 13, 0, 1, 6},
178 {0xe000, 0xf000, 5, 0, 2, 1},
179 {0x1d00, 0xff00, 9, 0, 2, 2}, //20
180 {0x0380, 0xffc0, 11, 0, 2, 3},
181 {0x0510, 0xfff0, 13, 0, 2, 4},
182 {0x6800, 0xf800, 6, 0, 3, 1},
183 {0x1180, 0xff80, 10, 0, 3, 2},
184 {0x0340, 0xffc0, 11, 0, 3, 3},
185 {0x6000, 0xf800, 6, 0, 4, 1},
186 {0x1100, 0xff80, 10, 0, 4, 2},
187 {0x0520, 0xfff0, 13, 0, 4, 3},
188 {0x5800, 0xf800, 6, 0, 5, 1},
189 {0x0300, 0xffc0, 11, 0, 5, 2}, // 30
190 {0x0530, 0xfff0, 13, 0, 5, 3},
191 {0x4c00, 0xfc00, 7, 0, 6, 1},
192 {0x02c0, 0xffc0, 11, 0, 6, 2},
193 {0x0540, 0xfff0, 13, 0, 6, 3},
194 {0x4800, 0xfc00, 7, 0, 7, 1},
195 {0x0280, 0xffc0, 11, 0, 7, 2},
196 {0x4400, 0xfc00, 7, 0, 8, 1},
197 {0x0240, 0xffc0, 11, 0, 8, 2},
198 {0x4000, 0xfc00, 7, 0, 9, 1},
199 {0x0200, 0xffc0, 11, 0, 9, 2}, // 40
200 {0x2c00, 0xfe00, 8, 0, 10, 1},
201 {0x0550, 0xfff0, 13, 0, 10, 2},
202 {0x2a00, 0xfe00, 8, 0, 11, 1},
203 {0x2800, 0xfe00, 8, 0, 12, 1},
204 {0x1c00, 0xff00, 9, 0, 13, 1},
205 {0x1b00, 0xff00, 9, 0, 14, 1},
206 {0x1080, 0xff80, 10, 0, 15, 1},
207 {0x1000, 0xff80, 10, 0, 16, 1},
208 {0x0f80, 0xff80, 10, 0, 17, 1},
209 {0x0f00, 0xff80, 10, 0, 18, 1}, // 50
210 {0x0e80, 0xff80, 10, 0, 19, 1},
211 {0x0e00, 0xff80, 10, 0, 20, 1},
212 {0x0d80, 0xff80, 10, 0, 21, 1},
213 {0x0d00, 0xff80, 10, 0, 22, 1},
214 {0x0440, 0xffe0, 12, 0, 23, 1},
215 {0x0460, 0xffe0, 12, 0, 24, 1},
216 {0x0560, 0xfff0, 13, 0, 25, 1},
217 {0x0570, 0xfff0, 13, 0, 26, 1},
218 {0x7000, 0xf000, 5, 1, 0, 1},
219 {0x0c80, 0xff80, 10, 1, 0, 2}, // 60
220 {0x00a0, 0xffe0, 12, 1, 0, 3},
221 {0x3c00, 0xfc00, 7, 1, 1, 1},
222 {0x0080, 0xffe0, 12, 1, 1, 2},
223 {0x3800, 0xfc00, 7, 1, 2, 1},
224 {0x3400, 0xfc00, 7, 1, 3, 1},
225 {0x3000, 0xfc00, 7, 1, 4, 1},
226 {0x2600, 0xfe00, 8, 1, 5, 1},
227 {0x2400, 0xfe00, 8, 1, 6, 1},
228 {0x2200, 0xfe00, 8, 1, 7, 1},
229 {0x2000, 0xfe00, 8, 1, 8, 1}, // 70
230 {0x1a00, 0xff00, 9, 1, 9, 1},
231 {0x1900, 0xff00, 9, 1, 10, 1},
232 {0x1800, 0xff00, 9, 1, 11, 1},
233 {0x1700, 0xff00, 9, 1, 12, 1},
234 {0x1600, 0xff00, 9, 1, 13, 1},
235 {0x1500, 0xff00, 9, 1, 14, 1},
236 {0x1400, 0xff00, 9, 1, 15, 1},
237 {0x1300, 0xff00, 9, 1, 16, 1},
238 {0x0c00, 0xff80, 10, 1, 17, 1},
239 {0x0b80, 0xff80, 10, 1, 18, 1}, // 80
240 {0x0b00, 0xff80, 10, 1, 19, 1},
241 {0x0a80, 0xff80, 10, 1, 20, 1},
242 {0x0a00, 0xff80, 10, 1, 21, 1},
243 {0x0980, 0xff80, 10, 1, 22, 1},
244 {0x0900, 0xff80, 10, 1, 23, 1},
245 {0x0880, 0xff80, 10, 1, 24, 1},
246 {0x01c0, 0xffc0, 11, 1, 25, 1},
247 {0x0180, 0xffc0, 11, 1, 26, 1},
248 {0x0140, 0xffc0, 11, 1, 27, 1},
249 {0x0100, 0xffc0, 11, 1, 28, 1}, // 90
250 {0x0480, 0xffe0, 12, 1, 29, 1},
251 {0x04a0, 0xffe0, 12, 1, 30, 1},
252 {0x04c0, 0xffe0, 12, 1, 31, 1},
253 {0x04e0, 0xffe0, 12, 1, 32, 1},
254 {0x0580, 0xfff0, 13, 1, 33, 1},
255 {0x0590, 0xfff0, 13, 1, 34, 1},
256 {0x05a0, 0xfff0, 13, 1, 35, 1},
257 {0x05b0, 0xfff0, 13, 1, 36, 1},
258 {0x05c0, 0xfff0, 13, 1, 37, 1},
259 {0x05d0, 0xfff0, 13, 1, 38, 1}, // 100
260 {0x05e0, 0xfff0, 13, 1, 39, 1},
261 {0x05f0, 0xfff0, 13, 1, 40, 1},
262 {0x0600, 0xfe00, 7, 0, 0xffff, 0xffff}
266 * Motion vector code table (Code, mask, nbits, vector (halfpixel, two's complement), diff (halfpixel, two's complement))
270 static guint16 mvd[64][5] = {
271 {0x0028, 0xfff8, 13, 0x0060, 0x0020},
272 {0x0038, 0xfff8, 13, 0x0061, 0x0021},
273 {0x0050, 0xfff0, 12, 0x0062, 0x0022},
274 {0x0070, 0xfff0, 12, 0x0063, 0x0023},
275 {0x0090, 0xfff0, 12, 0x0064, 0x0024},
276 {0x00b0, 0xfff0, 12, 0x0065, 0x0025},
277 {0x00d0, 0xfff0, 12, 0x0066, 0x0026},
278 {0x00f0, 0xfff0, 12, 0x0067, 0x0027},
279 {0x0120, 0xffe0, 11, 0x0068, 0x0028},
280 {0x0160, 0xffe0, 11, 0x0069, 0x0029},
281 {0x01a0, 0xffe0, 11, 0x006a, 0x002a},
282 {0x01e0, 0xffe0, 11, 0x006b, 0x002b},
283 {0x0220, 0xffe0, 11, 0x006c, 0x002c},
284 {0x0260, 0xffe0, 11, 0x006d, 0x002d},
285 {0x02a0, 0xffe0, 11, 0x006e, 0x002e},
286 {0x02e0, 0xffe0, 11, 0x006f, 0x002f},
287 {0x0320, 0xffe0, 11, 0x0070, 0x0030},
288 {0x0360, 0xffe0, 11, 0x0071, 0x0031},
289 {0x03a0, 0xffe0, 11, 0x0072, 0x0032},
290 {0x03e0, 0xffe0, 11, 0x0073, 0x0033},
291 {0x0420, 0xffe0, 11, 0x0074, 0x0034},
292 {0x0460, 0xffe0, 11, 0x0075, 0x0035},
293 {0x04c0, 0xffc0, 10, 0x0076, 0x0036},
294 {0x0540, 0xffc0, 10, 0x0077, 0x0037},
295 {0x05c0, 0xffc0, 10, 0x0078, 0x0038},
296 {0x0700, 0xff00, 8, 0x0079, 0x0039},
297 {0x0900, 0xff00, 8, 0x007a, 0x003a},
298 {0x0b00, 0xff00, 8, 0x007b, 0x003b},
299 {0x0e00, 0xfe00, 7, 0x007c, 0x003c},
300 {0x1800, 0xf800, 5, 0x007d, 0x003d},
301 {0x3000, 0xf000, 4, 0x007e, 0x003e},
302 {0x6000, 0xe000, 3, 0x007f, 0x003f},
303 {0x8000, 0x8000, 1, 0x0000, 0x0000},
304 {0x4000, 0xe000, 3, 0x0001, 0x0041},
305 {0x2000, 0xf000, 4, 0x0002, 0x0042},
306 {0x1000, 0xf800, 5, 0x0003, 0x0043},
307 {0x0c00, 0xfe00, 7, 0x0004, 0x0044},
308 {0x0a00, 0xff00, 8, 0x0005, 0x0045},
309 {0x0800, 0xff00, 8, 0x0006, 0x0046},
310 {0x0600, 0xff00, 8, 0x0007, 0x0047},
311 {0x0580, 0xffc0, 10, 0x0008, 0x0048},
312 {0x0500, 0xffc0, 10, 0x0009, 0x0049},
313 {0x0480, 0xffc0, 10, 0x000a, 0x004a},
314 {0x0440, 0xffe0, 11, 0x000b, 0x004b},
315 {0x0400, 0xffe0, 11, 0x000c, 0x004c},
316 {0x03c0, 0xffe0, 11, 0x000d, 0x004d},
317 {0x0380, 0xffe0, 11, 0x000e, 0x004e},
318 {0x0340, 0xffe0, 11, 0x000f, 0x004f},
319 {0x0300, 0xffe0, 11, 0x0010, 0x0050},
320 {0x02c0, 0xffe0, 11, 0x0011, 0x0051},
321 {0x0280, 0xffe0, 11, 0x0012, 0x0052},
322 {0x0240, 0xffe0, 11, 0x0013, 0x0053},
323 {0x0200, 0xffe0, 11, 0x0014, 0x0054},
324 {0x01c0, 0xffe0, 11, 0x0015, 0x0055},
325 {0x0180, 0xffe0, 11, 0x0016, 0x0056},
326 {0x0140, 0xffe0, 11, 0x0017, 0x0057},
327 {0x0100, 0xffe0, 11, 0x0018, 0x0058},
328 {0x00e0, 0xfff0, 12, 0x0019, 0x0059},
329 {0x00c0, 0xfff0, 12, 0x001a, 0x005a},
330 {0x00a0, 0xfff0, 12, 0x001b, 0x005b},
331 {0x0080, 0xfff0, 12, 0x001c, 0x005c},
332 {0x0060, 0xfff0, 12, 0x001d, 0x005d},
333 {0x0040, 0xfff0, 12, 0x001e, 0x005e},
334 {0x0030, 0xfff8, 13, 0x001f, 0x005f}
337 GST_DEBUG_CATEGORY_STATIC (rtph263pay_debug);
338 #define GST_CAT_DEFAULT (rtph263pay_debug)
340 #define GST_RTP_HEADER_LEN 12
348 /* elementfactory information */
349 static const GstElementDetails gst_rtp_h263pay_details =
350 GST_ELEMENT_DETAILS ("RTP H263 packet payloader",
351 "Codec/Payloader/Network",
352 "Payload-encodes H263 video in RTP packets (RFC 2190)",
353 "Neil Stratford <neils@vipadia.com>"
354 "Dejan Sakelsak <dejan.sakelsak@marand.si>");
356 static GstStaticPadTemplate gst_rtp_h263_pay_sink_template =
357 GST_STATIC_PAD_TEMPLATE ("sink",
360 GST_STATIC_CAPS ("video/x-h263, "
361 "variant = (string) \"itu\", " "h263version = (string) \"h263\"")
364 static GstStaticPadTemplate gst_rtp_h263_pay_src_template =
365 GST_STATIC_PAD_TEMPLATE ("src",
368 GST_STATIC_CAPS ("application/x-rtp, "
369 "media = (string) \"video\", "
370 "payload = (int) " GST_RTP_PAYLOAD_H263_STRING ", "
371 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263\"; "
372 "application/x-rtp, "
373 "media = (string) \"video\", "
374 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
375 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263\"")
378 static void gst_rtp_h263_pay_class_init (GstRtpH263PayClass * klass);
379 static void gst_rtp_h263_pay_base_init (GstRtpH263PayClass * klass);
380 static void gst_rtp_h263_pay_init (GstRtpH263Pay * rtph263pay);
381 static void gst_rtp_h263_pay_finalize (GObject * object);
383 static gboolean gst_rtp_h263_pay_setcaps (GstBaseRTPPayload * payload,
385 static void gst_rtp_h263_pay_set_property (GObject * object, guint prop_id,
386 const GValue * value, GParamSpec * pspec);
387 static void gst_rtp_h263_pay_get_property (GObject * object, guint prop_id,
388 GValue * value, GParamSpec * pspec);
389 static GstFlowReturn gst_rtp_h263_pay_handle_buffer (GstBaseRTPPayload *
390 payload, GstBuffer * buffer);
392 static GstRtpH263PayBoundry *gst_rtp_h263_pay_boundry_new (guint8 * start,
393 guint8 * end, guint8 sbit, guint8 ebit);
394 static GstRtpH263PayGob *gst_rtp_h263_pay_gob_new (GstRtpH263PayBoundry *
395 boundry, guint gobn);
396 static GstRtpH263PayMB *gst_rtp_h263_pay_mb_new (GstRtpH263PayBoundry * boundry,
398 static GstRtpH263PayPackage *gst_rtp_h263_pay_package_new_empty ();
399 static GstRtpH263PayPackage *gst_rtp_h263_pay_package_new (guint8 * start,
400 guint8 * end, guint length, guint8 sbit, guint8 ebit, GstBuffer * outbuf,
403 static void gst_rtp_h263_pay_boundry_destroy (GstRtpH263PayBoundry * boundry);
404 static void gst_rtp_h263_pay_mb_destroy (GstRtpH263PayMB * mb);
405 static void gst_rtp_h263_pay_gob_destroy (GstRtpH263PayGob * gob, guint ind);
406 static void gst_rtp_h263_pay_context_destroy (GstRtpH263PayContext * context,
408 static void gst_rtp_h263_pay_package_destroy (GstRtpH263PayPackage * pack);
410 static GstBaseRTPPayloadClass *parent_class = NULL;
413 gst_rtp_h263_pay_get_type (void)
415 static GType rtph263pay_type = 0;
417 if (!rtph263pay_type) {
418 static const GTypeInfo rtph263pay_info = {
419 sizeof (GstRtpH263PayClass),
420 (GBaseInitFunc) gst_rtp_h263_pay_base_init,
422 (GClassInitFunc) gst_rtp_h263_pay_class_init,
425 sizeof (GstRtpH263Pay),
427 (GInstanceInitFunc) gst_rtp_h263_pay_init,
431 g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpH263Pay",
432 &rtph263pay_info, 0);
434 return rtph263pay_type;
438 gst_rtp_h263_pay_base_init (GstRtpH263PayClass * klass)
440 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
442 gst_element_class_add_pad_template (element_class,
443 gst_static_pad_template_get (&gst_rtp_h263_pay_src_template));
444 gst_element_class_add_pad_template (element_class,
445 gst_static_pad_template_get (&gst_rtp_h263_pay_sink_template));
447 gst_element_class_set_details (element_class, &gst_rtp_h263pay_details);
451 gst_rtp_h263_pay_class_init (GstRtpH263PayClass * klass)
453 GObjectClass *gobject_class;
454 GstBaseRTPPayloadClass *gstbasertppayload_class;
456 gobject_class = (GObjectClass *) klass;
457 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
459 parent_class = g_type_class_peek_parent (klass);
461 gobject_class->finalize = gst_rtp_h263_pay_finalize;
463 gstbasertppayload_class->set_caps = gst_rtp_h263_pay_setcaps;
464 gstbasertppayload_class->handle_buffer = gst_rtp_h263_pay_handle_buffer;
465 gobject_class->set_property = gst_rtp_h263_pay_set_property;
466 gobject_class->get_property = gst_rtp_h263_pay_get_property;
468 g_object_class_install_property (G_OBJECT_CLASS (klass),
469 PROP_MODE_A_ONLY, g_param_spec_boolean ("modea-only",
470 "Fragment packets in mode A Only",
471 "Disable packetization modes B and C", DEFAULT_MODE_A,
474 GST_DEBUG_CATEGORY_INIT (rtph263pay_debug, "rtph263pay", 0,
475 "H263 RTP Payloader");
479 gst_rtp_h263_pay_init (GstRtpH263Pay * rtph263pay)
481 rtph263pay->adapter = gst_adapter_new ();
483 rtph263pay->prop_payload_mode = DEFAULT_MODE_A;
487 gst_rtp_h263_pay_finalize (GObject * object)
489 GstRtpH263Pay *rtph263pay;
491 rtph263pay = GST_RTP_H263_PAY (object);
493 g_object_unref (rtph263pay->adapter);
494 rtph263pay->adapter = NULL;
496 G_OBJECT_CLASS (parent_class)->finalize (object);
500 gst_rtp_h263_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
504 payload->pt = GST_RTP_PAYLOAD_H263;
505 gst_basertppayload_set_options (payload, "video", TRUE, "H263", 90000);
506 res = gst_basertppayload_set_outcaps (payload, NULL);
512 gst_rtp_h263_pay_context_destroy (GstRtpH263PayContext * context, guint ind)
520 for (i = 0; i < format_props[ind][0]; i++) {
521 if (context->gobs[i]) {
522 gst_rtp_h263_pay_gob_destroy (context->gobs[i], ind);
526 g_free (context->gobs);
533 gst_rtp_h263_pay_set_property (GObject * object, guint prop_id,
534 const GValue * value, GParamSpec * pspec)
536 GstRtpH263Pay *rtph263pay;
538 rtph263pay = GST_RTP_H263_PAY (object);
541 case PROP_MODE_A_ONLY:
542 rtph263pay->prop_payload_mode = g_value_get_boolean (value);
545 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
551 gst_rtp_h263_pay_get_property (GObject * object, guint prop_id,
552 GValue * value, GParamSpec * pspec)
554 GstRtpH263Pay *rtph263pay;
556 rtph263pay = GST_RTP_H263_PAY (object);
559 case PROP_MODE_A_ONLY:
560 g_value_set_boolean (value, rtph263pay->prop_payload_mode);
563 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
568 static GstRtpH263PayPackage *
569 gst_rtp_h263_pay_package_new_empty ()
571 return (GstRtpH263PayPackage *) g_malloc0 (sizeof (GstRtpH263PayPackage));
574 static GstRtpH263PayPackage *
575 gst_rtp_h263_pay_package_new (guint8 * start, guint8 * end, guint length,
576 guint8 sbit, guint8 ebit, GstBuffer * outbuf, gboolean marker)
579 GstRtpH263PayPackage *package;
581 package = gst_rtp_h263_pay_package_new_empty ();
583 package->payload_start = start;
584 package->payload_end = end;
585 package->payload_len = length;
586 package->sbit = sbit;
587 package->ebit = ebit;
588 package->outbuf = outbuf;
589 package->marker = marker;
595 gst_rtp_h263_pay_package_destroy (GstRtpH263PayPackage * pack)
601 static GstRtpH263PayBoundry *
602 gst_rtp_h263_pay_boundry_new (guint8 * start, guint8 * end, guint8 sbit,
605 GstRtpH263PayBoundry *boundry;
607 boundry = (GstRtpH263PayBoundry *) g_malloc0 (sizeof (GstRtpH263PayBoundry));
609 boundry->start = start;
611 boundry->sbit = sbit;
612 boundry->ebit = ebit;
618 gst_rtp_h263_pay_boundry_destroy (GstRtpH263PayBoundry * boundry)
625 gst_rtp_h263_pay_splat_header_A (guint8 * header,
626 GstRtpH263PayPackage * package, GstRtpH263PayPic * piclayer)
629 GstRtpH263PayAHeader *a_header;
631 a_header = (GstRtpH263PayAHeader *) header;
635 a_header->sbit = package->sbit;
636 a_header->ebit = package->ebit;
637 a_header->src = GST_H263_PICTURELAYER_PLSRC (piclayer);
638 a_header->i = GST_H263_PICTURELAYER_PLTYPE (piclayer);
639 a_header->u = GST_H263_PICTURELAYER_PLUMV (piclayer);
640 a_header->s = GST_H263_PICTURELAYER_PLSAC (piclayer);
641 a_header->a = GST_H263_PICTURELAYER_PLAP (piclayer);
651 gst_rtp_h263_pay_splat_header_B (guint8 * header,
652 GstRtpH263PayPackage * package, GstRtpH263PayPic * piclayer)
655 GstRtpH263PayBHeader *b_header;
657 b_header = (GstRtpH263PayBHeader *) header;
661 b_header->sbit = package->sbit;
662 b_header->ebit = package->ebit;
663 b_header->src = GST_H263_PICTURELAYER_PLSRC (piclayer);
664 b_header->quant = package->quant;
665 b_header->gobn = package->gobn;
666 b_header->mba1 = package->mba >> 6;
667 b_header->mba2 = package->mba & 0x003f;
669 b_header->i = GST_H263_PICTURELAYER_PLTYPE (piclayer);
670 b_header->u = GST_H263_PICTURELAYER_PLUMV (piclayer);
671 b_header->s = GST_H263_PICTURELAYER_PLSAC (piclayer);
672 b_header->a = GST_H263_PICTURELAYER_PLAP (piclayer);
682 if (package->nmvd > 0) {
684 b_header->hmv11 = (package->mvd[0] & 0x7f) >> 3;
685 b_header->hmv12 = (package->mvd[0] & 0x07);
687 b_header->vmv11 = (package->mvd[1] & 0x07f) >> 2;
688 b_header->vmv12 = (package->mvd[1] & 0x03);
690 if (package->nmvd == 8) {
692 b_header->hmv21 = (package->mvd[4] & 0x7f) >> 1;
693 b_header->hmv22 = (package->mvd[4] & 0x01);
695 b_header->vmv21 = (package->mvd[5] & 0x7f);
701 static GstRtpH263PayBoundry *
702 gst_rtp_h263_pay_gobfinder (GstRtpH263Pay * rtph263pay,
703 GstRtpH263PayBoundry * boundry)
708 GstRtpH263PayBoundry *b;
710 current = boundry->end + 1;
711 range = (rtph263pay->data - current) + rtph263pay->available_data;
714 GST_DEBUG ("Searching for next GOB, data:%p, len:%u, payload_len:%p,"
715 " current:%p, range:%u", rtph263pay->data, rtph263pay->available_data,
716 boundry->end + 1, current, range);
718 /* If we are past the end, stop */
719 if (current >= rtph263pay->data + rtph263pay->available_data)
722 for (i = 3; i < range - 3; i++) {
723 if ((current[i] == 0x0) &&
724 (current[i + 1] == 0x0) && (current[i + 2] >> 7 == 0x1)) {
725 GST_LOG ("GOB end found at: %p start: %p len: %d", current + i - 1,
726 boundry->end + 1, current + i - boundry->end + 2);
727 b = gst_rtp_h263_pay_boundry_new (boundry->end + 1, current + i - 1, 0,
729 gst_rtp_h263_pay_boundry_destroy (boundry);
735 GST_DEBUG ("Couldn't find any new GBSC in this frame, range:%u", range);
737 b = gst_rtp_h263_pay_boundry_new (boundry->end + 1,
738 (guint8 *) (rtph263pay->data + rtph263pay->available_data - 1), 0, 0);
739 gst_rtp_h263_pay_boundry_destroy (boundry);
744 static GstRtpH263PayGob *
745 gst_rtp_h263_pay_gob_new (GstRtpH263PayBoundry * boundry, guint gobn)
747 GstRtpH263PayGob *gob;
749 gob = (GstRtpH263PayGob *) g_malloc0 (sizeof (GstRtpH263PayGob));
751 gob->start = boundry->start;
752 gob->end = boundry->end;
753 gob->length = boundry->end - boundry->start + 1;
754 gob->ebit = boundry->ebit;
755 gob->sbit = boundry->sbit;
758 gob->macroblocks = NULL;
759 gob->nmacroblocs = 0;
765 gst_rtp_h263_pay_gob_destroy (GstRtpH263PayGob * gob, guint ind)
771 if (gob->macroblocks) {
775 for (i = 0; i < gob->nmacroblocs; i++) {
776 gst_rtp_h263_pay_mb_destroy (gob->macroblocks[i]);
779 g_free (gob->macroblocks);
786 * decode MCBPC for I frames and return index in table or -1 if not found
789 gst_rtp_h263_pay_decode_mcbpc_I (guint32 value)
797 GST_DEBUG ("value:0x%08x, code:0x%04x", value, code);
799 for (i = 0; i < MCBPC_I_LEN; i++) {
800 if ((code & mcbpc_I[i][1]) == mcbpc_I[i][0]) {
805 GST_WARNING ("Couldn't find code, returning -1");
811 * decode MCBPC for P frames and return index in table or -1 if not found
814 gst_rtp_h263_pay_decode_mcbpc_P (guint32 value)
822 GST_DEBUG ("value:0x%08x, code:0x%04x", value, code);
824 for (i = 0; i < MCBPC_P_LEN; i++) {
825 if ((code & mcbpc_P[i][1]) == mcbpc_P[i][0]) {
830 GST_WARNING ("Couldn't find code, returning -1");
836 * decode CBPY and return index in table or -1 if not found
839 gst_rtp_h263_pay_decode_cbpy (guint32 value, guint8 cbpy_table[16][7])
847 GST_DEBUG ("value:0x%08x, code:0x%04x", value, code);
849 for (i = 0; i < CBPY_LEN; i++) {
850 if ((code & cbpy_table[i][1]) == cbpy_table[i][0]) {
855 GST_WARNING ("Couldn't find code, returning -1");
861 * decode MVD and return index in table or -1 if not found
864 gst_rtp_h263_pay_decode_mvd (guint32 value)
872 GST_DEBUG ("value:0x%08x, code:0x%04x", value, code);
874 for (i = 0; i < MVD_LEN; i++) {
875 if ((code & mvd[i][1]) == mvd[i][0]) {
880 GST_WARNING ("Couldn't find code, returning -1");
886 * decode TCOEF and return index in table or -1 if not found
889 gst_rtp_h263_pay_decode_tcoef (guint32 value)
897 GST_DEBUG ("value:0x%08x, code:0x%04x", value, code);
899 for (i = 0; i < TCOEF_LEN; i++) {
900 if ((code & tcoef[i][1]) == tcoef[i][0]) {
901 GST_LOG ("tcoef is %d", i);
906 GST_WARNING ("Couldn't find code, returning -1");
912 * the 32-bit register is like a window that we move right for "move_bits" to get the next v "data" h263 field
913 * "rest_bits" tells how many bits in the "data" byte address are still not used
917 gst_rtp_h263_pay_move_window_right (GstRtpH263PayContext * context, guint n,
918 guint rest_bits, guint8 ** orig_data, guint8 ** data_end)
921 GST_DEBUG ("Moving window: 0x%08x from: %p for %d bits, rest_bits: %d",
922 context->window, context->win_end, n, rest_bits);
927 while (n != 0 || context->win_end == ((*data_end) + 1)) {
929 if (rest_bits == 0) {
931 context->window = (context->window << 8) | *context->win_end;
935 (context->window << n) | (*context->win_end >> (8 - n));
944 (context->window << rest_bits) | (*context->
945 win_end & (((guint) pow (2.0, (double) rest_bits)) - 1));
950 (context->window << n) | ((*context->win_end & (((guint) pow (2.0,
951 (double) rest_bits)) - 1)) >> (rest_bits - n));
962 *orig_data = context->win_end - 4;
965 ("Window moved to %p with value: 0x%08x and orig_data: %p rest_bits: %d",
966 context->win_end, context->window, *orig_data, rest_bits);
971 * Find the start of the next MB (end of the current MB)
972 * returns the number of excess bits and stores the end of the MB in end
973 * data must be placed on first MB byte
975 static GstRtpH263PayMB *
976 gst_rtp_h263_pay_B_mbfinder (GstRtpH263PayContext * context,
977 GstRtpH263PayGob * gob, GstRtpH263PayMB * macroblock, guint mba)
981 guint cbpy_type_index;
982 guint tcoef_type_index;
983 GstRtpH263PayMB *mac;
986 gst_rtp_h263_pay_mb_new (gst_rtp_h263_pay_boundry_new (macroblock->end,
987 macroblock->end, 8 - macroblock->ebit, macroblock->ebit), mba);
989 if (mac->sbit == 8) {
995 GST_LOG ("current_pos:%p, end:%p, rest_bits:%d, window:%x",
996 mac->start, mac->end, macroblock->ebit, context->window);
998 GST_LOG ("Current pos after COD: %p", mac->end);
1000 if (context->piclayer->ptype_pictype == 0) {
1001 //We have an I frame
1006 //Step 2 decode MCBPC I
1007 mb_type_index = gst_rtp_h263_pay_decode_mcbpc_I (context->window);
1009 GST_DEBUG ("MCBPC index: %d", mb_type_index);
1010 if (mb_type_index == -1) {
1011 GST_ERROR ("MB index shouldn't be -1 in window: %08x", context->window);
1016 gst_rtp_h263_pay_move_window_right (context, mcbpc_I[mb_type_index][2],
1017 mac->ebit, &mac->end, &gob->end);
1019 mac->mb_type = mcbpc_I[mb_type_index][5];
1021 if (mb_type_index == 8) {
1022 GST_LOG ("Stuffing skipping rest of MB header");
1025 //Step 3 decode CBPY I
1026 cbpy_type_index = gst_rtp_h263_pay_decode_cbpy (context->window, cbpy_I);
1028 GST_DEBUG ("CBPY index: %d", cbpy_type_index);
1029 if (cbpy_type_index == -1) {
1030 GST_ERROR ("CBPY index shouldn't be -1 in window: %08x", context->window);
1035 gst_rtp_h263_pay_move_window_right (context, cbpy_I[cbpy_type_index][2],
1036 mac->ebit, &mac->end, &gob->end);
1038 //Step 4 decode rest of MB
1039 //MB type 1 and 4 have DQUANT - we store it for packaging purposes
1040 if (mcbpc_I[mb_type_index][5] == 4) {
1041 GST_DEBUG ("Shifting DQUANT");
1043 mac->quant = (context->window >> 30);
1046 gst_rtp_h263_pay_move_window_right (context, 2, mac->ebit, &mac->end,
1049 //Step 5 go trough the blocks - decode DC and TCOEF
1053 for (i = 0; i < N_BLOCKS; i++) {
1055 GST_DEBUG ("Decoding INTRADC and TCOEF, i:%d", i);
1057 gst_rtp_h263_pay_move_window_right (context, 8, mac->ebit, &mac->end,
1061 ind = mcbpc_I[mb_type_index][i - 1];
1063 ind = cbpy_I[cbpy_type_index][i + 3];
1068 tcoef_type_index = gst_rtp_h263_pay_decode_tcoef (context->window);
1070 GST_DEBUG ("TCOEF index: %d", tcoef_type_index);
1071 if (tcoef_type_index == -1) {
1072 GST_ERROR ("TCOEF index shouldn't be -1 in window: %08x",
1077 gst_rtp_h263_pay_move_window_right (context,
1078 tcoef[tcoef_type_index][2], mac->ebit, &mac->end, &gob->end);
1080 last = tcoef[tcoef_type_index][3];
1081 if (tcoef_type_index == 102) {
1082 if ((context->window & 0x80000000) == 0x80000000)
1088 gst_rtp_h263_pay_move_window_right (context, 15,
1089 mac->ebit, &mac->end, &gob->end);
1103 GST_DEBUG ("Checking for COD");
1104 if ((context->window & 0x80000000) == 0x80000000) {
1105 //The MB is not coded
1107 gst_rtp_h263_pay_move_window_right (context, 1, mac->ebit, &mac->end,
1109 GST_DEBUG ("COOOOOOOOOOOD = 1");
1115 gst_rtp_h263_pay_move_window_right (context, 1, mac->ebit, &mac->end,
1119 //Step 2 decode MCBPC P
1120 mb_type_index = gst_rtp_h263_pay_decode_mcbpc_P (context->window);
1122 GST_DEBUG ("MCBPC index: %d", mb_type_index);
1123 if (mb_type_index == -1) {
1124 GST_ERROR ("MB index shouldn't be -1 in window: %08x", context->window);
1128 gst_rtp_h263_pay_move_window_right (context, mcbpc_P[mb_type_index][2],
1129 mac->ebit, &mac->end, &gob->end);
1131 mac->mb_type = mcbpc_P[mb_type_index][5];
1133 if (mb_type_index == 20) {
1134 GST_LOG ("Stuffing skipping rest of MB header");
1137 //Step 3 decode CBPY P
1138 cbpy_type_index = gst_rtp_h263_pay_decode_cbpy (context->window, cbpy_P);
1140 GST_DEBUG ("CBPY index: %d", cbpy_type_index);
1141 if (cbpy_type_index == -1) {
1142 GST_ERROR ("CBPY index shouldn't be -1 in window: %08x", context->window);
1146 gst_rtp_h263_pay_move_window_right (context, cbpy_P[cbpy_type_index][2],
1147 mac->ebit, &mac->end, &gob->end);
1149 //MB type 1 and 4 have DQUANT - we add it to MB object and jump over
1150 if (mcbpc_P[mb_type_index][5] == 4 || mcbpc_P[mb_type_index][5] == 1) {
1151 GST_DEBUG ("Shifting DQUANT");
1153 mac->quant = context->window >> 30;
1156 gst_rtp_h263_pay_move_window_right (context, 2, mac->ebit, &mac->end,
1159 //MB types < 3 have MVD1-4
1160 if (mcbpc_P[mb_type_index][5] < 3) {
1166 if (mcbpc_P[mb_type_index][5] == 2)
1169 for (j = 0; j < nmvd; j++) {
1172 mvd_type = gst_rtp_h263_pay_decode_mvd (context->window);
1174 if (mvd_type == -1) {
1175 GST_ERROR ("MVD1-4 index shouldn't be -1 in window: %08x",
1179 //set the MB mvd values
1180 mac->mvd[j] = mvd[mvd_type][3];
1183 gst_rtp_h263_pay_move_window_right (context, mvd[mvd_type][2],
1184 mac->ebit, &mac->end, &gob->end);
1189 //Step 5 go trough the blocks - decode DC and TCOEF
1193 for (i = 0; i < N_BLOCKS; i++) {
1195 //if MB type 3 or 4 then INTRADC coef are present in blocks
1196 if (mcbpc_P[mb_type_index][5] > 2) {
1197 GST_DEBUG ("INTRADC coef: %d", i);
1199 gst_rtp_h263_pay_move_window_right (context, 8, mac->ebit,
1200 &mac->end, &gob->end);
1202 GST_DEBUG ("INTRADC coef is not present");
1205 //check if the block has TCOEF
1207 ind = mcbpc_P[mb_type_index][i - 1];
1209 if (mcbpc_P[mb_type_index][5] > 2) {
1210 ind = cbpy_I[cbpy_type_index][i + 3];
1212 ind = cbpy_P[cbpy_type_index][i + 3];
1218 tcoef_type_index = gst_rtp_h263_pay_decode_tcoef (context->window);
1220 GST_DEBUG ("TCOEF index: %d", tcoef_type_index);
1221 if (tcoef_type_index == -1) {
1222 GST_ERROR ("TCOEF index shouldn't be -1 in window: %08x",
1228 gst_rtp_h263_pay_move_window_right (context,
1229 tcoef[tcoef_type_index][2], mac->ebit, &mac->end, &gob->end);
1231 last = tcoef[tcoef_type_index][3];
1232 if (tcoef_type_index == 102) {
1233 if ((context->window & 0x80000000) == 0x80000000)
1239 gst_rtp_h263_pay_move_window_right (context, 15,
1240 mac->ebit, &mac->end, &gob->end);
1248 mac->length = mac->end - mac->start + 1;
1253 static GstRtpH263PayMB *
1254 gst_rtp_h263_pay_mb_new (GstRtpH263PayBoundry * boundry, guint mba)
1256 GstRtpH263PayMB *mb;
1259 mb = (GstRtpH263PayMB *) g_malloc0 (sizeof (GstRtpH263PayMB));
1261 mb->start = boundry->start;
1262 mb->end = boundry->end;
1263 mb->length = boundry->end - boundry->start + 1;
1264 mb->sbit = boundry->sbit;
1265 mb->ebit = boundry->ebit;
1268 for (i = 0; i < 5; i++)
1275 gst_rtp_h263_pay_mb_destroy (GstRtpH263PayMB * mb)
1283 static GstFlowReturn
1284 gst_rtp_h263_pay_push (GstRtpH263Pay * rtph263pay,
1285 GstRtpH263PayContext * context, GstRtpH263PayPackage * package)
1289 * Splat the payload header values
1295 header = gst_rtp_buffer_get_payload (package->outbuf);
1296 payload = header + package->mode;
1298 switch (package->mode) {
1299 case GST_RTP_H263_PAYLOAD_HEADER_MODE_A:
1300 GST_LOG ("Pushing A packet");
1301 gst_rtp_h263_pay_splat_header_A (header, package, context->piclayer);
1303 case GST_RTP_H263_PAYLOAD_HEADER_MODE_B:
1304 GST_LOG ("Pushing B packet");
1305 gst_rtp_h263_pay_splat_header_B (header, package, context->piclayer);
1307 case GST_RTP_H263_PAYLOAD_HEADER_MODE_C:
1308 //gst_rtp_h263_pay_splat_header_C(header, package, context->piclayer);
1311 return GST_FLOW_ERROR;
1316 * Copy the payload data in the buffer
1318 GST_DEBUG ("Copiing memory");
1319 memcpy (payload, (guint8 *) package->payload_start, package->payload_len);
1322 * timestamp the buffer
1324 GST_BUFFER_TIMESTAMP (package->outbuf) = rtph263pay->first_ts;
1326 gst_rtp_buffer_set_marker (package->outbuf, package->marker);
1327 if (package->marker)
1328 GST_DEBUG ("Marker set!");
1331 gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtph263pay),
1333 GST_DEBUG ("Package pushed, returning");
1335 gst_rtp_h263_pay_package_destroy (package);
1340 static GstFlowReturn
1341 gst_rtp_h263_pay_A_fragment_push (GstRtpH263Pay * rtph263pay,
1342 GstRtpH263PayContext * context, guint first, guint last)
1345 GstRtpH263PayPackage *pack;
1347 pack = gst_rtp_h263_pay_package_new_empty ();
1349 pack->payload_start = context->gobs[first]->start;
1350 pack->sbit = context->gobs[first]->sbit;
1351 pack->ebit = context->gobs[last]->ebit;
1353 (context->gobs[last]->end - context->gobs[first]->start) + 1;
1354 pack->marker = FALSE;
1356 if (last == format_props[context->piclayer->ptype_srcformat][0] - 1) {
1357 pack->marker = TRUE;
1360 pack->gobn = context->gobs[first]->gobn;
1361 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_A;
1363 gst_rtp_buffer_new_allocate (pack->payload_len + pack->mode, 0, 0);
1365 GST_DEBUG ("Sending len:%d data to push function", pack->payload_len);
1367 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1370 static GstFlowReturn
1371 gst_rtp_h263_pay_B_fragment_push (GstRtpH263Pay * rtph263pay,
1372 GstRtpH263PayContext * context, GstRtpH263PayGob * gob, guint first,
1373 guint last, GstRtpH263PayBoundry * boundry)
1376 GstRtpH263PayPackage *pack;
1379 pack = gst_rtp_h263_pay_package_new_empty ();
1381 pack->payload_start = gob->macroblocks[first]->start;
1382 pack->sbit = gob->macroblocks[first]->sbit;
1384 pack->payload_start = boundry->start;
1385 pack->sbit = boundry->sbit;
1386 pack->quant = gob->quant;
1388 pack->quant = gob->macroblocks[first]->quant;
1390 pack->payload_end = gob->macroblocks[last]->end;
1392 pack->ebit = gob->macroblocks[last]->ebit;
1393 pack->mba = gob->macroblocks[first]->mba;
1394 pack->gobn = gob->gobn;
1395 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_B;
1398 if (gob->macroblocks[first]->mb_type < 3) {
1399 if (gob->macroblocks[first]->mb_type == 2)
1401 else if (gob->macroblocks[first]->mb_type < 2)
1404 for (mv = 0; mv < pack->nmvd; mv++)
1405 pack->mvd[mv] = gob->macroblocks[first]->mvd[mv];
1408 pack->marker = FALSE;
1409 if (last == gob->nmacroblocs - 1) {
1413 if ((format_props[context->piclayer->ptype_srcformat][0] - 1 == gob->gobn)
1414 && (last == gob->nmacroblocs - 1)) {
1415 pack->marker = TRUE;
1418 pack->payload_len = pack->payload_end - pack->payload_start + 1;
1420 gst_rtp_buffer_new_allocate (pack->payload_len + pack->mode, 0, 0);
1422 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1427 gst_rtp_h263_pay_mode_B_fragment (GstRtpH263Pay * rtph263pay,
1428 GstRtpH263PayContext * context, GstRtpH263PayGob * gob)
1432 /*---------- MODE B MODE FRAGMENTATION ----------*/
1434 GstRtpH263PayMB *mac;
1435 guint max_payload_size;
1436 GstRtpH263PayBoundry *boundry;
1445 context->mtu - GST_RTP_H263_PAYLOAD_HEADER_MODE_B - GST_RTP_HEADER_LEN;
1447 boundry = gst_rtp_h263_pay_boundry_new (gob->start, gob->start, gob->sbit, 0);
1450 (GstRtpH263PayMB **) g_malloc0 (sizeof (GstRtpH263PayMB *) *
1451 format_props[context->piclayer->ptype_srcformat][1]);
1453 GST_LOG ("GOB isn't PB frame, applying mode B");
1455 //initializing window
1456 context->win_end = boundry->end;
1457 if (gst_rtp_h263_pay_move_window_right (context, 32, boundry->ebit,
1458 &boundry->end, &gob->end) != 0) {
1460 ("The rest of the bits should be 0, exiting, because something bad happend");
1461 gst_adapter_flush (rtph263pay->adapter, rtph263pay->available_data);
1464 //The first GOB of a frame "has no" actual header - PICTURE header is his header
1465 if (gob->gobn == 0) {
1467 GST_LOG ("Initial GOB");
1471 gst_rtp_h263_pay_move_window_right (context, shift, boundry->ebit,
1472 &boundry->end, &gob->end);
1474 //We need PQUANT for mode B packages - so we store it
1475 gob->quant = context->window >> 27;
1477 //if PCM == 1, then PSBI is present - header has 51 bits
1478 //shift for PQUANT (5) and PCM (1) = 6 bits
1480 if (context->cpm == 1)
1483 gst_rtp_h263_pay_move_window_right (context, shift, boundry->ebit,
1484 &boundry->end, &gob->end);
1486 GST_DEBUG ("window: 0x%08x", context->window);
1488 //Shifting the PEI and PSPARE fields
1489 while ((context->window & 0x80000000) == 0x80000000) {
1491 gst_rtp_h263_pay_move_window_right (context, 9,
1492 boundry->ebit, &boundry->end, &gob->end);
1493 GST_LOG ("window: 0x%x", context->window);
1496 //shift the last PEI field
1498 gst_rtp_h263_pay_move_window_right (context, 1, boundry->ebit,
1499 &boundry->end, &gob->end);
1502 //skipping GOBs 24 header bits + 5 GQUANT
1505 GST_LOG ("INTER GOB");
1507 //if CPM == 1, there are 2 more bits in the header - GSBI header is 31 bits long
1508 if (context->cpm == 1)
1511 GST_LOG ("window: 0x%x", context->window);
1513 gst_rtp_h263_pay_move_window_right (context, shift,
1514 boundry->ebit, &boundry->end, &gob->end);
1516 //We need GQUANT for mode B packages - so we store it
1517 gob->quant = context->window >> 27;
1521 gst_rtp_h263_pay_move_window_right (context, shift,
1522 boundry->ebit, &boundry->end, &gob->end);
1524 GST_LOG ("window: 0x%x", context->window);
1527 GST_DEBUG ("GQUANT IS: %08x", gob->quant);
1529 // We are on MB layer
1533 m = mac = a = gst_rtp_h263_pay_mb_new (boundry, 0);
1534 for (mb = 0; mb < format_props[context->piclayer->ptype_srcformat][1]; mb++) {
1536 GST_DEBUG ("================ START MB %d =================", mb);
1538 //Find next macroblock boundaries
1539 if (!(mac = gst_rtp_h263_pay_B_mbfinder (context, gob, mac, mb))) {
1541 GST_DEBUG ("Error decoding MB - sbit: %d", 8 - m->ebit);
1542 GST_ERROR ("Error decoding in GOB");
1546 //If mb_type == stuffing, don't increment the mb address
1547 if (mac->mb_type == 10) {
1554 gob->macroblocks[mb] = mac;
1556 if (mac->end >= gob->end) {
1557 GST_LOG ("No more MBs in this GOB");
1561 gob->end = mac->end;
1565 GST_DEBUG ("Found MB: mba: %d start: %p end: %p len: %d sbit: %d ebit: %d",
1566 mac->mba, mac->start, mac->end, mac->length, mac->sbit, mac->ebit);
1567 GST_DEBUG ("================ END MB %d =================", mb);
1572 payload_len = boundry->end - boundry->start + 1;
1573 GST_DEBUG ("------------------------- NEW PACKAGE ----------------------");
1574 while (mb < gob->nmacroblocs) {
1575 if (payload_len + gob->macroblocks[mb]->length < max_payload_size) {
1577 //FIXME: payload_len is not the real length -> ignoring sbit/ebit
1578 payload_len += gob->macroblocks[mb]->length;
1582 //FIXME: we should include the last few bits of the GOB in the package - do we do that now?
1583 //GST_DEBUG ("Pushing GOBS %d to %d because payload size is %d", first,
1584 // first == mb - 1, payload_len);
1586 if (gst_rtp_h263_pay_B_fragment_push (rtph263pay, context, gob, first,
1588 GST_ERROR ("Oooops, there was an error sending");
1595 ("------------------------- END PACKAGE ----------------------");
1597 ("------------------------- NEW PACKAGE ----------------------");
1602 GST_DEBUG ("Remainder first: %d, MB: %d", first, mb);
1603 if (payload_len != 0) {
1604 if (gst_rtp_h263_pay_B_fragment_push (rtph263pay, context, gob, first,
1606 GST_ERROR ("Oooops, there was an error sending!");
1611 /*---------- END OF MODE B FRAGMENTATION ----------*/
1619 static GstFlowReturn
1620 gst_rtp_h263_send_entire_frame (GstRtpH263Pay * rtph263pay,
1621 GstRtpH263PayContext * context)
1623 GstRtpH263PayPackage *pack;
1625 gst_rtp_h263_pay_package_new (rtph263pay->data,
1626 rtph263pay->data + rtph263pay->available_data,
1627 rtph263pay->available_data, 0, 0, NULL, TRUE);
1628 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_A;
1630 GST_DEBUG ("Available data: %d", rtph263pay->available_data);
1633 gst_rtp_buffer_new_allocate (pack->payload_len +
1634 GST_RTP_H263_PAYLOAD_HEADER_MODE_A, 0, 0);
1636 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1639 static GstFlowReturn
1640 gst_rtp_h263_pay_flush (GstRtpH263Pay * rtph263pay)
1644 * FIXME: GSTUF bits are ignored right now,
1645 * - not using EBIT/SBIT payload header fields in mode A fragmentation - ffmpeg doesn't need them, but others?
1649 GstRtpH263PayContext *context;
1653 context = (GstRtpH263PayContext *) g_malloc0 (sizeof (GstRtpH263PayContext));
1655 rtph263pay->payload.mtu - (MTU_SECURITY_OFFSET + GST_RTP_HEADER_LEN +
1656 GST_RTP_H263_PAYLOAD_HEADER_MODE_C);
1658 GST_DEBUG ("MTU: %d", context->mtu);
1659 rtph263pay->available_data = gst_adapter_available (rtph263pay->adapter);
1660 if (rtph263pay->available_data == 0) {
1661 ret = GST_FLOW_RESEND;
1665 /* Get a pointer to all the data for the frame */
1667 (guint8 *) gst_adapter_peek (rtph263pay->adapter,
1668 rtph263pay->available_data);
1670 /* Picture header */
1671 context->piclayer = (GstRtpH263PayPic *) rtph263pay->data;
1673 if (context->piclayer->ptype_pictype == 0)
1674 GST_DEBUG ("We got an I-frame");
1676 GST_DEBUG ("We got a P-frame");
1678 context->cpm = rtph263pay->data[6] >> 7;
1680 GST_DEBUG ("CPM: %d", context->cpm);
1682 GST_DEBUG ("Payload length is: %d", rtph263pay->available_data);
1685 * - MODE A - If normal, I and P frames, -> mode A
1686 * - GOB layer fragmentation
1687 * - MODE B - If normal, I and P frames, but GOBs > mtu
1688 * - MB layer fragmentation
1689 * - MODE C - For P frames with PB option, but GOBs > mtu
1690 * - MB layer fragmentation
1692 if (rtph263pay->available_data + GST_RTP_H263_PAYLOAD_HEADER_MODE_A +
1693 GST_RTP_HEADER_LEN < context->mtu) {
1694 ret = gst_rtp_h263_send_entire_frame (rtph263pay, context);
1697 /*---------- FRAGMENTING THE FRAME BECAUSE TOO LARGE TO FIT IN MTU ----------*/
1698 GstRtpH263PayBoundry *bound;
1702 GST_DEBUG ("Frame too large for MTU");
1704 * Let's go trough all the data and fragment it untill end is reached
1707 bound = gst_rtp_h263_pay_boundry_new (NULL, rtph263pay->data - 1, 0, 0);
1709 (GstRtpH263PayGob **) g_malloc0 (format_props[context->piclayer->
1710 ptype_srcformat][0] * sizeof (GstRtpH263PayGob *));
1713 for (i = 0; i < format_props[context->piclayer->ptype_srcformat][0]; i++) {
1714 GST_DEBUG ("Searching for gob %d", i);
1715 bound = gst_rtp_h263_pay_gobfinder (rtph263pay, bound);
1718 ("No GOB's were found in data stream! Please enable RTP mode in encoder. Forcing mode A for now.");
1719 ret = gst_rtp_h263_send_entire_frame (rtph263pay, context);
1723 context->gobs[i] = gst_rtp_h263_pay_gob_new (bound, i);
1724 //FIXME - encoders may generate an EOS gob that has to be processed
1726 ("Gob values are: gobn: %d, start: %p len: %d ebit: %d sbit: %d", i,
1727 context->gobs[i]->start, context->gobs[i]->length,
1728 context->gobs[i]->ebit, context->gobs[i]->sbit);
1731 gst_rtp_h263_pay_boundry_destroy (bound);
1733 // Make packages smaller than MTU
1735 // - if ( GOB > MTU) -> B mode || C mode
1741 while (i < format_props[context->piclayer->ptype_srcformat][0]) {
1743 if (context->gobs[i]->length >= context->mtu) {
1744 if (payload_len == 0) {
1746 GST_DEBUG ("GOB len > MTU");
1747 if (rtph263pay->prop_payload_mode) {
1748 payload_len = context->gobs[i]->length;
1751 if (!context->piclayer->ptype_pbmode) {
1752 GST_DEBUG ("MODE B on GOB %d needed", i);
1753 if (!gst_rtp_h263_pay_mode_B_fragment (rtph263pay, context,
1754 context->gobs[i])) {
1755 GST_ERROR ("There was an error fragmenting in mode B");
1756 return GST_FLOW_ERROR;
1760 GST_ERROR ("MODE C on GOB %d needed, but not supported yet", i);
1761 /*if(!gst_rtp_h263_pay_mode_C_fragment(rtph263pay, context, context->gobs[i])) {
1763 GST_ERROR("There was an error fragmenting in mode C");
1774 goto payload_a_push;
1778 if (payload_len + context->gobs[i]->length < context->mtu) {
1779 GST_DEBUG ("GOB %d fills mtu", i);
1780 payload_len += context->gobs[i]->length;
1782 if (i == format_props[context->piclayer->ptype_srcformat][0]) {
1783 GST_DEBUG ("LAST GOB %d", i);
1784 goto payload_a_push;
1789 GST_DEBUG ("Pushing GOBS %d to %d because payload size is %d", first,
1790 first == i ? i : i - 1, payload_len);
1791 gst_rtp_h263_pay_A_fragment_push (rtph263pay, context, first,
1792 first == i ? i : i - 1);
1799 GST_DEBUG ("Pushing GOBS %d to %d because payload size is %d", first, i,
1801 gst_rtp_h263_pay_A_fragment_push (rtph263pay, context, first, i);
1808 }/*---------- END OF FRAGMENTATION ----------*/
1810 /* Flush the input buffer data */
1813 gst_rtp_h263_pay_context_destroy (context,
1814 context->piclayer->ptype_srcformat);
1815 gst_adapter_flush (rtph263pay->adapter, rtph263pay->available_data);
1820 static GstFlowReturn
1821 gst_rtp_h263_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
1824 GstRtpH263Pay *rtph263pay;
1828 GST_DEBUG ("-------------------- NEW FRAME ---------------");
1829 rtph263pay = GST_RTP_H263_PAY (payload);
1831 size = GST_BUFFER_SIZE (buffer);
1832 rtph263pay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
1834 /* we always encode and flush a full picture */
1835 gst_adapter_push (rtph263pay->adapter, buffer);
1836 ret = gst_rtp_h263_pay_flush (rtph263pay);
1837 GST_DEBUG ("-------------------- END FRAME ---------------");
1843 gst_rtp_h263_pay_plugin_init (GstPlugin * plugin)
1845 return gst_element_register (plugin, "rtph263pay",
1846 GST_RANK_NONE, GST_TYPE_RTP_H263_PAY);