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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
29 #include <gst/rtp/gstrtpbuffer.h>
30 #include <gst/video/video.h>
32 #include "gstrtpelements.h"
33 #include "gstrtph263pay.h"
34 #include "gstrtputils.h"
38 GST_H263_FRAME_TYPE_I = 0,
39 GST_H263_FRAME_TYPE_P = 1,
40 GST_H263_FRAME_TYPE_PB = 2
41 } GstRtpH263PayFrameType;
45 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_RES1 = 0,
46 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_SQCIF = 1,
47 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_QCIF = 2,
48 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_CIF = 3,
49 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_4CIF = 4,
50 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_16CIF = 5,
51 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_RES2 = 6,
52 GST_H263_PAYLOAD_PICTURE_FORMAT_PLUS = 7
53 } GstRtpH263PayPictureFormat;
55 static const guint format_props[8][2] = { {254, 254},
66 * I-frame MCBPC table: code, mask, nbits, cb, cr, mb type -> 10 = undefined (because we have guint)
70 static const guint32 mcbpc_I[9][6] = {
71 {0x8000, 0x8000, 1, 0, 0, 3},
72 {0x2000, 0xe000, 3, 0, 1, 3},
73 {0x4000, 0xe000, 3, 1, 0, 3},
74 {0x6000, 0xe000, 3, 1, 1, 3},
75 {0x1000, 0xf000, 4, 0, 0, 4},
76 {0x0400, 0xfc00, 6, 0, 1, 4},
77 {0x0800, 0xfc00, 6, 1, 0, 4},
78 {0x0c00, 0xfc00, 6, 1, 1, 4},
79 {0x0080, 0xff80, 9, 10, 10, 10}
83 * P-frame MCBPC table: code, mask, nbits, cb, cr, mb type -> 10 = undefined (because we have guint)
85 #define MCBPC_P_LEN 21
87 static const guint16 mcbpc_P[21][6] = {
88 {0x8000, 0x8000, 1, 0, 0, 0},
89 {0x3000, 0xf000, 4, 0, 1, 0},
90 {0x2000, 0xf000, 4, 1, 0, 0},
91 {0x1400, 0xfc00, 6, 1, 1, 0},
92 {0x6000, 0xe000, 3, 0, 0, 1},
93 {0x0e00, 0xfe00, 7, 0, 1, 1},
94 {0x0c00, 0xfe00, 7, 1, 0, 1},
95 {0x0280, 0xff80, 9, 1, 1, 1},
96 {0x4000, 0xe000, 3, 0, 0, 2},
97 {0x0a00, 0xfe00, 7, 0, 1, 2},
98 {0x0800, 0xfe00, 7, 1, 0, 2},
99 {0x0500, 0xff00, 8, 1, 1, 2},
100 {0x1800, 0xf800, 5, 0, 0, 3},
101 {0x0400, 0xff00, 8, 0, 1, 3},
102 {0x0300, 0xff00, 8, 1, 0, 3},
103 {0x0600, 0xfe00, 7, 1, 1, 3},
104 {0x1000, 0xfc00, 6, 0, 0, 4},
105 {0x0200, 0xff80, 9, 0, 1, 4},
106 {0x0180, 0xff80, 9, 1, 0, 4},
107 {0x0100, 0xff80, 9, 1, 1, 4},
108 {0x0080, 0xff80, 9, 10, 10, 10}
112 * I-frame CBPY (code, mask, nbits, Y0, Y1, Y2, Y3)
116 static const guint8 cbpy_I[16][7] = {
117 {0x30, 0xf0, 4, 0, 0, 0, 0},
118 {0x28, 0xf8, 5, 0, 0, 0, 1},
119 {0x20, 0xf8, 5, 0, 0, 1, 0},
120 {0x90, 0xf0, 4, 0, 0, 1, 1},
121 {0x18, 0xf8, 5, 0, 1, 0, 0},
122 {0x70, 0xf0, 4, 0, 1, 0, 1},
123 {0x08, 0xfc, 6, 0, 1, 1, 0},
124 {0xb0, 0xf0, 4, 0, 1, 1, 1},
125 {0x10, 0xf8, 5, 1, 0, 0, 0},
126 {0x0c, 0xfc, 6, 1, 0, 0, 1},
127 {0x50, 0xf0, 4, 1, 0, 1, 0},
128 {0xa0, 0xf0, 4, 1, 0, 1, 1},
129 {0x40, 0xf0, 4, 1, 1, 0, 0},
130 {0x80, 0xf0, 4, 1, 1, 0, 1},
131 {0x60, 0xf0, 4, 1, 1, 1, 0},
132 {0xc0, 0xc0, 2, 1, 1, 1, 1}
136 * P-frame CBPY (code, mask, nbits, Y0, Y1, Y2, Y3)
138 static const guint8 cbpy_P[16][7] = {
139 {0x30, 0xf0, 4, 1, 1, 1, 1},
140 {0x28, 0xf8, 5, 1, 1, 1, 0},
141 {0x20, 0xf8, 5, 1, 1, 0, 1},
142 {0x90, 0xf0, 4, 1, 1, 0, 0},
143 {0x18, 0xf8, 5, 1, 0, 1, 1},
144 {0x70, 0xf0, 4, 1, 0, 1, 0},
145 {0x08, 0xfc, 6, 1, 0, 0, 1},
146 {0xb0, 0xf0, 4, 1, 0, 0, 0},
147 {0x10, 0xf8, 5, 0, 1, 1, 1},
148 {0x0c, 0xfc, 6, 0, 1, 1, 0},
149 {0x50, 0xf0, 4, 0, 1, 0, 1},
150 {0xa0, 0xf0, 4, 0, 1, 0, 0},
151 {0x40, 0xf0, 4, 0, 0, 1, 1},
152 {0x80, 0xf0, 4, 0, 0, 1, 0},
153 {0x60, 0xf0, 4, 0, 0, 0, 1},
154 {0xc0, 0xc0, 2, 0, 0, 0, 0}
158 * Block TCOEF table (code, mask, nbits, LAST, RUN, LEVEL)
160 #define TCOEF_LEN 103
162 static const guint16 tcoef[103][6] = {
163 {0x8000, 0xc000, 3, 0, 0, 1},
164 {0xf000, 0xf000, 5, 0, 0, 2},
165 {0x5400, 0xfc00, 7, 0, 0, 3},
166 {0x2e00, 0xfe00, 8, 0, 0, 4},
167 {0x1f00, 0xff00, 9, 0, 0, 5},
168 {0x1280, 0xff80, 10, 0, 0, 6},
169 {0x1200, 0xff80, 10, 0, 0, 7},
170 {0x0840, 0xffc0, 11, 0, 0, 8},
171 {0x0800, 0xffc0, 11, 0, 0, 9},
172 {0x00e0, 0xffe0, 12, 0, 0, 10}, //10
173 {0x00c0, 0xffe0, 12, 0, 0, 11},
174 {0x0400, 0xffe0, 12, 0, 0, 12},
175 {0xc000, 0xe000, 4, 0, 1, 1},
176 {0x5000, 0xfc00, 7, 0, 1, 2},
177 {0x1e00, 0xff00, 9, 0, 1, 3},
178 {0x03c0, 0xffc0, 11, 0, 1, 4},
179 {0x0420, 0xffe0, 12, 0, 1, 5},
180 {0x0500, 0xfff0, 13, 0, 1, 6},
181 {0xe000, 0xf000, 5, 0, 2, 1},
182 {0x1d00, 0xff00, 9, 0, 2, 2}, //20
183 {0x0380, 0xffc0, 11, 0, 2, 3},
184 {0x0510, 0xfff0, 13, 0, 2, 4},
185 {0x6800, 0xf800, 6, 0, 3, 1},
186 {0x1180, 0xff80, 10, 0, 3, 2},
187 {0x0340, 0xffc0, 11, 0, 3, 3},
188 {0x6000, 0xf800, 6, 0, 4, 1},
189 {0x1100, 0xff80, 10, 0, 4, 2},
190 {0x0520, 0xfff0, 13, 0, 4, 3},
191 {0x5800, 0xf800, 6, 0, 5, 1},
192 {0x0300, 0xffc0, 11, 0, 5, 2}, // 30
193 {0x0530, 0xfff0, 13, 0, 5, 3},
194 {0x4c00, 0xfc00, 7, 0, 6, 1},
195 {0x02c0, 0xffc0, 11, 0, 6, 2},
196 {0x0540, 0xfff0, 13, 0, 6, 3},
197 {0x4800, 0xfc00, 7, 0, 7, 1},
198 {0x0280, 0xffc0, 11, 0, 7, 2},
199 {0x4400, 0xfc00, 7, 0, 8, 1},
200 {0x0240, 0xffc0, 11, 0, 8, 2},
201 {0x4000, 0xfc00, 7, 0, 9, 1},
202 {0x0200, 0xffc0, 11, 0, 9, 2}, // 40
203 {0x2c00, 0xfe00, 8, 0, 10, 1},
204 {0x0550, 0xfff0, 13, 0, 10, 2},
205 {0x2a00, 0xfe00, 8, 0, 11, 1},
206 {0x2800, 0xfe00, 8, 0, 12, 1},
207 {0x1c00, 0xff00, 9, 0, 13, 1},
208 {0x1b00, 0xff00, 9, 0, 14, 1},
209 {0x1080, 0xff80, 10, 0, 15, 1},
210 {0x1000, 0xff80, 10, 0, 16, 1},
211 {0x0f80, 0xff80, 10, 0, 17, 1},
212 {0x0f00, 0xff80, 10, 0, 18, 1}, // 50
213 {0x0e80, 0xff80, 10, 0, 19, 1},
214 {0x0e00, 0xff80, 10, 0, 20, 1},
215 {0x0d80, 0xff80, 10, 0, 21, 1},
216 {0x0d00, 0xff80, 10, 0, 22, 1},
217 {0x0440, 0xffe0, 12, 0, 23, 1},
218 {0x0460, 0xffe0, 12, 0, 24, 1},
219 {0x0560, 0xfff0, 13, 0, 25, 1},
220 {0x0570, 0xfff0, 13, 0, 26, 1},
221 {0x7000, 0xf000, 5, 1, 0, 1},
222 {0x0c80, 0xff80, 10, 1, 0, 2}, // 60
223 {0x00a0, 0xffe0, 12, 1, 0, 3},
224 {0x3c00, 0xfc00, 7, 1, 1, 1},
225 {0x0080, 0xffe0, 12, 1, 1, 2},
226 {0x3800, 0xfc00, 7, 1, 2, 1},
227 {0x3400, 0xfc00, 7, 1, 3, 1},
228 {0x3000, 0xfc00, 7, 1, 4, 1},
229 {0x2600, 0xfe00, 8, 1, 5, 1},
230 {0x2400, 0xfe00, 8, 1, 6, 1},
231 {0x2200, 0xfe00, 8, 1, 7, 1},
232 {0x2000, 0xfe00, 8, 1, 8, 1}, // 70
233 {0x1a00, 0xff00, 9, 1, 9, 1},
234 {0x1900, 0xff00, 9, 1, 10, 1},
235 {0x1800, 0xff00, 9, 1, 11, 1},
236 {0x1700, 0xff00, 9, 1, 12, 1},
237 {0x1600, 0xff00, 9, 1, 13, 1},
238 {0x1500, 0xff00, 9, 1, 14, 1},
239 {0x1400, 0xff00, 9, 1, 15, 1},
240 {0x1300, 0xff00, 9, 1, 16, 1},
241 {0x0c00, 0xff80, 10, 1, 17, 1},
242 {0x0b80, 0xff80, 10, 1, 18, 1}, // 80
243 {0x0b00, 0xff80, 10, 1, 19, 1},
244 {0x0a80, 0xff80, 10, 1, 20, 1},
245 {0x0a00, 0xff80, 10, 1, 21, 1},
246 {0x0980, 0xff80, 10, 1, 22, 1},
247 {0x0900, 0xff80, 10, 1, 23, 1},
248 {0x0880, 0xff80, 10, 1, 24, 1},
249 {0x01c0, 0xffc0, 11, 1, 25, 1},
250 {0x0180, 0xffc0, 11, 1, 26, 1},
251 {0x0140, 0xffc0, 11, 1, 27, 1},
252 {0x0100, 0xffc0, 11, 1, 28, 1}, // 90
253 {0x0480, 0xffe0, 12, 1, 29, 1},
254 {0x04a0, 0xffe0, 12, 1, 30, 1},
255 {0x04c0, 0xffe0, 12, 1, 31, 1},
256 {0x04e0, 0xffe0, 12, 1, 32, 1},
257 {0x0580, 0xfff0, 13, 1, 33, 1},
258 {0x0590, 0xfff0, 13, 1, 34, 1},
259 {0x05a0, 0xfff0, 13, 1, 35, 1},
260 {0x05b0, 0xfff0, 13, 1, 36, 1},
261 {0x05c0, 0xfff0, 13, 1, 37, 1},
262 {0x05d0, 0xfff0, 13, 1, 38, 1}, // 100
263 {0x05e0, 0xfff0, 13, 1, 39, 1},
264 {0x05f0, 0xfff0, 13, 1, 40, 1},
265 {0x0600, 0xfe00, 7, 0, 0xffff, 0xffff}
269 * Motion vector code table (Code, mask, nbits, vector (halfpixel, two's complement), diff (halfpixel, two's complement))
273 static const guint16 mvd[64][5] = {
274 {0x0028, 0xfff8, 13, 0x0060, 0x0020},
275 {0x0038, 0xfff8, 13, 0x0061, 0x0021},
276 {0x0050, 0xfff0, 12, 0x0062, 0x0022},
277 {0x0070, 0xfff0, 12, 0x0063, 0x0023},
278 {0x0090, 0xfff0, 12, 0x0064, 0x0024},
279 {0x00b0, 0xfff0, 12, 0x0065, 0x0025},
280 {0x00d0, 0xfff0, 12, 0x0066, 0x0026},
281 {0x00f0, 0xfff0, 12, 0x0067, 0x0027},
282 {0x0120, 0xffe0, 11, 0x0068, 0x0028},
283 {0x0160, 0xffe0, 11, 0x0069, 0x0029},
284 {0x01a0, 0xffe0, 11, 0x006a, 0x002a},
285 {0x01e0, 0xffe0, 11, 0x006b, 0x002b},
286 {0x0220, 0xffe0, 11, 0x006c, 0x002c},
287 {0x0260, 0xffe0, 11, 0x006d, 0x002d},
288 {0x02a0, 0xffe0, 11, 0x006e, 0x002e},
289 {0x02e0, 0xffe0, 11, 0x006f, 0x002f},
290 {0x0320, 0xffe0, 11, 0x0070, 0x0030},
291 {0x0360, 0xffe0, 11, 0x0071, 0x0031},
292 {0x03a0, 0xffe0, 11, 0x0072, 0x0032},
293 {0x03e0, 0xffe0, 11, 0x0073, 0x0033},
294 {0x0420, 0xffe0, 11, 0x0074, 0x0034},
295 {0x0460, 0xffe0, 11, 0x0075, 0x0035},
296 {0x04c0, 0xffc0, 10, 0x0076, 0x0036},
297 {0x0540, 0xffc0, 10, 0x0077, 0x0037},
298 {0x05c0, 0xffc0, 10, 0x0078, 0x0038},
299 {0x0700, 0xff00, 8, 0x0079, 0x0039},
300 {0x0900, 0xff00, 8, 0x007a, 0x003a},
301 {0x0b00, 0xff00, 8, 0x007b, 0x003b},
302 {0x0e00, 0xfe00, 7, 0x007c, 0x003c},
303 {0x1800, 0xf800, 5, 0x007d, 0x003d},
304 {0x3000, 0xf000, 4, 0x007e, 0x003e},
305 {0x6000, 0xe000, 3, 0x007f, 0x003f},
306 {0x8000, 0x8000, 1, 0x0000, 0x0000},
307 {0x4000, 0xe000, 3, 0x0001, 0x0041},
308 {0x2000, 0xf000, 4, 0x0002, 0x0042},
309 {0x1000, 0xf800, 5, 0x0003, 0x0043},
310 {0x0c00, 0xfe00, 7, 0x0004, 0x0044},
311 {0x0a00, 0xff00, 8, 0x0005, 0x0045},
312 {0x0800, 0xff00, 8, 0x0006, 0x0046},
313 {0x0600, 0xff00, 8, 0x0007, 0x0047},
314 {0x0580, 0xffc0, 10, 0x0008, 0x0048},
315 {0x0500, 0xffc0, 10, 0x0009, 0x0049},
316 {0x0480, 0xffc0, 10, 0x000a, 0x004a},
317 {0x0440, 0xffe0, 11, 0x000b, 0x004b},
318 {0x0400, 0xffe0, 11, 0x000c, 0x004c},
319 {0x03c0, 0xffe0, 11, 0x000d, 0x004d},
320 {0x0380, 0xffe0, 11, 0x000e, 0x004e},
321 {0x0340, 0xffe0, 11, 0x000f, 0x004f},
322 {0x0300, 0xffe0, 11, 0x0010, 0x0050},
323 {0x02c0, 0xffe0, 11, 0x0011, 0x0051},
324 {0x0280, 0xffe0, 11, 0x0012, 0x0052},
325 {0x0240, 0xffe0, 11, 0x0013, 0x0053},
326 {0x0200, 0xffe0, 11, 0x0014, 0x0054},
327 {0x01c0, 0xffe0, 11, 0x0015, 0x0055},
328 {0x0180, 0xffe0, 11, 0x0016, 0x0056},
329 {0x0140, 0xffe0, 11, 0x0017, 0x0057},
330 {0x0100, 0xffe0, 11, 0x0018, 0x0058},
331 {0x00e0, 0xfff0, 12, 0x0019, 0x0059},
332 {0x00c0, 0xfff0, 12, 0x001a, 0x005a},
333 {0x00a0, 0xfff0, 12, 0x001b, 0x005b},
334 {0x0080, 0xfff0, 12, 0x001c, 0x005c},
335 {0x0060, 0xfff0, 12, 0x001d, 0x005d},
336 {0x0040, 0xfff0, 12, 0x001e, 0x005e},
337 {0x0030, 0xfff8, 13, 0x001f, 0x005f}
340 GST_DEBUG_CATEGORY_STATIC (rtph263pay_debug);
341 #define GST_CAT_DEFAULT (rtph263pay_debug)
343 #define GST_RTP_HEADER_LEN 12
351 static GstStaticPadTemplate gst_rtp_h263_pay_sink_template =
352 GST_STATIC_PAD_TEMPLATE ("sink",
355 GST_STATIC_CAPS ("video/x-h263, "
356 "variant = (string) \"itu\", " "h263version = (string) \"h263\"")
359 static GstStaticPadTemplate gst_rtp_h263_pay_src_template =
360 GST_STATIC_PAD_TEMPLATE ("src",
363 GST_STATIC_CAPS ("application/x-rtp, "
364 "media = (string) \"video\", "
365 "payload = (int) " GST_RTP_PAYLOAD_H263_STRING ", "
366 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263\"; "
367 "application/x-rtp, "
368 "media = (string) \"video\", "
369 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
370 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263\"")
373 static void gst_rtp_h263_pay_finalize (GObject * object);
375 static gboolean gst_rtp_h263_pay_setcaps (GstRTPBasePayload * payload,
377 static void gst_rtp_h263_pay_set_property (GObject * object, guint prop_id,
378 const GValue * value, GParamSpec * pspec);
379 static void gst_rtp_h263_pay_get_property (GObject * object, guint prop_id,
380 GValue * value, GParamSpec * pspec);
381 static GstFlowReturn gst_rtp_h263_pay_handle_buffer (GstRTPBasePayload *
382 payload, GstBuffer * buffer);
384 static void gst_rtp_h263_pay_boundry_init (GstRtpH263PayBoundry * boundry,
385 guint8 * start, guint8 * end, guint8 sbit, guint8 ebit);
386 static GstRtpH263PayGob *gst_rtp_h263_pay_gob_new (GstRtpH263PayBoundry *
387 boundry, guint gobn);
388 static GstRtpH263PayMB *gst_rtp_h263_pay_mb_new (GstRtpH263PayBoundry * boundry,
390 static GstRtpH263PayPackage *gst_rtp_h263_pay_package_new_empty ();
391 static GstRtpH263PayPackage *gst_rtp_h263_pay_package_new (guint8 * start,
392 guint8 * end, guint length, guint8 sbit, guint8 ebit, GstBuffer * outbuf,
395 static void gst_rtp_h263_pay_mb_destroy (GstRtpH263PayMB * mb);
396 static void gst_rtp_h263_pay_gob_destroy (GstRtpH263PayGob * gob, guint ind);
397 static void gst_rtp_h263_pay_context_destroy (GstRtpH263PayContext * context,
399 static void gst_rtp_h263_pay_package_destroy (GstRtpH263PayPackage * pack);
401 #define gst_rtp_h263_pay_parent_class parent_class
402 G_DEFINE_TYPE (GstRtpH263Pay, gst_rtp_h263_pay, GST_TYPE_RTP_BASE_PAYLOAD);
403 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtph263pay, "rtph263pay",
404 GST_RANK_SECONDARY, GST_TYPE_RTP_H263_PAY, rtp_element_init (plugin));
407 gst_rtp_h263_pay_class_init (GstRtpH263PayClass * klass)
409 GObjectClass *gobject_class;
410 GstElementClass *gstelement_class;
411 GstRTPBasePayloadClass *gstrtpbasepayload_class;
413 gobject_class = (GObjectClass *) klass;
414 gstelement_class = (GstElementClass *) klass;
415 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
417 gobject_class->finalize = gst_rtp_h263_pay_finalize;
419 gstrtpbasepayload_class->set_caps = gst_rtp_h263_pay_setcaps;
420 gstrtpbasepayload_class->handle_buffer = gst_rtp_h263_pay_handle_buffer;
421 gobject_class->set_property = gst_rtp_h263_pay_set_property;
422 gobject_class->get_property = gst_rtp_h263_pay_get_property;
424 g_object_class_install_property (G_OBJECT_CLASS (klass),
425 PROP_MODE_A_ONLY, g_param_spec_boolean ("modea-only",
426 "Fragment packets in mode A Only",
427 "Disable packetization modes B and C", DEFAULT_MODE_A,
428 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 gst_element_class_add_static_pad_template (gstelement_class,
431 &gst_rtp_h263_pay_src_template);
432 gst_element_class_add_static_pad_template (gstelement_class,
433 &gst_rtp_h263_pay_sink_template);
435 gst_element_class_set_static_metadata (gstelement_class,
436 "RTP H263 packet payloader", "Codec/Payloader/Network/RTP",
437 "Payload-encodes H263 video in RTP packets (RFC 2190)",
438 "Neil Stratford <neils@vipadia.com>"
439 "Dejan Sakelsak <dejan.sakelsak@marand.si>");
441 GST_DEBUG_CATEGORY_INIT (rtph263pay_debug, "rtph263pay", 0,
442 "H263 RTP Payloader");
446 gst_rtp_h263_pay_init (GstRtpH263Pay * rtph263pay)
448 GST_RTP_BASE_PAYLOAD_PT (rtph263pay) = GST_RTP_PAYLOAD_H263;
449 rtph263pay->prop_payload_mode = DEFAULT_MODE_A;
453 gst_rtp_h263_pay_finalize (GObject * object)
455 GstRtpH263Pay *rtph263pay;
457 rtph263pay = GST_RTP_H263_PAY (object);
459 gst_buffer_replace (&rtph263pay->current_buffer, NULL);
461 G_OBJECT_CLASS (parent_class)->finalize (object);
465 gst_rtp_h263_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
467 GstStructure *s = gst_caps_get_structure (caps, 0);
469 gchar *framesize = NULL;
472 if (gst_structure_has_field (s, "width") &&
473 gst_structure_has_field (s, "height")) {
474 if (!gst_structure_get_int (s, "width", &width) || width <= 0) {
475 goto invalid_dimension;
478 if (!gst_structure_get_int (s, "height", &height) || height <= 0) {
479 goto invalid_dimension;
482 framesize = g_strdup_printf ("%d-%d", width, height);
485 gst_rtp_base_payload_set_options (payload, "video",
486 payload->pt != GST_RTP_PAYLOAD_H263, "H263", 90000);
488 if (framesize != NULL) {
489 res = gst_rtp_base_payload_set_outcaps (payload,
490 "a-framesize", G_TYPE_STRING, framesize, NULL);
492 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
501 GST_ERROR_OBJECT (payload, "Invalid width/height from caps");
507 gst_rtp_h263_pay_context_destroy (GstRtpH263PayContext * context, guint ind)
515 for (i = 0; i < format_props[ind][0]; i++) {
516 if (context->gobs[i]) {
517 gst_rtp_h263_pay_gob_destroy (context->gobs[i], ind);
521 g_free (context->gobs);
528 gst_rtp_h263_pay_set_property (GObject * object, guint prop_id,
529 const GValue * value, GParamSpec * pspec)
531 GstRtpH263Pay *rtph263pay;
533 rtph263pay = GST_RTP_H263_PAY (object);
536 case PROP_MODE_A_ONLY:
537 rtph263pay->prop_payload_mode = g_value_get_boolean (value);
540 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
546 gst_rtp_h263_pay_get_property (GObject * object, guint prop_id,
547 GValue * value, GParamSpec * pspec)
549 GstRtpH263Pay *rtph263pay;
551 rtph263pay = GST_RTP_H263_PAY (object);
554 case PROP_MODE_A_ONLY:
555 g_value_set_boolean (value, rtph263pay->prop_payload_mode);
558 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
563 static GstRtpH263PayPackage *
564 gst_rtp_h263_pay_package_new_empty (void)
566 return (GstRtpH263PayPackage *) g_malloc0 (sizeof (GstRtpH263PayPackage));
569 static GstRtpH263PayPackage *
570 gst_rtp_h263_pay_package_new (guint8 * start, guint8 * end, guint length,
571 guint8 sbit, guint8 ebit, GstBuffer * outbuf, gboolean marker)
574 GstRtpH263PayPackage *package;
576 package = gst_rtp_h263_pay_package_new_empty ();
578 package->payload_start = start;
579 package->payload_end = end;
580 package->payload_len = length;
581 package->sbit = sbit;
582 package->ebit = ebit;
583 package->outbuf = outbuf;
584 package->marker = marker;
590 gst_rtp_h263_pay_package_destroy (GstRtpH263PayPackage * pack)
597 gst_rtp_h263_pay_boundry_init (GstRtpH263PayBoundry * boundry,
598 guint8 * start, guint8 * end, guint8 sbit, guint8 ebit)
600 boundry->start = start;
602 boundry->sbit = sbit;
603 boundry->ebit = ebit;
607 gst_rtp_h263_pay_splat_header_A (guint8 * header,
608 GstRtpH263PayPackage * package, GstRtpH263PayPic * piclayer)
611 GstRtpH263PayAHeader *a_header;
613 a_header = (GstRtpH263PayAHeader *) header;
617 a_header->sbit = package->sbit;
618 a_header->ebit = package->ebit;
619 a_header->src = GST_H263_PICTURELAYER_PLSRC (piclayer);
620 a_header->i = GST_H263_PICTURELAYER_PLTYPE (piclayer);
621 a_header->u = GST_H263_PICTURELAYER_PLUMV (piclayer);
622 a_header->s = GST_H263_PICTURELAYER_PLSAC (piclayer);
623 a_header->a = GST_H263_PICTURELAYER_PLAP (piclayer);
633 gst_rtp_h263_pay_splat_header_B (guint8 * header,
634 GstRtpH263PayPackage * package, GstRtpH263PayPic * piclayer)
637 GstRtpH263PayBHeader *b_header;
639 b_header = (GstRtpH263PayBHeader *) header;
643 b_header->sbit = package->sbit;
644 b_header->ebit = package->ebit;
645 b_header->src = GST_H263_PICTURELAYER_PLSRC (piclayer);
646 b_header->quant = package->quant;
647 b_header->gobn = package->gobn;
648 b_header->mba1 = package->mba >> 6;
649 b_header->mba2 = package->mba & 0x003f;
651 b_header->i = GST_H263_PICTURELAYER_PLTYPE (piclayer);
652 b_header->u = GST_H263_PICTURELAYER_PLUMV (piclayer);
653 b_header->s = GST_H263_PICTURELAYER_PLSAC (piclayer);
654 b_header->a = GST_H263_PICTURELAYER_PLAP (piclayer);
664 if (package->nmvd > 0) {
666 b_header->hmv11 = (package->mvd[0] & 0x7f) >> 3;
667 b_header->hmv12 = (package->mvd[0] & 0x07);
669 b_header->vmv11 = (package->mvd[1] & 0x07f) >> 2;
670 b_header->vmv12 = (package->mvd[1] & 0x03);
672 if (package->nmvd == 8) {
674 b_header->hmv21 = (package->mvd[4] & 0x7f) >> 1;
675 b_header->hmv22 = (package->mvd[4] & 0x01);
677 b_header->vmv21 = (package->mvd[5] & 0x7f);
684 gst_rtp_h263_pay_gobfinder (GstRtpH263Pay * rtph263pay,
685 GstRtpH263PayBoundry * boundry)
691 current = boundry->end + 1;
692 range = (rtph263pay->data - current) + rtph263pay->available_data;
695 GST_DEBUG_OBJECT (rtph263pay,
696 "Searching for next GOB, data:%p, len:%u, payload_len:%p,"
697 " current:%p, range:%u", rtph263pay->data, rtph263pay->available_data,
698 boundry->end + 1, current, range);
700 /* If we are past the end, stop */
701 if (current >= rtph263pay->data + rtph263pay->available_data)
704 for (i = 3; i < range - 3; i++) {
705 if ((current[i] == 0x0) &&
706 (current[i + 1] == 0x0) && (current[i + 2] >> 7 == 0x1)) {
707 GST_LOG_OBJECT (rtph263pay, "GOB end found at: %p start: %p len: %u",
708 current + i - 1, boundry->end + 1,
709 (guint) (current + i - boundry->end + 2));
710 gst_rtp_h263_pay_boundry_init (boundry, boundry->end + 1, current + i - 1,
717 GST_DEBUG_OBJECT (rtph263pay,
718 "Couldn't find any new GBSC in this frame, range:%u", range);
720 gst_rtp_h263_pay_boundry_init (boundry, boundry->end + 1,
721 (guint8 *) (rtph263pay->data + rtph263pay->available_data - 1), 0, 0);
726 static GstRtpH263PayGob *
727 gst_rtp_h263_pay_gob_new (GstRtpH263PayBoundry * boundry, guint gobn)
729 GstRtpH263PayGob *gob;
731 gob = (GstRtpH263PayGob *) g_malloc0 (sizeof (GstRtpH263PayGob));
733 gob->start = boundry->start;
734 gob->end = boundry->end;
735 gob->length = boundry->end - boundry->start + 1;
736 gob->ebit = boundry->ebit;
737 gob->sbit = boundry->sbit;
740 gob->macroblocks = NULL;
741 gob->nmacroblocs = 0;
747 gst_rtp_h263_pay_gob_destroy (GstRtpH263PayGob * gob, guint ind)
753 if (gob->macroblocks) {
757 for (i = 0; i < gob->nmacroblocs; i++) {
758 gst_rtp_h263_pay_mb_destroy (gob->macroblocks[i]);
761 g_free (gob->macroblocks);
768 * decode MCBPC for I frames and return index in table or -1 if not found
771 gst_rtp_h263_pay_decode_mcbpc_I (GstRtpH263Pay * rtph263pay, guint32 value)
779 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
781 for (i = 0; i < MCBPC_I_LEN; i++) {
782 if ((code & mcbpc_I[i][1]) == mcbpc_I[i][0]) {
787 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
793 * decode MCBPC for P frames and return index in table or -1 if not found
796 gst_rtp_h263_pay_decode_mcbpc_P (GstRtpH263Pay * rtph263pay, guint32 value)
804 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
806 for (i = 0; i < MCBPC_P_LEN; i++) {
807 if ((code & mcbpc_P[i][1]) == mcbpc_P[i][0]) {
812 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
818 * decode CBPY and return index in table or -1 if not found
821 gst_rtp_h263_pay_decode_cbpy (GstRtpH263Pay * rtph263pay, guint32 value,
822 const guint8 cbpy_table[16][7])
830 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
832 for (i = 0; i < CBPY_LEN; i++) {
833 if ((code & cbpy_table[i][1]) == cbpy_table[i][0]) {
838 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
844 * decode MVD and return index in table or -1 if not found
847 gst_rtp_h263_pay_decode_mvd (GstRtpH263Pay * rtph263pay, guint32 value)
855 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
857 for (i = 0; i < MVD_LEN; i++) {
858 if ((code & mvd[i][1]) == mvd[i][0]) {
863 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
869 * decode TCOEF and return index in table or -1 if not found
872 gst_rtp_h263_pay_decode_tcoef (GstRtpH263Pay * rtph263pay, guint32 value)
880 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
882 for (i = 0; i < TCOEF_LEN; i++) {
883 if ((code & tcoef[i][1]) == tcoef[i][0]) {
884 GST_TRACE_OBJECT (rtph263pay, "tcoef is %d", i);
889 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
895 * the 32-bit register is like a window that we move right for "move_bits" to get the next v "data" h263 field
896 * "rest_bits" tells how many bits in the "data" byte address are still not used
900 gst_rtp_h263_pay_move_window_right (GstRtpH263Pay * rtph263pay,
901 GstRtpH263PayContext * context, guint n, guint rest_bits,
902 guint8 ** orig_data, guint8 ** data_end)
905 GST_TRACE_OBJECT (rtph263pay,
906 "Moving window: 0x%08x from: %p for %d bits, rest_bits: %d, data_end %p",
907 context->window, context->win_end, n, rest_bits, *data_end);
912 while (n != 0 || context->win_end == ((*data_end) + 1)) {
913 guint8 b = context->win_end <= *data_end ? *context->win_end : 0;
914 if (rest_bits == 0) {
916 context->window = (context->window << 8) | b;
919 context->window = (context->window << n) | (b >> (8 - n));
927 context->window = (context->window << rest_bits) |
928 (b & (((guint) pow (2.0, (double) rest_bits)) - 1));
932 context->window = (context->window << n) |
933 ((b & (((guint) pow (2.0, (double) rest_bits)) - 1)) >>
945 *orig_data = context->win_end - 4;
947 GST_TRACE_OBJECT (rtph263pay,
948 "Window moved to %p with value: 0x%08x and orig_data: %p rest_bits: %d",
949 context->win_end, context->window, *orig_data, rest_bits);
954 * Find the start of the next MB (end of the current MB)
955 * returns the number of excess bits and stores the end of the MB in end
956 * data must be placed on first MB byte
958 static GstRtpH263PayMB *
959 gst_rtp_h263_pay_B_mbfinder (GstRtpH263Pay * rtph263pay,
960 GstRtpH263PayContext * context, GstRtpH263PayGob * gob,
961 GstRtpH263PayMB * macroblock, guint mba)
964 guint cbpy_type_index;
965 guint tcoef_type_index;
966 GstRtpH263PayMB *mac;
967 GstRtpH263PayBoundry boundry;
969 gst_rtp_h263_pay_boundry_init (&boundry, macroblock->end,
970 macroblock->end, 8 - macroblock->ebit, macroblock->ebit);
971 mac = gst_rtp_h263_pay_mb_new (&boundry, mba);
973 if (mac->sbit == 8) {
979 GST_LOG_OBJECT (rtph263pay,
980 "current_pos:%p, end:%p, rest_bits:%d, window:0x%08x", mac->start,
981 mac->end, macroblock->ebit, context->window);
983 if (context->piclayer->ptype_pictype == 0) {
989 //Step 2 decode MCBPC I
991 gst_rtp_h263_pay_decode_mcbpc_I (rtph263pay, context->window);
993 GST_TRACE_OBJECT (rtph263pay, "MCBPC index: %d", mb_type_index);
994 if (mb_type_index == -1) {
995 GST_ERROR_OBJECT (rtph263pay, "MB index shouldn't be -1 in window: %08x",
1001 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1002 mcbpc_I[mb_type_index][2], mac->ebit, &mac->end, &gob->end);
1004 mac->mb_type = mcbpc_I[mb_type_index][5];
1006 if (mb_type_index == 8) {
1007 GST_TRACE_OBJECT (rtph263pay, "Stuffing skipping rest of MB header");
1010 //Step 3 decode CBPY I
1012 gst_rtp_h263_pay_decode_cbpy (rtph263pay, context->window, cbpy_I);
1014 GST_TRACE_OBJECT (rtph263pay, "CBPY index: %d", cbpy_type_index);
1015 if (cbpy_type_index == -1) {
1016 GST_ERROR_OBJECT (rtph263pay,
1017 "CBPY index shouldn't be -1 in window: %08x", context->window);
1022 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1023 cbpy_I[cbpy_type_index][2], mac->ebit, &mac->end, &gob->end);
1025 //Step 4 decode rest of MB
1026 //MB type 1 and 4 have DQUANT - we store it for packaging purposes
1027 if (mcbpc_I[mb_type_index][5] == 4) {
1028 GST_TRACE_OBJECT (rtph263pay, "Shifting DQUANT");
1030 mac->quant = (context->window >> 30);
1033 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 2, mac->ebit,
1034 &mac->end, &gob->end);
1036 //Step 5 go trough the blocks - decode DC and TCOEF
1038 for (i = 0; i < N_BLOCKS; i++) {
1040 GST_TRACE_OBJECT (rtph263pay, "Decoding INTRADC and TCOEF, i:%d", i);
1042 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 8, mac->ebit,
1043 &mac->end, &gob->end);
1046 ind = mcbpc_I[mb_type_index][i - 1];
1048 ind = cbpy_I[cbpy_type_index][i + 3];
1054 gst_rtp_h263_pay_decode_tcoef (rtph263pay, context->window);
1056 GST_TRACE_OBJECT (rtph263pay, "TCOEF index: %d", tcoef_type_index);
1057 if (tcoef_type_index == -1) {
1058 GST_ERROR_OBJECT (rtph263pay,
1059 "TCOEF index shouldn't be -1 in window: %08x", context->window);
1063 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1064 tcoef[tcoef_type_index][2], mac->ebit, &mac->end, &gob->end);
1066 last = tcoef[tcoef_type_index][3];
1067 if (tcoef_type_index == 102) {
1068 if ((context->window & 0x80000000) == 0x80000000)
1074 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 15,
1075 mac->ebit, &mac->end, &gob->end);
1089 GST_TRACE_OBJECT (rtph263pay, "Checking for COD");
1090 if ((context->window & 0x80000000) == 0x80000000) {
1091 //The MB is not coded
1093 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 1, mac->ebit,
1094 &mac->end, &gob->end);
1095 GST_TRACE_OBJECT (rtph263pay, "COOOOOOOOOOOD = 1");
1101 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 1, mac->ebit,
1102 &mac->end, &gob->end);
1105 //Step 2 decode MCBPC P
1107 gst_rtp_h263_pay_decode_mcbpc_P (rtph263pay, context->window);
1109 GST_TRACE_OBJECT (rtph263pay, "MCBPC index: %d", mb_type_index);
1110 if (mb_type_index == -1) {
1111 GST_ERROR_OBJECT (rtph263pay, "MB index shouldn't be -1 in window: %08x",
1116 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1117 mcbpc_P[mb_type_index][2], mac->ebit, &mac->end, &gob->end);
1119 mac->mb_type = mcbpc_P[mb_type_index][5];
1121 if (mb_type_index == 20) {
1122 GST_TRACE_OBJECT (rtph263pay, "Stuffing skipping rest of MB header");
1125 //Step 3 decode CBPY P
1127 gst_rtp_h263_pay_decode_cbpy (rtph263pay, context->window, cbpy_P);
1129 GST_TRACE_OBJECT (rtph263pay, "CBPY index: %d", cbpy_type_index);
1130 if (cbpy_type_index == -1) {
1131 GST_ERROR_OBJECT (rtph263pay,
1132 "CBPY index shouldn't be -1 in window: %08x", context->window);
1136 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1137 cbpy_P[cbpy_type_index][2], mac->ebit, &mac->end, &gob->end);
1139 //MB type 1 and 4 have DQUANT - we add it to MB object and jump over
1140 if (mcbpc_P[mb_type_index][5] == 4 || mcbpc_P[mb_type_index][5] == 1) {
1141 GST_TRACE_OBJECT (rtph263pay, "Shifting DQUANT");
1143 mac->quant = context->window >> 30;
1146 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 2, mac->ebit,
1147 &mac->end, &gob->end);
1149 //MB types < 3 have MVD1-4
1150 if (mcbpc_P[mb_type_index][5] < 3) {
1156 if (mcbpc_P[mb_type_index][5] == 2)
1159 for (j = 0; j < nmvd; j++) {
1162 mvd_type = gst_rtp_h263_pay_decode_mvd (rtph263pay, context->window);
1164 if (mvd_type == -1) {
1165 GST_ERROR_OBJECT (rtph263pay,
1166 "MVD1-4 index shouldn't be -1 in window: %08x", context->window);
1169 //set the MB mvd values
1170 mac->mvd[j] = mvd[mvd_type][3];
1173 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1174 mvd[mvd_type][2], mac->ebit, &mac->end, &gob->end);
1179 //Step 5 go trough the blocks - decode DC and TCOEF
1181 for (i = 0; i < N_BLOCKS; i++) {
1183 //if MB type 3 or 4 then INTRADC coef are present in blocks
1184 if (mcbpc_P[mb_type_index][5] > 2) {
1185 GST_TRACE_OBJECT (rtph263pay, "INTRADC coef: %d", i);
1187 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 8,
1188 mac->ebit, &mac->end, &gob->end);
1190 GST_TRACE_OBJECT (rtph263pay, "INTRADC coef is not present");
1193 //check if the block has TCOEF
1195 ind = mcbpc_P[mb_type_index][i - 1];
1197 if (mcbpc_P[mb_type_index][5] > 2) {
1198 ind = cbpy_I[cbpy_type_index][i + 3];
1200 ind = cbpy_P[cbpy_type_index][i + 3];
1207 gst_rtp_h263_pay_decode_tcoef (rtph263pay, context->window);
1209 GST_TRACE_OBJECT (rtph263pay, "TCOEF index: %d", tcoef_type_index);
1210 if (tcoef_type_index == -1) {
1211 GST_ERROR_OBJECT (rtph263pay,
1212 "TCOEF index shouldn't be -1 in window: %08x", context->window);
1217 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1218 tcoef[tcoef_type_index][2], mac->ebit, &mac->end, &gob->end);
1220 last = tcoef[tcoef_type_index][3];
1221 if (tcoef_type_index == 102) {
1222 if ((context->window & 0x80000000) == 0x80000000)
1228 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 15,
1229 mac->ebit, &mac->end, &gob->end);
1237 mac->length = mac->end - mac->start + 1;
1242 gst_rtp_h263_pay_mb_destroy (mac);
1246 static GstRtpH263PayMB *
1247 gst_rtp_h263_pay_mb_new (GstRtpH263PayBoundry * boundry, guint mba)
1249 GstRtpH263PayMB *mb;
1252 mb = (GstRtpH263PayMB *) g_malloc0 (sizeof (GstRtpH263PayMB));
1254 mb->start = boundry->start;
1255 mb->end = boundry->end;
1256 mb->length = boundry->end - boundry->start + 1;
1257 mb->sbit = boundry->sbit;
1258 mb->ebit = boundry->ebit;
1261 for (i = 0; i < 5; i++)
1268 gst_rtp_h263_pay_mb_destroy (GstRtpH263PayMB * mb)
1276 static GstFlowReturn
1277 gst_rtp_h263_pay_push (GstRtpH263Pay * rtph263pay,
1278 GstRtpH263PayContext * context, GstRtpH263PayPackage * package)
1282 * Splat the payload header values
1286 GstRTPBuffer rtp = { NULL };
1288 gst_rtp_buffer_map (package->outbuf, GST_MAP_WRITE, &rtp);
1290 header = gst_rtp_buffer_get_payload (&rtp);
1292 switch (package->mode) {
1293 case GST_RTP_H263_PAYLOAD_HEADER_MODE_A:
1294 GST_LOG_OBJECT (rtph263pay, "Pushing A packet");
1295 gst_rtp_h263_pay_splat_header_A (header, package, context->piclayer);
1297 case GST_RTP_H263_PAYLOAD_HEADER_MODE_B:
1298 GST_LOG_OBJECT (rtph263pay, "Pushing B packet");
1299 gst_rtp_h263_pay_splat_header_B (header, package, context->piclayer);
1301 case GST_RTP_H263_PAYLOAD_HEADER_MODE_C:
1302 //gst_rtp_h263_pay_splat_header_C(header, package, context->piclayer);
1305 return GST_FLOW_ERROR;
1309 * timestamp the buffer
1311 GST_BUFFER_PTS (package->outbuf) = rtph263pay->first_ts;
1313 gst_rtp_buffer_set_marker (&rtp, package->marker);
1314 if (package->marker) {
1315 GST_BUFFER_FLAG_SET (package->outbuf, GST_BUFFER_FLAG_MARKER);
1316 GST_DEBUG_OBJECT (rtph263pay, "Marker set!");
1319 gst_rtp_buffer_unmap (&rtp);
1322 * Copy the payload data in the buffer
1324 GST_DEBUG_OBJECT (rtph263pay, "Copying memory");
1325 gst_buffer_copy_into (package->outbuf, rtph263pay->current_buffer,
1326 GST_BUFFER_COPY_MEMORY, package->payload_start - rtph263pay->map.data,
1327 package->payload_len);
1328 gst_rtp_copy_video_meta (rtph263pay, package->outbuf,
1329 rtph263pay->current_buffer);
1332 gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtph263pay),
1334 GST_DEBUG_OBJECT (rtph263pay, "Package pushed, returning");
1336 gst_rtp_h263_pay_package_destroy (package);
1341 static GstFlowReturn
1342 gst_rtp_h263_pay_A_fragment_push (GstRtpH263Pay * rtph263pay,
1343 GstRtpH263PayContext * context, guint first, guint last)
1346 GstRtpH263PayPackage *pack;
1348 pack = gst_rtp_h263_pay_package_new_empty ();
1350 pack->payload_start = context->gobs[first]->start;
1351 pack->sbit = context->gobs[first]->sbit;
1352 pack->ebit = context->gobs[last]->ebit;
1354 (context->gobs[last]->end - context->gobs[first]->start) + 1;
1355 pack->marker = FALSE;
1357 if (last == context->no_gobs - 1) {
1358 pack->marker = TRUE;
1361 pack->gobn = context->gobs[first]->gobn;
1362 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_A;
1364 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
1365 (rtph263pay), pack->mode, 0, 0);
1367 GST_DEBUG_OBJECT (rtph263pay, "Sending len:%d data to push function",
1370 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1373 static GstFlowReturn
1374 gst_rtp_h263_pay_B_fragment_push (GstRtpH263Pay * rtph263pay,
1375 GstRtpH263PayContext * context, GstRtpH263PayGob * gob, guint first,
1376 guint last, GstRtpH263PayBoundry * boundry)
1379 GstRtpH263PayPackage *pack;
1382 pack = gst_rtp_h263_pay_package_new_empty ();
1384 pack->payload_start = gob->macroblocks[first]->start;
1385 pack->sbit = gob->macroblocks[first]->sbit;
1387 pack->payload_start = boundry->start;
1388 pack->sbit = boundry->sbit;
1389 pack->quant = gob->quant;
1391 pack->quant = gob->macroblocks[first]->quant;
1393 pack->payload_end = gob->macroblocks[last]->end;
1395 pack->ebit = gob->macroblocks[last]->ebit;
1396 pack->mba = gob->macroblocks[first]->mba;
1397 pack->gobn = gob->gobn;
1398 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_B;
1401 if (gob->macroblocks[first]->mb_type < 3) {
1402 if (gob->macroblocks[first]->mb_type == 2)
1404 else if (gob->macroblocks[first]->mb_type < 2)
1407 for (mv = 0; mv < pack->nmvd; mv++)
1408 pack->mvd[mv] = gob->macroblocks[first]->mvd[mv];
1411 pack->marker = FALSE;
1412 if (last == gob->nmacroblocs - 1) {
1416 if ((format_props[context->piclayer->ptype_srcformat][0] - 1 == gob->gobn)
1417 && (last == gob->nmacroblocs - 1)) {
1418 pack->marker = TRUE;
1421 pack->payload_len = pack->payload_end - pack->payload_start + 1;
1423 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
1424 (rtph263pay), pack->mode, 0, 0);
1426 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1431 gst_rtp_h263_pay_mode_B_fragment (GstRtpH263Pay * rtph263pay,
1432 GstRtpH263PayContext * context, GstRtpH263PayGob * gob)
1436 /*---------- MODE B MODE FRAGMENTATION ----------*/
1437 GstRtpH263PayMB *mac, *mac0;
1438 guint max_payload_size;
1439 GstRtpH263PayBoundry boundry;
1447 context->mtu - GST_RTP_H263_PAYLOAD_HEADER_MODE_B - GST_RTP_HEADER_LEN;
1449 gst_rtp_h263_pay_boundry_init (&boundry, gob->start, gob->start, gob->sbit,
1453 (GstRtpH263PayMB **) g_malloc0 (sizeof (GstRtpH263PayMB *) *
1454 format_props[context->piclayer->ptype_srcformat][1]);
1456 GST_LOG_OBJECT (rtph263pay, "GOB isn't PB frame, applying mode B");
1458 //initializing window
1459 context->win_end = boundry.end;
1460 if (gst_rtp_h263_pay_move_window_right (rtph263pay, context, 32, boundry.ebit,
1461 &boundry.end, &gob->end) != 0) {
1462 GST_ERROR_OBJECT (rtph263pay,
1463 "The rest of the bits should be 0, exiting, because something bad happend");
1466 //The first GOB of a frame "has no" actual header - PICTURE header is his header
1467 if (gob->gobn == 0) {
1469 GST_LOG_OBJECT (rtph263pay, "Initial GOB");
1473 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1474 boundry.ebit, &boundry.end, &gob->end);
1476 //We need PQUANT for mode B packages - so we store it
1477 gob->quant = context->window >> 27;
1479 //if PCM == 1, then PSBI is present - header has 51 bits
1480 //shift for PQUANT (5) and PCM (1) = 6 bits
1482 if (context->cpm == 1)
1485 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1486 boundry.ebit, &boundry.end, &gob->end);
1488 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1490 //Shifting the PEI and PSPARE fields
1491 while ((context->window & 0x80000000) == 0x80000000) {
1493 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 9,
1494 boundry.ebit, &boundry.end, &gob->end);
1495 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1498 //shift the last PEI field
1500 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 1,
1501 boundry.ebit, &boundry.end, &gob->end);
1504 //skipping GOBs 24 header bits + 5 GQUANT
1507 GST_TRACE_OBJECT (rtph263pay, "INTER GOB");
1509 //if CPM == 1, there are 2 more bits in the header - GSBI header is 31 bits long
1510 if (context->cpm == 1)
1513 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1515 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1516 boundry.ebit, &boundry.end, &gob->end);
1518 //We need GQUANT for mode B packages - so we store it
1519 gob->quant = context->window >> 27;
1523 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1524 boundry.ebit, &boundry.end, &gob->end);
1526 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1529 GST_TRACE_OBJECT (rtph263pay, "GQUANT IS: %08x", gob->quant);
1531 // We are on MB layer
1533 mac = mac0 = gst_rtp_h263_pay_mb_new (&boundry, 0);
1534 for (mb = 0; mb < format_props[context->piclayer->ptype_srcformat][1]; mb++) {
1536 GST_TRACE_OBJECT (rtph263pay,
1537 "================ START MB %d =================", mb);
1539 //Find next macroblock boundaries
1542 gst_rtp_h263_pay_B_mbfinder (rtph263pay, context, gob, mac, mb))) {
1544 GST_LOG_OBJECT (rtph263pay, "Error decoding MB - sbit: %d", 8 - ebit);
1545 GST_ERROR_OBJECT (rtph263pay, "Error decoding in GOB");
1547 gst_rtp_h263_pay_mb_destroy (mac0);
1551 /* Store macroblock for further processing and delete old MB if any */
1552 gst_rtp_h263_pay_mb_destroy (gob->macroblocks[mb]);
1553 gob->macroblocks[mb] = mac;
1555 //If mb_type == stuffing, don't increment the mb address
1556 if (mac->mb_type == 10) {
1563 if (mac->end >= gob->end) {
1564 GST_LOG_OBJECT (rtph263pay, "No more MBs in this GOB");
1568 gob->end = mac->end;
1571 GST_DEBUG_OBJECT (rtph263pay,
1572 "Found MB: mba: %d start: %p end: %p len: %d sbit: %d ebit: %d",
1573 mac->mba, mac->start, mac->end, mac->length, mac->sbit, mac->ebit);
1574 GST_TRACE_OBJECT (rtph263pay,
1575 "================ END MB %d =================", mb);
1577 gst_rtp_h263_pay_mb_destroy (mac0);
1581 payload_len = boundry.end - boundry.start + 1;
1582 GST_DEBUG_OBJECT (rtph263pay,
1583 "------------------------- NEW PACKAGE ----------------------");
1584 while (mb < gob->nmacroblocs) {
1585 if (payload_len + gob->macroblocks[mb]->length < max_payload_size) {
1587 //FIXME: payload_len is not the real length -> ignoring sbit/ebit
1588 payload_len += gob->macroblocks[mb]->length;
1592 //FIXME: we should include the last few bits of the GOB in the package - do we do that now?
1593 //GST_DEBUG_OBJECT (rtph263pay, "Pushing GOBS %d to %d because payload size is %d", first,
1594 // first == mb - 1, payload_len);
1596 // FIXME: segfault if mb == 0 (first MB is larger than max_payload_size)
1597 GST_DEBUG_OBJECT (rtph263pay, "Push B mode fragment from mb %d to %d",
1599 if (gst_rtp_h263_pay_B_fragment_push (rtph263pay, context, gob, first,
1600 mb - 1, &boundry)) {
1601 GST_ERROR_OBJECT (rtph263pay, "Oooops, there was an error sending");
1607 GST_DEBUG_OBJECT (rtph263pay,
1608 "------------------------- END PACKAGE ----------------------");
1609 GST_DEBUG_OBJECT (rtph263pay,
1610 "------------------------- NEW PACKAGE ----------------------");
1615 GST_DEBUG_OBJECT (rtph263pay, "Remainder first: %d, MB: %d", first, mb);
1616 if (payload_len != 0) {
1617 GST_DEBUG_OBJECT (rtph263pay, "Push B mode fragment from mb %d to %d",
1619 if (gst_rtp_h263_pay_B_fragment_push (rtph263pay, context, gob, first,
1620 mb - 1, &boundry)) {
1621 GST_ERROR_OBJECT (rtph263pay, "Oooops, there was an error sending!");
1626 /*---------- END OF MODE B FRAGMENTATION ----------*/
1634 static GstFlowReturn
1635 gst_rtp_h263_send_entire_frame (GstRtpH263Pay * rtph263pay,
1636 GstRtpH263PayContext * context)
1638 GstRtpH263PayPackage *pack;
1640 gst_rtp_h263_pay_package_new (rtph263pay->data,
1641 rtph263pay->data + rtph263pay->available_data,
1642 rtph263pay->available_data, 0, 0, NULL, TRUE);
1643 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_A;
1645 GST_DEBUG_OBJECT (rtph263pay, "Available data: %d",
1646 rtph263pay->available_data);
1649 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
1650 (rtph263pay), GST_RTP_H263_PAYLOAD_HEADER_MODE_A, 0, 0);
1652 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1655 static GstFlowReturn
1656 gst_rtp_h263_pay_flush (GstRtpH263Pay * rtph263pay)
1660 * FIXME: GSTUF bits are ignored right now,
1661 * - not using EBIT/SBIT payload header fields in mode A fragmentation - ffmpeg doesn't need them, but others?
1665 GstRtpH263PayContext *context;
1669 context = (GstRtpH263PayContext *) g_malloc0 (sizeof (GstRtpH263PayContext));
1671 rtph263pay->payload.mtu - (MTU_SECURITY_OFFSET + GST_RTP_HEADER_LEN +
1672 GST_RTP_H263_PAYLOAD_HEADER_MODE_C);
1674 GST_DEBUG_OBJECT (rtph263pay, "MTU: %d", context->mtu);
1675 rtph263pay->available_data = gst_buffer_get_size (rtph263pay->current_buffer);
1676 if (rtph263pay->available_data == 0) {
1681 /* Get a pointer to all the data for the frame */
1682 gst_buffer_map (rtph263pay->current_buffer, &rtph263pay->map, GST_MAP_READ);
1683 rtph263pay->data = (guint8 *) rtph263pay->map.data;
1685 /* Picture header */
1686 context->piclayer = (GstRtpH263PayPic *) rtph263pay->data;
1688 if (context->piclayer->ptype_pictype == 0)
1689 GST_DEBUG_OBJECT (rtph263pay, "We got an I-frame");
1691 GST_DEBUG_OBJECT (rtph263pay, "We got a P-frame");
1693 context->cpm = rtph263pay->data[6] >> 7;
1695 GST_DEBUG_OBJECT (rtph263pay, "CPM: %d", context->cpm);
1697 GST_DEBUG_OBJECT (rtph263pay, "Payload length is: %d",
1698 rtph263pay->available_data);
1701 * - MODE A - If normal, I and P frames, -> mode A
1702 * - GOB layer fragmentation
1703 * - MODE B - If normal, I and P frames, but GOBs > mtu
1704 * - MB layer fragmentation
1705 * - MODE C - For P frames with PB option, but GOBs > mtu
1706 * - MB layer fragmentation
1708 if (rtph263pay->available_data + GST_RTP_H263_PAYLOAD_HEADER_MODE_A +
1709 GST_RTP_HEADER_LEN < context->mtu) {
1710 ret = gst_rtp_h263_send_entire_frame (rtph263pay, context);
1713 /*---------- FRAGMENTING THE FRAME BECAUSE TOO LARGE TO FIT IN MTU ----------*/
1714 GstRtpH263PayBoundry bound;
1717 gboolean forcea = FALSE;
1719 GST_DEBUG_OBJECT (rtph263pay, "Frame too large for MTU");
1721 * Let's go trough all the data and fragment it until end is reached
1724 gst_rtp_h263_pay_boundry_init (&bound, NULL, rtph263pay->data - 1, 0, 0);
1726 (GstRtpH263PayGob **) g_malloc0 (format_props[context->piclayer->
1727 ptype_srcformat][0] * sizeof (GstRtpH263PayGob *));
1730 for (i = 0; i < format_props[context->piclayer->ptype_srcformat][0]; i++) {
1731 GST_DEBUG_OBJECT (rtph263pay, "Searching for gob %d", i);
1732 if (!gst_rtp_h263_pay_gobfinder (rtph263pay, &bound)) {
1734 GST_WARNING_OBJECT (rtph263pay,
1735 "No GOB's were found in data stream! Please enable RTP mode in encoder. Forcing mode A for now.");
1736 ret = gst_rtp_h263_send_entire_frame (rtph263pay, context);
1739 /* try to send fragments corresponding to found GOBs */
1745 context->gobs[i] = gst_rtp_h263_pay_gob_new (&bound, i);
1746 //FIXME - encoders may generate an EOS gob that has to be processed
1747 GST_DEBUG_OBJECT (rtph263pay,
1748 "Gob values are: gobn: %d, start: %p len: %d ebit: %d sbit: %d", i,
1749 context->gobs[i]->start, context->gobs[i]->length,
1750 context->gobs[i]->ebit, context->gobs[i]->sbit);
1752 /* NOTE some places may still assume this to be the max possible */
1753 context->no_gobs = i;
1754 GST_DEBUG_OBJECT (rtph263pay, "Found %d GOBS of maximum %d",
1755 context->no_gobs, format_props[context->piclayer->ptype_srcformat][0]);
1757 // Make packages smaller than MTU
1759 // - if ( GOB > MTU) -> B mode || C mode
1765 while (i < context->no_gobs) {
1767 if (context->gobs[i]->length >= context->mtu) {
1768 if (payload_len == 0) {
1770 GST_DEBUG_OBJECT (rtph263pay, "GOB len > MTU");
1771 if (rtph263pay->prop_payload_mode || forcea) {
1772 payload_len = context->gobs[i]->length;
1775 if (!context->piclayer->ptype_pbmode) {
1776 GST_DEBUG_OBJECT (rtph263pay, "MODE B on GOB %d needed", i);
1777 if (!gst_rtp_h263_pay_mode_B_fragment (rtph263pay, context,
1778 context->gobs[i])) {
1779 GST_ERROR_OBJECT (rtph263pay,
1780 "There was an error fragmenting in mode B");
1781 ret = GST_FLOW_ERROR;
1786 GST_ERROR_OBJECT (rtph263pay,
1787 "MODE C on GOB %d needed, but not supported yet", i);
1788 /*if(!gst_rtp_h263_pay_mode_C_fragment(rtph263pay, context, context->gobs[i])) {
1790 GST_ERROR("There was an error fragmenting in mode C");
1801 goto payload_a_push;
1805 if (payload_len + context->gobs[i]->length < context->mtu) {
1806 GST_DEBUG_OBJECT (rtph263pay, "GOB %d fills mtu", i);
1807 payload_len += context->gobs[i]->length;
1809 if (i == context->no_gobs) {
1810 GST_DEBUG_OBJECT (rtph263pay, "LAST GOB %d", i);
1811 goto payload_a_push;
1816 GST_DEBUG_OBJECT (rtph263pay,
1817 "Pushing GOBS %d to %d because payload size is %d", first,
1818 first == i ? i : i - 1, payload_len);
1819 gst_rtp_h263_pay_A_fragment_push (rtph263pay, context, first,
1820 first == i ? i : i - 1);
1827 GST_DEBUG_OBJECT (rtph263pay,
1828 "Pushing GOBS %d to %d because payload size is %d", first, i,
1830 gst_rtp_h263_pay_A_fragment_push (rtph263pay, context, first, i);
1837 }/*---------- END OF FRAGMENTATION ----------*/
1839 /* Flush the input buffer data */
1842 gst_rtp_h263_pay_context_destroy (context,
1843 context->piclayer->ptype_srcformat);
1844 gst_buffer_unmap (rtph263pay->current_buffer, &rtph263pay->map);
1845 gst_buffer_replace (&rtph263pay->current_buffer, NULL);
1850 static GstFlowReturn
1851 gst_rtp_h263_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
1854 GstRtpH263Pay *rtph263pay;
1857 rtph263pay = GST_RTP_H263_PAY (payload);
1858 GST_DEBUG_OBJECT (rtph263pay,
1859 "-------------------- NEW FRAME ---------------");
1861 rtph263pay->first_ts = GST_BUFFER_PTS (buffer);
1863 gst_buffer_replace (&rtph263pay->current_buffer, buffer);
1864 gst_buffer_unref (buffer);
1866 /* we always encode and flush a full picture */
1867 ret = gst_rtp_h263_pay_flush (rtph263pay);
1868 GST_DEBUG_OBJECT (rtph263pay,
1869 "-------------------- END FRAME ---------------");