1 /* GStreamer
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>
5 *
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.
10 *
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.
15 *
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.
20 *
21 */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <string.h>
28 #include <math.h>
29 #include <gst/rtp/gstrtpbuffer.h>
30 #include <gst/video/video.h>
31
32 #include "gstrtpelements.h"
33 #include "gstrtph263pay.h"
34 #include "gstrtputils.h"
35
36 typedef enum
37 {
38 GST_H263_FRAME_TYPE_I = 0,
39 GST_H263_FRAME_TYPE_P = 1,
40 GST_H263_FRAME_TYPE_PB = 2
41 } GstRtpH263PayFrameType;
42
43 typedef enum
44 {
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;
54
55 static const guint format_props[8][2] = { {254, 254},
56 {6, 8},
57 {9, 11},
58 {18, 22},
59 {18, 88},
60 {18, 352},
61 {254, 254},
62 {255, 255}
63 };
64
65 /*
66 * I-frame MCBPC table: code, mask, nbits, cb, cr, mb type -> 10 = undefined (because we have guint)
67 */
68 #define MCBPC_I_LEN 9
69 #define MCBPC_I_WID 6
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}
80 };
81
82 /*
83 * P-frame MCBPC table: code, mask, nbits, cb, cr, mb type -> 10 = undefined (because we have guint)
84 */
85 #define MCBPC_P_LEN 21
86 #define MCBPC_P_WID 6
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}
109 };
110
111 /*
112 * I-frame CBPY (code, mask, nbits, Y0, Y1, Y2, Y3)
113 */
114 #define CBPY_LEN 16
115 #define CBPY_WID 7
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}
133 };
134
135 /*
136 * P-frame CBPY (code, mask, nbits, Y0, Y1, Y2, Y3)
137 */
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}
155 };
156
157 /*
158 * Block TCOEF table (code, mask, nbits, LAST, RUN, LEVEL)
159 */
160 #define TCOEF_LEN 103
161 #define TCOEF_WID 6
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}
266 };
267
268 /*
269 * Motion vector code table (Code, mask, nbits, vector (halfpixel, two's complement), diff (halfpixel, two's complement))
270 */
271 #define MVD_LEN 64
272 #define MVD_WID 5
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}
338 };
339
340 GST_DEBUG_CATEGORY_STATIC (rtph263pay_debug);
341 #define GST_CAT_DEFAULT (rtph263pay_debug)
342
343 #define GST_RTP_HEADER_LEN 12
344
345 enum
346 {
347 PROP_0,
348 PROP_MODE_A_ONLY
349 };
350
351 static GstStaticPadTemplate gst_rtp_h263_pay_sink_template =
352 GST_STATIC_PAD_TEMPLATE ("sink",
353 GST_PAD_SINK,
354 GST_PAD_ALWAYS,
355 GST_STATIC_CAPS ("video/x-h263, "
356 "variant = (string) \"itu\", " "h263version = (string) \"h263\"")
357 );
358
359 static GstStaticPadTemplate gst_rtp_h263_pay_src_template =
360 GST_STATIC_PAD_TEMPLATE ("src",
361 GST_PAD_SRC,
362 GST_PAD_ALWAYS,
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\"")
371 );
372
373 static void gst_rtp_h263_pay_finalize (GObject * object);
374
375 static gboolean gst_rtp_h263_pay_setcaps (GstRTPBasePayload * payload,
376 GstCaps * caps);
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);
383
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,
389 guint mba);
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,
393 gboolean marker);
394
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,
398 guint ind);
399 static void gst_rtp_h263_pay_package_destroy (GstRtpH263PayPackage * pack);
400
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));
405
406 static void
gst_rtp_h263_pay_class_init(GstRtpH263PayClass * klass)407 gst_rtp_h263_pay_class_init (GstRtpH263PayClass * klass)
408 {
409 GObjectClass *gobject_class;
410 GstElementClass *gstelement_class;
411 GstRTPBasePayloadClass *gstrtpbasepayload_class;
412
413 gobject_class = (GObjectClass *) klass;
414 gstelement_class = (GstElementClass *) klass;
415 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
416
417 gobject_class->finalize = gst_rtp_h263_pay_finalize;
418
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;
423
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));
429
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);
434
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>");
440
441 GST_DEBUG_CATEGORY_INIT (rtph263pay_debug, "rtph263pay", 0,
442 "H263 RTP Payloader");
443 }
444
445 static void
gst_rtp_h263_pay_init(GstRtpH263Pay * rtph263pay)446 gst_rtp_h263_pay_init (GstRtpH263Pay * rtph263pay)
447 {
448 GST_RTP_BASE_PAYLOAD_PT (rtph263pay) = GST_RTP_PAYLOAD_H263;
449 rtph263pay->prop_payload_mode = DEFAULT_MODE_A;
450 }
451
452 static void
gst_rtp_h263_pay_finalize(GObject * object)453 gst_rtp_h263_pay_finalize (GObject * object)
454 {
455 GstRtpH263Pay *rtph263pay;
456
457 rtph263pay = GST_RTP_H263_PAY (object);
458
459 gst_buffer_replace (&rtph263pay->current_buffer, NULL);
460
461 G_OBJECT_CLASS (parent_class)->finalize (object);
462 }
463
464 static gboolean
gst_rtp_h263_pay_setcaps(GstRTPBasePayload * payload,GstCaps * caps)465 gst_rtp_h263_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
466 {
467 GstStructure *s = gst_caps_get_structure (caps, 0);
468 gint width, height;
469 gchar *framesize = NULL;
470 gboolean res;
471
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;
476 }
477
478 if (!gst_structure_get_int (s, "height", &height) || height <= 0) {
479 goto invalid_dimension;
480 }
481
482 framesize = g_strdup_printf ("%d-%d", width, height);
483 }
484
485 gst_rtp_base_payload_set_options (payload, "video",
486 payload->pt != GST_RTP_PAYLOAD_H263, "H263", 90000);
487
488 if (framesize != NULL) {
489 res = gst_rtp_base_payload_set_outcaps (payload,
490 "a-framesize", G_TYPE_STRING, framesize, NULL);
491 } else {
492 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
493 }
494 g_free (framesize);
495
496 return res;
497
498 /* ERRORS */
499 invalid_dimension:
500 {
501 GST_ERROR_OBJECT (payload, "Invalid width/height from caps");
502 return FALSE;
503 }
504 }
505
506 static void
gst_rtp_h263_pay_context_destroy(GstRtpH263PayContext * context,guint ind)507 gst_rtp_h263_pay_context_destroy (GstRtpH263PayContext * context, guint ind)
508 {
509 if (!context)
510 return;
511
512 if (context->gobs) {
513 guint i;
514
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);
518 }
519 }
520
521 g_free (context->gobs);
522 }
523
524 g_free (context);
525 }
526
527 static void
gst_rtp_h263_pay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)528 gst_rtp_h263_pay_set_property (GObject * object, guint prop_id,
529 const GValue * value, GParamSpec * pspec)
530 {
531 GstRtpH263Pay *rtph263pay;
532
533 rtph263pay = GST_RTP_H263_PAY (object);
534
535 switch (prop_id) {
536 case PROP_MODE_A_ONLY:
537 rtph263pay->prop_payload_mode = g_value_get_boolean (value);
538 break;
539 default:
540 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
541 break;
542 }
543 }
544
545 static void
gst_rtp_h263_pay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)546 gst_rtp_h263_pay_get_property (GObject * object, guint prop_id,
547 GValue * value, GParamSpec * pspec)
548 {
549 GstRtpH263Pay *rtph263pay;
550
551 rtph263pay = GST_RTP_H263_PAY (object);
552
553 switch (prop_id) {
554 case PROP_MODE_A_ONLY:
555 g_value_set_boolean (value, rtph263pay->prop_payload_mode);
556 break;
557 default:
558 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
559 break;
560 }
561 }
562
563 static GstRtpH263PayPackage *
gst_rtp_h263_pay_package_new_empty(void)564 gst_rtp_h263_pay_package_new_empty (void)
565 {
566 return (GstRtpH263PayPackage *) g_malloc0 (sizeof (GstRtpH263PayPackage));
567 }
568
569 static GstRtpH263PayPackage *
gst_rtp_h263_pay_package_new(guint8 * start,guint8 * end,guint length,guint8 sbit,guint8 ebit,GstBuffer * outbuf,gboolean marker)570 gst_rtp_h263_pay_package_new (guint8 * start, guint8 * end, guint length,
571 guint8 sbit, guint8 ebit, GstBuffer * outbuf, gboolean marker)
572 {
573
574 GstRtpH263PayPackage *package;
575
576 package = gst_rtp_h263_pay_package_new_empty ();
577
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;
585
586 return package;
587 }
588
589 static void
gst_rtp_h263_pay_package_destroy(GstRtpH263PayPackage * pack)590 gst_rtp_h263_pay_package_destroy (GstRtpH263PayPackage * pack)
591 {
592 if (pack)
593 g_free (pack);
594 }
595
596 static void
gst_rtp_h263_pay_boundry_init(GstRtpH263PayBoundry * boundry,guint8 * start,guint8 * end,guint8 sbit,guint8 ebit)597 gst_rtp_h263_pay_boundry_init (GstRtpH263PayBoundry * boundry,
598 guint8 * start, guint8 * end, guint8 sbit, guint8 ebit)
599 {
600 boundry->start = start;
601 boundry->end = end;
602 boundry->sbit = sbit;
603 boundry->ebit = ebit;
604 }
605
606 static void
gst_rtp_h263_pay_splat_header_A(guint8 * header,GstRtpH263PayPackage * package,GstRtpH263PayPic * piclayer)607 gst_rtp_h263_pay_splat_header_A (guint8 * header,
608 GstRtpH263PayPackage * package, GstRtpH263PayPic * piclayer)
609 {
610
611 GstRtpH263PayAHeader *a_header;
612
613 a_header = (GstRtpH263PayAHeader *) header;
614
615 a_header->f = 0;
616 a_header->p = 0;
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);
624 a_header->r1 = 0;
625 a_header->r2 = 0;
626 a_header->dbq = 0;
627 a_header->trb = 0;
628 a_header->tr = 0;
629
630 }
631
632 static void
gst_rtp_h263_pay_splat_header_B(guint8 * header,GstRtpH263PayPackage * package,GstRtpH263PayPic * piclayer)633 gst_rtp_h263_pay_splat_header_B (guint8 * header,
634 GstRtpH263PayPackage * package, GstRtpH263PayPic * piclayer)
635 {
636
637 GstRtpH263PayBHeader *b_header;
638
639 b_header = (GstRtpH263PayBHeader *) header;
640
641 b_header->f = 1;
642 b_header->p = 0;
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;
650 b_header->r = 0;
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);
655
656 b_header->hmv11 = 0;
657 b_header->hmv12 = 0;
658 b_header->vmv11 = 0;
659 b_header->vmv12 = 0;
660 b_header->hmv21 = 0;
661 b_header->hmv22 = 0;
662 b_header->vmv21 = 0;
663
664 if (package->nmvd > 0) {
665 //mvd[0]
666 b_header->hmv11 = (package->mvd[0] & 0x7f) >> 3;
667 b_header->hmv12 = (package->mvd[0] & 0x07);
668 //mvd[1]
669 b_header->vmv11 = (package->mvd[1] & 0x07f) >> 2;
670 b_header->vmv12 = (package->mvd[1] & 0x03);
671
672 if (package->nmvd == 8) {
673 //mvd[4]
674 b_header->hmv21 = (package->mvd[4] & 0x7f) >> 1;
675 b_header->hmv22 = (package->mvd[4] & 0x01);
676 //mvd[5]
677 b_header->vmv21 = (package->mvd[5] & 0x7f);
678 }
679 }
680
681 }
682
683 static gboolean
gst_rtp_h263_pay_gobfinder(GstRtpH263Pay * rtph263pay,GstRtpH263PayBoundry * boundry)684 gst_rtp_h263_pay_gobfinder (GstRtpH263Pay * rtph263pay,
685 GstRtpH263PayBoundry * boundry)
686 {
687 guint8 *current;
688 guint range;
689 guint i;
690
691 current = boundry->end + 1;
692 range = (rtph263pay->data - current) + rtph263pay->available_data;
693
694
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);
699
700 /* If we are past the end, stop */
701 if (current >= rtph263pay->data + rtph263pay->available_data)
702 return FALSE;
703
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,
711 0, 0);
712
713 return TRUE;
714 }
715 }
716
717 GST_DEBUG_OBJECT (rtph263pay,
718 "Couldn't find any new GBSC in this frame, range:%u", range);
719
720 gst_rtp_h263_pay_boundry_init (boundry, boundry->end + 1,
721 (guint8 *) (rtph263pay->data + rtph263pay->available_data - 1), 0, 0);
722
723 return TRUE;
724 }
725
726 static GstRtpH263PayGob *
gst_rtp_h263_pay_gob_new(GstRtpH263PayBoundry * boundry,guint gobn)727 gst_rtp_h263_pay_gob_new (GstRtpH263PayBoundry * boundry, guint gobn)
728 {
729 GstRtpH263PayGob *gob;
730
731 gob = (GstRtpH263PayGob *) g_malloc0 (sizeof (GstRtpH263PayGob));
732
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;
738 gob->gobn = gobn;
739 gob->quant = 0;
740 gob->macroblocks = NULL;
741 gob->nmacroblocs = 0;
742
743 return gob;
744 }
745
746 static void
gst_rtp_h263_pay_gob_destroy(GstRtpH263PayGob * gob,guint ind)747 gst_rtp_h263_pay_gob_destroy (GstRtpH263PayGob * gob, guint ind)
748 {
749
750 if (!gob)
751 return;
752
753 if (gob->macroblocks) {
754
755 guint i;
756
757 for (i = 0; i < gob->nmacroblocs; i++) {
758 gst_rtp_h263_pay_mb_destroy (gob->macroblocks[i]);
759 }
760
761 g_free (gob->macroblocks);
762 }
763
764 g_free (gob);
765 }
766
767 /*
768 * decode MCBPC for I frames and return index in table or -1 if not found
769 */
770 static gint
gst_rtp_h263_pay_decode_mcbpc_I(GstRtpH263Pay * rtph263pay,guint32 value)771 gst_rtp_h263_pay_decode_mcbpc_I (GstRtpH263Pay * rtph263pay, guint32 value)
772 {
773
774 gint i;
775 guint16 code;
776
777 code = value >> 16;
778
779 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
780
781 for (i = 0; i < MCBPC_I_LEN; i++) {
782 if ((code & mcbpc_I[i][1]) == mcbpc_I[i][0]) {
783 return i;
784 }
785 }
786
787 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
788
789 return -1;
790 }
791
792 /*
793 * decode MCBPC for P frames and return index in table or -1 if not found
794 */
795 static gint
gst_rtp_h263_pay_decode_mcbpc_P(GstRtpH263Pay * rtph263pay,guint32 value)796 gst_rtp_h263_pay_decode_mcbpc_P (GstRtpH263Pay * rtph263pay, guint32 value)
797 {
798
799 gint i;
800 guint16 code;
801
802 code = value >> 16;
803
804 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
805
806 for (i = 0; i < MCBPC_P_LEN; i++) {
807 if ((code & mcbpc_P[i][1]) == mcbpc_P[i][0]) {
808 return i;
809 }
810 }
811
812 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
813
814 return -1;
815 }
816
817 /*
818 * decode CBPY and return index in table or -1 if not found
819 */
820 static gint
gst_rtp_h263_pay_decode_cbpy(GstRtpH263Pay * rtph263pay,guint32 value,const guint8 cbpy_table[16][7])821 gst_rtp_h263_pay_decode_cbpy (GstRtpH263Pay * rtph263pay, guint32 value,
822 const guint8 cbpy_table[16][7])
823 {
824
825 gint i;
826 guint8 code;
827
828 code = value >> 24;
829
830 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
831
832 for (i = 0; i < CBPY_LEN; i++) {
833 if ((code & cbpy_table[i][1]) == cbpy_table[i][0]) {
834 return i;
835 }
836 }
837
838 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
839
840 return -1;
841 }
842
843 /*
844 * decode MVD and return index in table or -1 if not found
845 */
846 static gint
gst_rtp_h263_pay_decode_mvd(GstRtpH263Pay * rtph263pay,guint32 value)847 gst_rtp_h263_pay_decode_mvd (GstRtpH263Pay * rtph263pay, guint32 value)
848 {
849
850 gint i;
851 guint16 code;
852
853 code = value >> 16;
854
855 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
856
857 for (i = 0; i < MVD_LEN; i++) {
858 if ((code & mvd[i][1]) == mvd[i][0]) {
859 return i;
860 }
861 }
862
863 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
864
865 return -1;
866 }
867
868 /*
869 * decode TCOEF and return index in table or -1 if not found
870 */
871 static gint
gst_rtp_h263_pay_decode_tcoef(GstRtpH263Pay * rtph263pay,guint32 value)872 gst_rtp_h263_pay_decode_tcoef (GstRtpH263Pay * rtph263pay, guint32 value)
873 {
874
875 gint i;
876 guint16 code;
877
878 code = value >> 16;
879
880 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
881
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);
885 return i;
886 }
887 }
888
889 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
890
891 return -1;
892 }
893
894 /*
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
897 */
898
899 static gint
gst_rtp_h263_pay_move_window_right(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,guint n,guint rest_bits,guint8 ** orig_data,guint8 ** data_end)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)
903 {
904
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);
908
909 if (n == 0)
910 return rest_bits;
911
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) {
915 if (n > 8) {
916 context->window = (context->window << 8) | b;
917 n -= 8;
918 } else {
919 context->window = (context->window << n) | (b >> (8 - n));
920 rest_bits = 8 - n;
921 if (rest_bits == 0)
922 context->win_end++;
923 break;
924 }
925 } else {
926 if (n > rest_bits) {
927 context->window = (context->window << rest_bits) |
928 (b & (((guint) pow (2.0, (double) rest_bits)) - 1));
929 n -= rest_bits;
930 rest_bits = 0;
931 } else {
932 context->window = (context->window << n) |
933 ((b & (((guint) pow (2.0, (double) rest_bits)) - 1)) >>
934 (rest_bits - n));
935 rest_bits -= n;
936 if (rest_bits == 0)
937 context->win_end++;
938 break;
939 }
940 }
941
942 context->win_end++;
943 }
944
945 *orig_data = context->win_end - 4;
946
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);
950 return rest_bits;
951 }
952
953 /*
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
957 */
958 static GstRtpH263PayMB *
gst_rtp_h263_pay_B_mbfinder(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,GstRtpH263PayGob * gob,GstRtpH263PayMB * macroblock,guint mba)959 gst_rtp_h263_pay_B_mbfinder (GstRtpH263Pay * rtph263pay,
960 GstRtpH263PayContext * context, GstRtpH263PayGob * gob,
961 GstRtpH263PayMB * macroblock, guint mba)
962 {
963 guint mb_type_index;
964 guint cbpy_type_index;
965 guint tcoef_type_index;
966 GstRtpH263PayMB *mac;
967 GstRtpH263PayBoundry boundry;
968
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);
972
973 if (mac->sbit == 8) {
974 mac->start++;
975 // mac->end++;
976 mac->sbit = 0;
977 }
978
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);
982
983 if (context->piclayer->ptype_pictype == 0) {
984 //We have an I frame
985 gint i;
986 guint last;
987 guint ind;
988
989 //Step 2 decode MCBPC I
990 mb_type_index =
991 gst_rtp_h263_pay_decode_mcbpc_I (rtph263pay, context->window);
992
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",
996 context->window);
997 goto beach;
998 }
999
1000 mac->ebit =
1001 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1002 mcbpc_I[mb_type_index][2], mac->ebit, &mac->end, &gob->end);
1003
1004 mac->mb_type = mcbpc_I[mb_type_index][5];
1005
1006 if (mb_type_index == 8) {
1007 GST_TRACE_OBJECT (rtph263pay, "Stuffing skipping rest of MB header");
1008 return mac;
1009 }
1010 //Step 3 decode CBPY I
1011 cbpy_type_index =
1012 gst_rtp_h263_pay_decode_cbpy (rtph263pay, context->window, cbpy_I);
1013
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);
1018 goto beach;
1019 }
1020
1021 mac->ebit =
1022 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1023 cbpy_I[cbpy_type_index][2], mac->ebit, &mac->end, &gob->end);
1024
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");
1029
1030 mac->quant = (context->window >> 30);
1031
1032 mac->ebit =
1033 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 2, mac->ebit,
1034 &mac->end, &gob->end);
1035 }
1036 //Step 5 go trough the blocks - decode DC and TCOEF
1037 last = 0;
1038 for (i = 0; i < N_BLOCKS; i++) {
1039
1040 GST_TRACE_OBJECT (rtph263pay, "Decoding INTRADC and TCOEF, i:%d", i);
1041 mac->ebit =
1042 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 8, mac->ebit,
1043 &mac->end, &gob->end);
1044
1045 if (i > 3) {
1046 ind = mcbpc_I[mb_type_index][i - 1];
1047 } else {
1048 ind = cbpy_I[cbpy_type_index][i + 3];
1049 }
1050
1051 if (ind == 1) {
1052 while (last == 0) {
1053 tcoef_type_index =
1054 gst_rtp_h263_pay_decode_tcoef (rtph263pay, context->window);
1055
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);
1060 goto beach;
1061 }
1062 mac->ebit =
1063 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1064 tcoef[tcoef_type_index][2], mac->ebit, &mac->end, &gob->end);
1065
1066 last = tcoef[tcoef_type_index][3];
1067 if (tcoef_type_index == 102) {
1068 if ((context->window & 0x80000000) == 0x80000000)
1069 last = 1;
1070 else
1071 last = 0;
1072
1073 mac->ebit =
1074 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 15,
1075 mac->ebit, &mac->end, &gob->end);
1076 }
1077 }
1078 last = 0;
1079 }
1080 }
1081
1082 } else {
1083 //We have a P frame
1084 guint i;
1085 guint last;
1086 guint ind;
1087
1088 //Step 1 check COD
1089 GST_TRACE_OBJECT (rtph263pay, "Checking for COD");
1090 if ((context->window & 0x80000000) == 0x80000000) {
1091 //The MB is not coded
1092 mac->ebit =
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");
1096
1097 return mac;
1098 } else {
1099 //The MB is coded
1100 mac->ebit =
1101 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 1, mac->ebit,
1102 &mac->end, &gob->end);
1103 }
1104
1105 //Step 2 decode MCBPC P
1106 mb_type_index =
1107 gst_rtp_h263_pay_decode_mcbpc_P (rtph263pay, context->window);
1108
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",
1112 context->window);
1113 goto beach;
1114 }
1115 mac->ebit =
1116 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1117 mcbpc_P[mb_type_index][2], mac->ebit, &mac->end, &gob->end);
1118
1119 mac->mb_type = mcbpc_P[mb_type_index][5];
1120
1121 if (mb_type_index == 20) {
1122 GST_TRACE_OBJECT (rtph263pay, "Stuffing skipping rest of MB header");
1123 return mac;
1124 }
1125 //Step 3 decode CBPY P
1126 cbpy_type_index =
1127 gst_rtp_h263_pay_decode_cbpy (rtph263pay, context->window, cbpy_P);
1128
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);
1133 goto beach;
1134 }
1135 mac->ebit =
1136 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1137 cbpy_P[cbpy_type_index][2], mac->ebit, &mac->end, &gob->end);
1138
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");
1142
1143 mac->quant = context->window >> 30;
1144
1145 mac->ebit =
1146 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 2, mac->ebit,
1147 &mac->end, &gob->end);
1148 }
1149 //MB types < 3 have MVD1-4
1150 if (mcbpc_P[mb_type_index][5] < 3) {
1151
1152 guint nmvd;
1153 gint j;
1154
1155 nmvd = 2;
1156 if (mcbpc_P[mb_type_index][5] == 2)
1157 nmvd = 8;
1158
1159 for (j = 0; j < nmvd; j++) {
1160 guint mvd_type;
1161
1162 mvd_type = gst_rtp_h263_pay_decode_mvd (rtph263pay, context->window);
1163
1164 if (mvd_type == -1) {
1165 GST_ERROR_OBJECT (rtph263pay,
1166 "MVD1-4 index shouldn't be -1 in window: %08x", context->window);
1167 goto beach;
1168 }
1169 //set the MB mvd values
1170 mac->mvd[j] = mvd[mvd_type][3];
1171
1172 mac->ebit =
1173 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1174 mvd[mvd_type][2], mac->ebit, &mac->end, &gob->end);
1175 }
1176
1177
1178 }
1179 //Step 5 go trough the blocks - decode DC and TCOEF
1180 last = 0;
1181 for (i = 0; i < N_BLOCKS; i++) {
1182
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);
1186 mac->ebit =
1187 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 8,
1188 mac->ebit, &mac->end, &gob->end);
1189 } else {
1190 GST_TRACE_OBJECT (rtph263pay, "INTRADC coef is not present");
1191 }
1192
1193 //check if the block has TCOEF
1194 if (i > 3) {
1195 ind = mcbpc_P[mb_type_index][i - 1];
1196 } else {
1197 if (mcbpc_P[mb_type_index][5] > 2) {
1198 ind = cbpy_I[cbpy_type_index][i + 3];
1199 } else {
1200 ind = cbpy_P[cbpy_type_index][i + 3];
1201 }
1202 }
1203
1204 if (ind == 1) {
1205 while (last == 0) {
1206 tcoef_type_index =
1207 gst_rtp_h263_pay_decode_tcoef (rtph263pay, context->window);
1208
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);
1213 goto beach;
1214 }
1215
1216 mac->ebit =
1217 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1218 tcoef[tcoef_type_index][2], mac->ebit, &mac->end, &gob->end);
1219
1220 last = tcoef[tcoef_type_index][3];
1221 if (tcoef_type_index == 102) {
1222 if ((context->window & 0x80000000) == 0x80000000)
1223 last = 1;
1224 else
1225 last = 0;
1226
1227 mac->ebit =
1228 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 15,
1229 mac->ebit, &mac->end, &gob->end);
1230 }
1231 }
1232 last = 0;
1233 }
1234 }
1235 }
1236
1237 mac->length = mac->end - mac->start + 1;
1238
1239 return mac;
1240
1241 beach:
1242 gst_rtp_h263_pay_mb_destroy (mac);
1243 return NULL;
1244 }
1245
1246 static GstRtpH263PayMB *
gst_rtp_h263_pay_mb_new(GstRtpH263PayBoundry * boundry,guint mba)1247 gst_rtp_h263_pay_mb_new (GstRtpH263PayBoundry * boundry, guint mba)
1248 {
1249 GstRtpH263PayMB *mb;
1250 gint i;
1251
1252 mb = (GstRtpH263PayMB *) g_malloc0 (sizeof (GstRtpH263PayMB));
1253
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;
1259 mb->mba = mba;
1260
1261 for (i = 0; i < 5; i++)
1262 mb->mvd[i] = 0;
1263
1264 return mb;
1265 }
1266
1267 static void
gst_rtp_h263_pay_mb_destroy(GstRtpH263PayMB * mb)1268 gst_rtp_h263_pay_mb_destroy (GstRtpH263PayMB * mb)
1269 {
1270 if (!mb)
1271 return;
1272
1273 g_free (mb);
1274 }
1275
1276 static GstFlowReturn
gst_rtp_h263_pay_push(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,GstRtpH263PayPackage * package)1277 gst_rtp_h263_pay_push (GstRtpH263Pay * rtph263pay,
1278 GstRtpH263PayContext * context, GstRtpH263PayPackage * package)
1279 {
1280
1281 /*
1282 * Splat the payload header values
1283 */
1284 guint8 *header;
1285 GstFlowReturn ret;
1286 GstRTPBuffer rtp = { NULL };
1287
1288 gst_rtp_buffer_map (package->outbuf, GST_MAP_WRITE, &rtp);
1289
1290 header = gst_rtp_buffer_get_payload (&rtp);
1291
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);
1296 break;
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);
1300 break;
1301 case GST_RTP_H263_PAYLOAD_HEADER_MODE_C:
1302 //gst_rtp_h263_pay_splat_header_C(header, package, context->piclayer);
1303 //break;
1304 default:
1305 return GST_FLOW_ERROR;
1306 }
1307
1308 /*
1309 * timestamp the buffer
1310 */
1311 GST_BUFFER_PTS (package->outbuf) = rtph263pay->first_ts;
1312
1313 gst_rtp_buffer_set_marker (&rtp, package->marker);
1314 if (package->marker)
1315 GST_DEBUG_OBJECT (rtph263pay, "Marker set!");
1316
1317 gst_rtp_buffer_unmap (&rtp);
1318
1319 /*
1320 * Copy the payload data in the buffer
1321 */
1322 GST_DEBUG_OBJECT (rtph263pay, "Copying memory");
1323 gst_buffer_copy_into (package->outbuf, rtph263pay->current_buffer,
1324 GST_BUFFER_COPY_MEMORY, package->payload_start - rtph263pay->map.data,
1325 package->payload_len);
1326 gst_rtp_copy_video_meta (rtph263pay, package->outbuf,
1327 rtph263pay->current_buffer);
1328
1329 ret =
1330 gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtph263pay),
1331 package->outbuf);
1332 GST_DEBUG_OBJECT (rtph263pay, "Package pushed, returning");
1333
1334 gst_rtp_h263_pay_package_destroy (package);
1335
1336 return ret;
1337 }
1338
1339 static GstFlowReturn
gst_rtp_h263_pay_A_fragment_push(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,guint first,guint last)1340 gst_rtp_h263_pay_A_fragment_push (GstRtpH263Pay * rtph263pay,
1341 GstRtpH263PayContext * context, guint first, guint last)
1342 {
1343
1344 GstRtpH263PayPackage *pack;
1345
1346 pack = gst_rtp_h263_pay_package_new_empty ();
1347
1348 pack->payload_start = context->gobs[first]->start;
1349 pack->sbit = context->gobs[first]->sbit;
1350 pack->ebit = context->gobs[last]->ebit;
1351 pack->payload_len =
1352 (context->gobs[last]->end - context->gobs[first]->start) + 1;
1353 pack->marker = FALSE;
1354
1355 if (last == context->no_gobs - 1) {
1356 pack->marker = TRUE;
1357 }
1358
1359 pack->gobn = context->gobs[first]->gobn;
1360 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_A;
1361 pack->outbuf =
1362 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
1363 (rtph263pay), pack->mode, 0, 0);
1364
1365 GST_DEBUG_OBJECT (rtph263pay, "Sending len:%d data to push function",
1366 pack->payload_len);
1367
1368 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1369 }
1370
1371 static GstFlowReturn
gst_rtp_h263_pay_B_fragment_push(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,GstRtpH263PayGob * gob,guint first,guint last,GstRtpH263PayBoundry * boundry)1372 gst_rtp_h263_pay_B_fragment_push (GstRtpH263Pay * rtph263pay,
1373 GstRtpH263PayContext * context, GstRtpH263PayGob * gob, guint first,
1374 guint last, GstRtpH263PayBoundry * boundry)
1375 {
1376
1377 GstRtpH263PayPackage *pack;
1378 guint mv;
1379
1380 pack = gst_rtp_h263_pay_package_new_empty ();
1381
1382 pack->payload_start = gob->macroblocks[first]->start;
1383 pack->sbit = gob->macroblocks[first]->sbit;
1384 if (first == 0) {
1385 pack->payload_start = boundry->start;
1386 pack->sbit = boundry->sbit;
1387 pack->quant = gob->quant;
1388 } else {
1389 pack->quant = gob->macroblocks[first]->quant;
1390 }
1391 pack->payload_end = gob->macroblocks[last]->end;
1392
1393 pack->ebit = gob->macroblocks[last]->ebit;
1394 pack->mba = gob->macroblocks[first]->mba;
1395 pack->gobn = gob->gobn;
1396 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_B;
1397 pack->nmvd = 0;
1398
1399 if (gob->macroblocks[first]->mb_type < 3) {
1400 if (gob->macroblocks[first]->mb_type == 2)
1401 pack->nmvd = 8;
1402 else if (gob->macroblocks[first]->mb_type < 2)
1403 pack->nmvd = 2;
1404
1405 for (mv = 0; mv < pack->nmvd; mv++)
1406 pack->mvd[mv] = gob->macroblocks[first]->mvd[mv];
1407 }
1408
1409 pack->marker = FALSE;
1410 if (last == gob->nmacroblocs - 1) {
1411 pack->ebit = 0;
1412 }
1413
1414 if ((format_props[context->piclayer->ptype_srcformat][0] - 1 == gob->gobn)
1415 && (last == gob->nmacroblocs - 1)) {
1416 pack->marker = TRUE;
1417 }
1418
1419 pack->payload_len = pack->payload_end - pack->payload_start + 1;
1420 pack->outbuf =
1421 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
1422 (rtph263pay), pack->mode, 0, 0);
1423
1424 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1425 }
1426
1427
1428 static gboolean
gst_rtp_h263_pay_mode_B_fragment(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,GstRtpH263PayGob * gob)1429 gst_rtp_h263_pay_mode_B_fragment (GstRtpH263Pay * rtph263pay,
1430 GstRtpH263PayContext * context, GstRtpH263PayGob * gob)
1431 {
1432
1433
1434 /*---------- MODE B MODE FRAGMENTATION ----------*/
1435 GstRtpH263PayMB *mac, *mac0;
1436 guint max_payload_size;
1437 GstRtpH263PayBoundry boundry;
1438 guint mb;
1439 guint8 ebit;
1440
1441 guint first = 0;
1442 guint payload_len;
1443
1444 max_payload_size =
1445 context->mtu - GST_RTP_H263_PAYLOAD_HEADER_MODE_B - GST_RTP_HEADER_LEN;
1446
1447 gst_rtp_h263_pay_boundry_init (&boundry, gob->start, gob->start, gob->sbit,
1448 0);
1449
1450 gob->macroblocks =
1451 (GstRtpH263PayMB **) g_malloc0 (sizeof (GstRtpH263PayMB *) *
1452 format_props[context->piclayer->ptype_srcformat][1]);
1453
1454 GST_LOG_OBJECT (rtph263pay, "GOB isn't PB frame, applying mode B");
1455
1456 //initializing window
1457 context->win_end = boundry.end;
1458 if (gst_rtp_h263_pay_move_window_right (rtph263pay, context, 32, boundry.ebit,
1459 &boundry.end, &gob->end) != 0) {
1460 GST_ERROR_OBJECT (rtph263pay,
1461 "The rest of the bits should be 0, exiting, because something bad happend");
1462 goto decode_error;
1463 }
1464 //The first GOB of a frame "has no" actual header - PICTURE header is his header
1465 if (gob->gobn == 0) {
1466 guint shift;
1467 GST_LOG_OBJECT (rtph263pay, "Initial GOB");
1468 shift = 43;
1469
1470 boundry.ebit =
1471 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1472 boundry.ebit, &boundry.end, &gob->end);
1473
1474 //We need PQUANT for mode B packages - so we store it
1475 gob->quant = context->window >> 27;
1476
1477 //if PCM == 1, then PSBI is present - header has 51 bits
1478 //shift for PQUANT (5) and PCM (1) = 6 bits
1479 shift = 6;
1480 if (context->cpm == 1)
1481 shift += 2;
1482 boundry.ebit =
1483 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1484 boundry.ebit, &boundry.end, &gob->end);
1485
1486 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1487
1488 //Shifting the PEI and PSPARE fields
1489 while ((context->window & 0x80000000) == 0x80000000) {
1490 boundry.ebit =
1491 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 9,
1492 boundry.ebit, &boundry.end, &gob->end);
1493 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1494 }
1495
1496 //shift the last PEI field
1497 boundry.ebit =
1498 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 1,
1499 boundry.ebit, &boundry.end, &gob->end);
1500
1501 } else {
1502 //skipping GOBs 24 header bits + 5 GQUANT
1503 guint shift = 24;
1504
1505 GST_TRACE_OBJECT (rtph263pay, "INTER GOB");
1506
1507 //if CPM == 1, there are 2 more bits in the header - GSBI header is 31 bits long
1508 if (context->cpm == 1)
1509 shift += 2;
1510
1511 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1512 boundry.ebit =
1513 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1514 boundry.ebit, &boundry.end, &gob->end);
1515
1516 //We need GQUANT for mode B packages - so we store it
1517 gob->quant = context->window >> 27;
1518
1519 shift = 5;
1520 boundry.ebit =
1521 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1522 boundry.ebit, &boundry.end, &gob->end);
1523
1524 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1525 }
1526
1527 GST_TRACE_OBJECT (rtph263pay, "GQUANT IS: %08x", gob->quant);
1528
1529 // We are on MB layer
1530
1531 mac = mac0 = gst_rtp_h263_pay_mb_new (&boundry, 0);
1532 for (mb = 0; mb < format_props[context->piclayer->ptype_srcformat][1]; mb++) {
1533
1534 GST_TRACE_OBJECT (rtph263pay,
1535 "================ START MB %d =================", mb);
1536
1537 //Find next macroblock boundaries
1538 ebit = mac->ebit;
1539 if (!(mac =
1540 gst_rtp_h263_pay_B_mbfinder (rtph263pay, context, gob, mac, mb))) {
1541
1542 GST_LOG_OBJECT (rtph263pay, "Error decoding MB - sbit: %d", 8 - ebit);
1543 GST_ERROR_OBJECT (rtph263pay, "Error decoding in GOB");
1544
1545 gst_rtp_h263_pay_mb_destroy (mac0);
1546 goto decode_error;
1547 }
1548
1549 /* Store macroblock for further processing and delete old MB if any */
1550 gst_rtp_h263_pay_mb_destroy (gob->macroblocks[mb]);
1551 gob->macroblocks[mb] = mac;
1552
1553 //If mb_type == stuffing, don't increment the mb address
1554 if (mac->mb_type == 10) {
1555 mb--;
1556 continue;
1557 } else {
1558 gob->nmacroblocs++;
1559 }
1560
1561 if (mac->end >= gob->end) {
1562 GST_LOG_OBJECT (rtph263pay, "No more MBs in this GOB");
1563 if (!mac->ebit) {
1564 mac->end--;
1565 }
1566 gob->end = mac->end;
1567 break;
1568 }
1569 GST_DEBUG_OBJECT (rtph263pay,
1570 "Found MB: mba: %d start: %p end: %p len: %d sbit: %d ebit: %d",
1571 mac->mba, mac->start, mac->end, mac->length, mac->sbit, mac->ebit);
1572 GST_TRACE_OBJECT (rtph263pay,
1573 "================ END MB %d =================", mb);
1574 }
1575 gst_rtp_h263_pay_mb_destroy (mac0);
1576
1577 mb = 0;
1578 first = 0;
1579 payload_len = boundry.end - boundry.start + 1;
1580 GST_DEBUG_OBJECT (rtph263pay,
1581 "------------------------- NEW PACKAGE ----------------------");
1582 while (mb < gob->nmacroblocs) {
1583 if (payload_len + gob->macroblocks[mb]->length < max_payload_size) {
1584
1585 //FIXME: payload_len is not the real length -> ignoring sbit/ebit
1586 payload_len += gob->macroblocks[mb]->length;
1587 mb++;
1588
1589 } else {
1590 //FIXME: we should include the last few bits of the GOB in the package - do we do that now?
1591 //GST_DEBUG_OBJECT (rtph263pay, "Pushing GOBS %d to %d because payload size is %d", first,
1592 // first == mb - 1, payload_len);
1593
1594 // FIXME: segfault if mb == 0 (first MB is larger than max_payload_size)
1595 GST_DEBUG_OBJECT (rtph263pay, "Push B mode fragment from mb %d to %d",
1596 first, mb - 1);
1597 if (gst_rtp_h263_pay_B_fragment_push (rtph263pay, context, gob, first,
1598 mb - 1, &boundry)) {
1599 GST_ERROR_OBJECT (rtph263pay, "Oooops, there was an error sending");
1600 goto decode_error;
1601 }
1602
1603 payload_len = 0;
1604 first = mb;
1605 GST_DEBUG_OBJECT (rtph263pay,
1606 "------------------------- END PACKAGE ----------------------");
1607 GST_DEBUG_OBJECT (rtph263pay,
1608 "------------------------- NEW PACKAGE ----------------------");
1609 }
1610 }
1611
1612 /* Push rest */
1613 GST_DEBUG_OBJECT (rtph263pay, "Remainder first: %d, MB: %d", first, mb);
1614 if (payload_len != 0) {
1615 GST_DEBUG_OBJECT (rtph263pay, "Push B mode fragment from mb %d to %d",
1616 first, mb - 1);
1617 if (gst_rtp_h263_pay_B_fragment_push (rtph263pay, context, gob, first,
1618 mb - 1, &boundry)) {
1619 GST_ERROR_OBJECT (rtph263pay, "Oooops, there was an error sending!");
1620 goto decode_error;
1621 }
1622 }
1623
1624 /*---------- END OF MODE B FRAGMENTATION ----------*/
1625
1626 return TRUE;
1627
1628 decode_error:
1629 return FALSE;
1630 }
1631
1632 static GstFlowReturn
gst_rtp_h263_send_entire_frame(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context)1633 gst_rtp_h263_send_entire_frame (GstRtpH263Pay * rtph263pay,
1634 GstRtpH263PayContext * context)
1635 {
1636 GstRtpH263PayPackage *pack;
1637 pack =
1638 gst_rtp_h263_pay_package_new (rtph263pay->data,
1639 rtph263pay->data + rtph263pay->available_data,
1640 rtph263pay->available_data, 0, 0, NULL, TRUE);
1641 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_A;
1642
1643 GST_DEBUG_OBJECT (rtph263pay, "Available data: %d",
1644 rtph263pay->available_data);
1645
1646 pack->outbuf =
1647 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
1648 (rtph263pay), GST_RTP_H263_PAYLOAD_HEADER_MODE_A, 0, 0);
1649
1650 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1651 }
1652
1653 static GstFlowReturn
gst_rtp_h263_pay_flush(GstRtpH263Pay * rtph263pay)1654 gst_rtp_h263_pay_flush (GstRtpH263Pay * rtph263pay)
1655 {
1656
1657 /*
1658 * FIXME: GSTUF bits are ignored right now,
1659 * - not using EBIT/SBIT payload header fields in mode A fragmentation - ffmpeg doesn't need them, but others?
1660 */
1661
1662 GstFlowReturn ret;
1663 GstRtpH263PayContext *context;
1664 gint i;
1665
1666 ret = 0;
1667 context = (GstRtpH263PayContext *) g_malloc0 (sizeof (GstRtpH263PayContext));
1668 context->mtu =
1669 rtph263pay->payload.mtu - (MTU_SECURITY_OFFSET + GST_RTP_HEADER_LEN +
1670 GST_RTP_H263_PAYLOAD_HEADER_MODE_C);
1671
1672 GST_DEBUG_OBJECT (rtph263pay, "MTU: %d", context->mtu);
1673 rtph263pay->available_data = gst_buffer_get_size (rtph263pay->current_buffer);
1674 if (rtph263pay->available_data == 0) {
1675 ret = GST_FLOW_OK;
1676 goto end;
1677 }
1678
1679 /* Get a pointer to all the data for the frame */
1680 gst_buffer_map (rtph263pay->current_buffer, &rtph263pay->map, GST_MAP_READ);
1681 rtph263pay->data = (guint8 *) rtph263pay->map.data;
1682
1683 /* Picture header */
1684 context->piclayer = (GstRtpH263PayPic *) rtph263pay->data;
1685
1686 if (context->piclayer->ptype_pictype == 0)
1687 GST_DEBUG_OBJECT (rtph263pay, "We got an I-frame");
1688 else
1689 GST_DEBUG_OBJECT (rtph263pay, "We got a P-frame");
1690
1691 context->cpm = rtph263pay->data[6] >> 7;
1692
1693 GST_DEBUG_OBJECT (rtph263pay, "CPM: %d", context->cpm);
1694
1695 GST_DEBUG_OBJECT (rtph263pay, "Payload length is: %d",
1696 rtph263pay->available_data);
1697
1698 /*
1699 * - MODE A - If normal, I and P frames, -> mode A
1700 * - GOB layer fragmentation
1701 * - MODE B - If normal, I and P frames, but GOBs > mtu
1702 * - MB layer fragmentation
1703 * - MODE C - For P frames with PB option, but GOBs > mtu
1704 * - MB layer fragmentation
1705 */
1706 if (rtph263pay->available_data + GST_RTP_H263_PAYLOAD_HEADER_MODE_A +
1707 GST_RTP_HEADER_LEN < context->mtu) {
1708 ret = gst_rtp_h263_send_entire_frame (rtph263pay, context);
1709 } else {
1710
1711 /*---------- FRAGMENTING THE FRAME BECAUSE TOO LARGE TO FIT IN MTU ----------*/
1712 GstRtpH263PayBoundry bound;
1713 gint first;
1714 guint payload_len;
1715 gboolean forcea = FALSE;
1716
1717 GST_DEBUG_OBJECT (rtph263pay, "Frame too large for MTU");
1718 /*
1719 * Let's go trough all the data and fragment it until end is reached
1720 */
1721
1722 gst_rtp_h263_pay_boundry_init (&bound, NULL, rtph263pay->data - 1, 0, 0);
1723 context->gobs =
1724 (GstRtpH263PayGob **) g_malloc0 (format_props[context->piclayer->
1725 ptype_srcformat][0] * sizeof (GstRtpH263PayGob *));
1726
1727
1728 for (i = 0; i < format_props[context->piclayer->ptype_srcformat][0]; i++) {
1729 GST_DEBUG_OBJECT (rtph263pay, "Searching for gob %d", i);
1730 if (!gst_rtp_h263_pay_gobfinder (rtph263pay, &bound)) {
1731 if (i <= 1) {
1732 GST_WARNING_OBJECT (rtph263pay,
1733 "No GOB's were found in data stream! Please enable RTP mode in encoder. Forcing mode A for now.");
1734 ret = gst_rtp_h263_send_entire_frame (rtph263pay, context);
1735 goto end;
1736 } else {
1737 /* try to send fragments corresponding to found GOBs */
1738 forcea = TRUE;
1739 break;
1740 }
1741 }
1742
1743 context->gobs[i] = gst_rtp_h263_pay_gob_new (&bound, i);
1744 //FIXME - encoders may generate an EOS gob that has to be processed
1745 GST_DEBUG_OBJECT (rtph263pay,
1746 "Gob values are: gobn: %d, start: %p len: %d ebit: %d sbit: %d", i,
1747 context->gobs[i]->start, context->gobs[i]->length,
1748 context->gobs[i]->ebit, context->gobs[i]->sbit);
1749 }
1750 /* NOTE some places may still assume this to be the max possible */
1751 context->no_gobs = i;
1752 GST_DEBUG_OBJECT (rtph263pay, "Found %d GOBS of maximum %d",
1753 context->no_gobs, format_props[context->piclayer->ptype_srcformat][0]);
1754
1755 // Make packages smaller than MTU
1756 // A mode
1757 // - if ( GOB > MTU) -> B mode || C mode
1758 // Push packages
1759
1760 first = 0;
1761 payload_len = 0;
1762 i = 0;
1763 while (i < context->no_gobs) {
1764
1765 if (context->gobs[i]->length >= context->mtu) {
1766 if (payload_len == 0) {
1767
1768 GST_DEBUG_OBJECT (rtph263pay, "GOB len > MTU");
1769 if (rtph263pay->prop_payload_mode || forcea) {
1770 payload_len = context->gobs[i]->length;
1771 goto force_a;
1772 }
1773 if (!context->piclayer->ptype_pbmode) {
1774 GST_DEBUG_OBJECT (rtph263pay, "MODE B on GOB %d needed", i);
1775 if (!gst_rtp_h263_pay_mode_B_fragment (rtph263pay, context,
1776 context->gobs[i])) {
1777 GST_ERROR_OBJECT (rtph263pay,
1778 "There was an error fragmenting in mode B");
1779 ret = GST_FLOW_ERROR;
1780 goto end;
1781 }
1782 } else {
1783 //IMPLEMENT C mode
1784 GST_ERROR_OBJECT (rtph263pay,
1785 "MODE C on GOB %d needed, but not supported yet", i);
1786 /*if(!gst_rtp_h263_pay_mode_C_fragment(rtph263pay, context, context->gobs[i])) {
1787 ret = GST_FLOW_OK;
1788 GST_ERROR("There was an error fragmenting in mode C");
1789 goto decode_error;
1790 } */
1791 goto decode_error;
1792 }
1793 decode_error:
1794 i++;
1795 first = i;
1796 continue;
1797
1798 } else {
1799 goto payload_a_push;
1800 }
1801 }
1802
1803 if (payload_len + context->gobs[i]->length < context->mtu) {
1804 GST_DEBUG_OBJECT (rtph263pay, "GOB %d fills mtu", i);
1805 payload_len += context->gobs[i]->length;
1806 i++;
1807 if (i == context->no_gobs) {
1808 GST_DEBUG_OBJECT (rtph263pay, "LAST GOB %d", i);
1809 goto payload_a_push;
1810 }
1811
1812 } else {
1813 payload_a_push:
1814 GST_DEBUG_OBJECT (rtph263pay,
1815 "Pushing GOBS %d to %d because payload size is %d", first,
1816 first == i ? i : i - 1, payload_len);
1817 gst_rtp_h263_pay_A_fragment_push (rtph263pay, context, first,
1818 first == i ? i : i - 1);
1819 payload_len = 0;
1820 first = i;
1821 }
1822 continue;
1823
1824 force_a:
1825 GST_DEBUG_OBJECT (rtph263pay,
1826 "Pushing GOBS %d to %d because payload size is %d", first, i,
1827 payload_len);
1828 gst_rtp_h263_pay_A_fragment_push (rtph263pay, context, first, i);
1829 payload_len = 0;
1830 i++;
1831 first = i;
1832 }
1833
1834
1835 }/*---------- END OF FRAGMENTATION ----------*/
1836
1837 /* Flush the input buffer data */
1838
1839 end:
1840 gst_rtp_h263_pay_context_destroy (context,
1841 context->piclayer->ptype_srcformat);
1842 gst_buffer_unmap (rtph263pay->current_buffer, &rtph263pay->map);
1843 gst_buffer_replace (&rtph263pay->current_buffer, NULL);
1844
1845 return ret;
1846 }
1847
1848 static GstFlowReturn
gst_rtp_h263_pay_handle_buffer(GstRTPBasePayload * payload,GstBuffer * buffer)1849 gst_rtp_h263_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
1850 {
1851
1852 GstRtpH263Pay *rtph263pay;
1853 GstFlowReturn ret;
1854
1855 rtph263pay = GST_RTP_H263_PAY (payload);
1856 GST_DEBUG_OBJECT (rtph263pay,
1857 "-------------------- NEW FRAME ---------------");
1858
1859 rtph263pay->first_ts = GST_BUFFER_PTS (buffer);
1860
1861 gst_buffer_replace (&rtph263pay->current_buffer, buffer);
1862 gst_buffer_unref (buffer);
1863
1864 /* we always encode and flush a full picture */
1865 ret = gst_rtp_h263_pay_flush (rtph263pay);
1866 GST_DEBUG_OBJECT (rtph263pay,
1867 "-------------------- END FRAME ---------------");
1868
1869 return ret;
1870 }
1871