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 "gstrtph263pay.h"
33 #include "gstrtputils.h"
34
35 typedef enum
36 {
37 GST_H263_FRAME_TYPE_I = 0,
38 GST_H263_FRAME_TYPE_P = 1,
39 GST_H263_FRAME_TYPE_PB = 2
40 } GstRtpH263PayFrameType;
41
42 typedef enum
43 {
44 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_RES1 = 0,
45 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_SQCIF = 1,
46 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_QCIF = 2,
47 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_CIF = 3,
48 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_4CIF = 4,
49 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_16CIF = 5,
50 GST_RTP_H263_PAYLOAD_PICTURE_FORMAT_RES2 = 6,
51 GST_H263_PAYLOAD_PICTURE_FORMAT_PLUS = 7
52 } GstRtpH263PayPictureFormat;
53
54 static const guint format_props[8][2] = { {254, 254},
55 {6, 8},
56 {9, 11},
57 {18, 22},
58 {18, 88},
59 {18, 352},
60 {254, 254},
61 {255, 255}
62 };
63
64 /*
65 * I-frame MCBPC table: code, mask, nbits, cb, cr, mb type -> 10 = undefined (because we have guint)
66 */
67 #define MCBPC_I_LEN 9
68 #define MCBPC_I_WID 6
69 static const guint32 mcbpc_I[9][6] = {
70 {0x8000, 0x8000, 1, 0, 0, 3},
71 {0x2000, 0xe000, 3, 0, 1, 3},
72 {0x4000, 0xe000, 3, 1, 0, 3},
73 {0x6000, 0xe000, 3, 1, 1, 3},
74 {0x1000, 0xf000, 4, 0, 0, 4},
75 {0x0400, 0xfc00, 6, 0, 1, 4},
76 {0x0800, 0xfc00, 6, 1, 0, 4},
77 {0x0c00, 0xfc00, 6, 1, 1, 4},
78 {0x0080, 0xff80, 9, 10, 10, 10}
79 };
80
81 /*
82 * P-frame MCBPC table: code, mask, nbits, cb, cr, mb type -> 10 = undefined (because we have guint)
83 */
84 #define MCBPC_P_LEN 21
85 #define MCBPC_P_WID 6
86 static const guint16 mcbpc_P[21][6] = {
87 {0x8000, 0x8000, 1, 0, 0, 0},
88 {0x3000, 0xf000, 4, 0, 1, 0},
89 {0x2000, 0xf000, 4, 1, 0, 0},
90 {0x1400, 0xfc00, 6, 1, 1, 0},
91 {0x6000, 0xe000, 3, 0, 0, 1},
92 {0x0e00, 0xfe00, 7, 0, 1, 1},
93 {0x0c00, 0xfe00, 7, 1, 0, 1},
94 {0x0280, 0xff80, 9, 1, 1, 1},
95 {0x4000, 0xe000, 3, 0, 0, 2},
96 {0x0a00, 0xfe00, 7, 0, 1, 2},
97 {0x0800, 0xfe00, 7, 1, 0, 2},
98 {0x0500, 0xff00, 8, 1, 1, 2},
99 {0x1800, 0xf800, 5, 0, 0, 3},
100 {0x0400, 0xff00, 8, 0, 1, 3},
101 {0x0300, 0xff00, 8, 1, 0, 3},
102 {0x0600, 0xfe00, 7, 1, 1, 3},
103 {0x1000, 0xfc00, 6, 0, 0, 4},
104 {0x0200, 0xff80, 9, 0, 1, 4},
105 {0x0180, 0xff80, 9, 1, 0, 4},
106 {0x0100, 0xff80, 9, 1, 1, 4},
107 {0x0080, 0xff80, 9, 10, 10, 10}
108 };
109
110 /*
111 * I-frame CBPY (code, mask, nbits, Y0, Y1, Y2, Y3)
112 */
113 #define CBPY_LEN 16
114 #define CBPY_WID 7
115 static const guint8 cbpy_I[16][7] = {
116 {0x30, 0xf0, 4, 0, 0, 0, 0},
117 {0x28, 0xf8, 5, 0, 0, 0, 1},
118 {0x20, 0xf8, 5, 0, 0, 1, 0},
119 {0x90, 0xf0, 4, 0, 0, 1, 1},
120 {0x18, 0xf8, 5, 0, 1, 0, 0},
121 {0x70, 0xf0, 4, 0, 1, 0, 1},
122 {0x08, 0xfc, 6, 0, 1, 1, 0},
123 {0xb0, 0xf0, 4, 0, 1, 1, 1},
124 {0x10, 0xf8, 5, 1, 0, 0, 0},
125 {0x0c, 0xfc, 6, 1, 0, 0, 1},
126 {0x50, 0xf0, 4, 1, 0, 1, 0},
127 {0xa0, 0xf0, 4, 1, 0, 1, 1},
128 {0x40, 0xf0, 4, 1, 1, 0, 0},
129 {0x80, 0xf0, 4, 1, 1, 0, 1},
130 {0x60, 0xf0, 4, 1, 1, 1, 0},
131 {0xc0, 0xc0, 2, 1, 1, 1, 1}
132 };
133
134 /*
135 * P-frame CBPY (code, mask, nbits, Y0, Y1, Y2, Y3)
136 */
137 static const guint8 cbpy_P[16][7] = {
138 {0x30, 0xf0, 4, 1, 1, 1, 1},
139 {0x28, 0xf8, 5, 1, 1, 1, 0},
140 {0x20, 0xf8, 5, 1, 1, 0, 1},
141 {0x90, 0xf0, 4, 1, 1, 0, 0},
142 {0x18, 0xf8, 5, 1, 0, 1, 1},
143 {0x70, 0xf0, 4, 1, 0, 1, 0},
144 {0x08, 0xfc, 6, 1, 0, 0, 1},
145 {0xb0, 0xf0, 4, 1, 0, 0, 0},
146 {0x10, 0xf8, 5, 0, 1, 1, 1},
147 {0x0c, 0xfc, 6, 0, 1, 1, 0},
148 {0x50, 0xf0, 4, 0, 1, 0, 1},
149 {0xa0, 0xf0, 4, 0, 1, 0, 0},
150 {0x40, 0xf0, 4, 0, 0, 1, 1},
151 {0x80, 0xf0, 4, 0, 0, 1, 0},
152 {0x60, 0xf0, 4, 0, 0, 0, 1},
153 {0xc0, 0xc0, 2, 0, 0, 0, 0}
154 };
155
156 /*
157 * Block TCOEF table (code, mask, nbits, LAST, RUN, LEVEL)
158 */
159 #define TCOEF_LEN 103
160 #define TCOEF_WID 6
161 static const guint16 tcoef[103][6] = {
162 {0x8000, 0xc000, 3, 0, 0, 1},
163 {0xf000, 0xf000, 5, 0, 0, 2},
164 {0x5400, 0xfc00, 7, 0, 0, 3},
165 {0x2e00, 0xfe00, 8, 0, 0, 4},
166 {0x1f00, 0xff00, 9, 0, 0, 5},
167 {0x1280, 0xff80, 10, 0, 0, 6},
168 {0x1200, 0xff80, 10, 0, 0, 7},
169 {0x0840, 0xffc0, 11, 0, 0, 8},
170 {0x0800, 0xffc0, 11, 0, 0, 9},
171 {0x00e0, 0xffe0, 12, 0, 0, 10}, //10
172 {0x00c0, 0xffe0, 12, 0, 0, 11},
173 {0x0400, 0xffe0, 12, 0, 0, 12},
174 {0xc000, 0xe000, 4, 0, 1, 1},
175 {0x5000, 0xfc00, 7, 0, 1, 2},
176 {0x1e00, 0xff00, 9, 0, 1, 3},
177 {0x03c0, 0xffc0, 11, 0, 1, 4},
178 {0x0420, 0xffe0, 12, 0, 1, 5},
179 {0x0500, 0xfff0, 13, 0, 1, 6},
180 {0xe000, 0xf000, 5, 0, 2, 1},
181 {0x1d00, 0xff00, 9, 0, 2, 2}, //20
182 {0x0380, 0xffc0, 11, 0, 2, 3},
183 {0x0510, 0xfff0, 13, 0, 2, 4},
184 {0x6800, 0xf800, 6, 0, 3, 1},
185 {0x1180, 0xff80, 10, 0, 3, 2},
186 {0x0340, 0xffc0, 11, 0, 3, 3},
187 {0x6000, 0xf800, 6, 0, 4, 1},
188 {0x1100, 0xff80, 10, 0, 4, 2},
189 {0x0520, 0xfff0, 13, 0, 4, 3},
190 {0x5800, 0xf800, 6, 0, 5, 1},
191 {0x0300, 0xffc0, 11, 0, 5, 2}, // 30
192 {0x0530, 0xfff0, 13, 0, 5, 3},
193 {0x4c00, 0xfc00, 7, 0, 6, 1},
194 {0x02c0, 0xffc0, 11, 0, 6, 2},
195 {0x0540, 0xfff0, 13, 0, 6, 3},
196 {0x4800, 0xfc00, 7, 0, 7, 1},
197 {0x0280, 0xffc0, 11, 0, 7, 2},
198 {0x4400, 0xfc00, 7, 0, 8, 1},
199 {0x0240, 0xffc0, 11, 0, 8, 2},
200 {0x4000, 0xfc00, 7, 0, 9, 1},
201 {0x0200, 0xffc0, 11, 0, 9, 2}, // 40
202 {0x2c00, 0xfe00, 8, 0, 10, 1},
203 {0x0550, 0xfff0, 13, 0, 10, 2},
204 {0x2a00, 0xfe00, 8, 0, 11, 1},
205 {0x2800, 0xfe00, 8, 0, 12, 1},
206 {0x1c00, 0xff00, 9, 0, 13, 1},
207 {0x1b00, 0xff00, 9, 0, 14, 1},
208 {0x1080, 0xff80, 10, 0, 15, 1},
209 {0x1000, 0xff80, 10, 0, 16, 1},
210 {0x0f80, 0xff80, 10, 0, 17, 1},
211 {0x0f00, 0xff80, 10, 0, 18, 1}, // 50
212 {0x0e80, 0xff80, 10, 0, 19, 1},
213 {0x0e00, 0xff80, 10, 0, 20, 1},
214 {0x0d80, 0xff80, 10, 0, 21, 1},
215 {0x0d00, 0xff80, 10, 0, 22, 1},
216 {0x0440, 0xffe0, 12, 0, 23, 1},
217 {0x0460, 0xffe0, 12, 0, 24, 1},
218 {0x0560, 0xfff0, 13, 0, 25, 1},
219 {0x0570, 0xfff0, 13, 0, 26, 1},
220 {0x7000, 0xf000, 5, 1, 0, 1},
221 {0x0c80, 0xff80, 10, 1, 0, 2}, // 60
222 {0x00a0, 0xffe0, 12, 1, 0, 3},
223 {0x3c00, 0xfc00, 7, 1, 1, 1},
224 {0x0080, 0xffe0, 12, 1, 1, 2},
225 {0x3800, 0xfc00, 7, 1, 2, 1},
226 {0x3400, 0xfc00, 7, 1, 3, 1},
227 {0x3000, 0xfc00, 7, 1, 4, 1},
228 {0x2600, 0xfe00, 8, 1, 5, 1},
229 {0x2400, 0xfe00, 8, 1, 6, 1},
230 {0x2200, 0xfe00, 8, 1, 7, 1},
231 {0x2000, 0xfe00, 8, 1, 8, 1}, // 70
232 {0x1a00, 0xff00, 9, 1, 9, 1},
233 {0x1900, 0xff00, 9, 1, 10, 1},
234 {0x1800, 0xff00, 9, 1, 11, 1},
235 {0x1700, 0xff00, 9, 1, 12, 1},
236 {0x1600, 0xff00, 9, 1, 13, 1},
237 {0x1500, 0xff00, 9, 1, 14, 1},
238 {0x1400, 0xff00, 9, 1, 15, 1},
239 {0x1300, 0xff00, 9, 1, 16, 1},
240 {0x0c00, 0xff80, 10, 1, 17, 1},
241 {0x0b80, 0xff80, 10, 1, 18, 1}, // 80
242 {0x0b00, 0xff80, 10, 1, 19, 1},
243 {0x0a80, 0xff80, 10, 1, 20, 1},
244 {0x0a00, 0xff80, 10, 1, 21, 1},
245 {0x0980, 0xff80, 10, 1, 22, 1},
246 {0x0900, 0xff80, 10, 1, 23, 1},
247 {0x0880, 0xff80, 10, 1, 24, 1},
248 {0x01c0, 0xffc0, 11, 1, 25, 1},
249 {0x0180, 0xffc0, 11, 1, 26, 1},
250 {0x0140, 0xffc0, 11, 1, 27, 1},
251 {0x0100, 0xffc0, 11, 1, 28, 1}, // 90
252 {0x0480, 0xffe0, 12, 1, 29, 1},
253 {0x04a0, 0xffe0, 12, 1, 30, 1},
254 {0x04c0, 0xffe0, 12, 1, 31, 1},
255 {0x04e0, 0xffe0, 12, 1, 32, 1},
256 {0x0580, 0xfff0, 13, 1, 33, 1},
257 {0x0590, 0xfff0, 13, 1, 34, 1},
258 {0x05a0, 0xfff0, 13, 1, 35, 1},
259 {0x05b0, 0xfff0, 13, 1, 36, 1},
260 {0x05c0, 0xfff0, 13, 1, 37, 1},
261 {0x05d0, 0xfff0, 13, 1, 38, 1}, // 100
262 {0x05e0, 0xfff0, 13, 1, 39, 1},
263 {0x05f0, 0xfff0, 13, 1, 40, 1},
264 {0x0600, 0xfe00, 7, 0, 0xffff, 0xffff}
265 };
266
267 /*
268 * Motion vector code table (Code, mask, nbits, vector (halfpixel, two's complement), diff (halfpixel, two's complement))
269 */
270 #define MVD_LEN 64
271 #define MVD_WID 5
272 static const guint16 mvd[64][5] = {
273 {0x0028, 0xfff8, 13, 0x0060, 0x0020},
274 {0x0038, 0xfff8, 13, 0x0061, 0x0021},
275 {0x0050, 0xfff0, 12, 0x0062, 0x0022},
276 {0x0070, 0xfff0, 12, 0x0063, 0x0023},
277 {0x0090, 0xfff0, 12, 0x0064, 0x0024},
278 {0x00b0, 0xfff0, 12, 0x0065, 0x0025},
279 {0x00d0, 0xfff0, 12, 0x0066, 0x0026},
280 {0x00f0, 0xfff0, 12, 0x0067, 0x0027},
281 {0x0120, 0xffe0, 11, 0x0068, 0x0028},
282 {0x0160, 0xffe0, 11, 0x0069, 0x0029},
283 {0x01a0, 0xffe0, 11, 0x006a, 0x002a},
284 {0x01e0, 0xffe0, 11, 0x006b, 0x002b},
285 {0x0220, 0xffe0, 11, 0x006c, 0x002c},
286 {0x0260, 0xffe0, 11, 0x006d, 0x002d},
287 {0x02a0, 0xffe0, 11, 0x006e, 0x002e},
288 {0x02e0, 0xffe0, 11, 0x006f, 0x002f},
289 {0x0320, 0xffe0, 11, 0x0070, 0x0030},
290 {0x0360, 0xffe0, 11, 0x0071, 0x0031},
291 {0x03a0, 0xffe0, 11, 0x0072, 0x0032},
292 {0x03e0, 0xffe0, 11, 0x0073, 0x0033},
293 {0x0420, 0xffe0, 11, 0x0074, 0x0034},
294 {0x0460, 0xffe0, 11, 0x0075, 0x0035},
295 {0x04c0, 0xffc0, 10, 0x0076, 0x0036},
296 {0x0540, 0xffc0, 10, 0x0077, 0x0037},
297 {0x05c0, 0xffc0, 10, 0x0078, 0x0038},
298 {0x0700, 0xff00, 8, 0x0079, 0x0039},
299 {0x0900, 0xff00, 8, 0x007a, 0x003a},
300 {0x0b00, 0xff00, 8, 0x007b, 0x003b},
301 {0x0e00, 0xfe00, 7, 0x007c, 0x003c},
302 {0x1800, 0xf800, 5, 0x007d, 0x003d},
303 {0x3000, 0xf000, 4, 0x007e, 0x003e},
304 {0x6000, 0xe000, 3, 0x007f, 0x003f},
305 {0x8000, 0x8000, 1, 0x0000, 0x0000},
306 {0x4000, 0xe000, 3, 0x0001, 0x0041},
307 {0x2000, 0xf000, 4, 0x0002, 0x0042},
308 {0x1000, 0xf800, 5, 0x0003, 0x0043},
309 {0x0c00, 0xfe00, 7, 0x0004, 0x0044},
310 {0x0a00, 0xff00, 8, 0x0005, 0x0045},
311 {0x0800, 0xff00, 8, 0x0006, 0x0046},
312 {0x0600, 0xff00, 8, 0x0007, 0x0047},
313 {0x0580, 0xffc0, 10, 0x0008, 0x0048},
314 {0x0500, 0xffc0, 10, 0x0009, 0x0049},
315 {0x0480, 0xffc0, 10, 0x000a, 0x004a},
316 {0x0440, 0xffe0, 11, 0x000b, 0x004b},
317 {0x0400, 0xffe0, 11, 0x000c, 0x004c},
318 {0x03c0, 0xffe0, 11, 0x000d, 0x004d},
319 {0x0380, 0xffe0, 11, 0x000e, 0x004e},
320 {0x0340, 0xffe0, 11, 0x000f, 0x004f},
321 {0x0300, 0xffe0, 11, 0x0010, 0x0050},
322 {0x02c0, 0xffe0, 11, 0x0011, 0x0051},
323 {0x0280, 0xffe0, 11, 0x0012, 0x0052},
324 {0x0240, 0xffe0, 11, 0x0013, 0x0053},
325 {0x0200, 0xffe0, 11, 0x0014, 0x0054},
326 {0x01c0, 0xffe0, 11, 0x0015, 0x0055},
327 {0x0180, 0xffe0, 11, 0x0016, 0x0056},
328 {0x0140, 0xffe0, 11, 0x0017, 0x0057},
329 {0x0100, 0xffe0, 11, 0x0018, 0x0058},
330 {0x00e0, 0xfff0, 12, 0x0019, 0x0059},
331 {0x00c0, 0xfff0, 12, 0x001a, 0x005a},
332 {0x00a0, 0xfff0, 12, 0x001b, 0x005b},
333 {0x0080, 0xfff0, 12, 0x001c, 0x005c},
334 {0x0060, 0xfff0, 12, 0x001d, 0x005d},
335 {0x0040, 0xfff0, 12, 0x001e, 0x005e},
336 {0x0030, 0xfff8, 13, 0x001f, 0x005f}
337 };
338
339 GST_DEBUG_CATEGORY_STATIC (rtph263pay_debug);
340 #define GST_CAT_DEFAULT (rtph263pay_debug)
341
342 #define GST_RTP_HEADER_LEN 12
343
344 enum
345 {
346 PROP_0,
347 PROP_MODE_A_ONLY
348 };
349
350 static GstStaticPadTemplate gst_rtp_h263_pay_sink_template =
351 GST_STATIC_PAD_TEMPLATE ("sink",
352 GST_PAD_SINK,
353 GST_PAD_ALWAYS,
354 GST_STATIC_CAPS ("video/x-h263, "
355 "variant = (string) \"itu\", " "h263version = (string) \"h263\"")
356 );
357
358 static GstStaticPadTemplate gst_rtp_h263_pay_src_template =
359 GST_STATIC_PAD_TEMPLATE ("src",
360 GST_PAD_SRC,
361 GST_PAD_ALWAYS,
362 GST_STATIC_CAPS ("application/x-rtp, "
363 "media = (string) \"video\", "
364 "payload = (int) " GST_RTP_PAYLOAD_H263_STRING ", "
365 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263\"; "
366 "application/x-rtp, "
367 "media = (string) \"video\", "
368 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
369 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263\"")
370 );
371
372 static void gst_rtp_h263_pay_finalize (GObject * object);
373
374 static gboolean gst_rtp_h263_pay_setcaps (GstRTPBasePayload * payload,
375 GstCaps * caps);
376 static void gst_rtp_h263_pay_set_property (GObject * object, guint prop_id,
377 const GValue * value, GParamSpec * pspec);
378 static void gst_rtp_h263_pay_get_property (GObject * object, guint prop_id,
379 GValue * value, GParamSpec * pspec);
380 static GstFlowReturn gst_rtp_h263_pay_handle_buffer (GstRTPBasePayload *
381 payload, GstBuffer * buffer);
382
383 static void gst_rtp_h263_pay_boundry_init (GstRtpH263PayBoundry * boundry,
384 guint8 * start, guint8 * end, guint8 sbit, guint8 ebit);
385 static GstRtpH263PayGob *gst_rtp_h263_pay_gob_new (GstRtpH263PayBoundry *
386 boundry, guint gobn);
387 static GstRtpH263PayMB *gst_rtp_h263_pay_mb_new (GstRtpH263PayBoundry * boundry,
388 guint mba);
389 static GstRtpH263PayPackage *gst_rtp_h263_pay_package_new_empty ();
390 static GstRtpH263PayPackage *gst_rtp_h263_pay_package_new (guint8 * start,
391 guint8 * end, guint length, guint8 sbit, guint8 ebit, GstBuffer * outbuf,
392 gboolean marker);
393
394 static void gst_rtp_h263_pay_mb_destroy (GstRtpH263PayMB * mb);
395 static void gst_rtp_h263_pay_gob_destroy (GstRtpH263PayGob * gob, guint ind);
396 static void gst_rtp_h263_pay_context_destroy (GstRtpH263PayContext * context,
397 guint ind);
398 static void gst_rtp_h263_pay_package_destroy (GstRtpH263PayPackage * pack);
399
400 #define gst_rtp_h263_pay_parent_class parent_class
401 G_DEFINE_TYPE (GstRtpH263Pay, gst_rtp_h263_pay, GST_TYPE_RTP_BASE_PAYLOAD);
402
403 static void
gst_rtp_h263_pay_class_init(GstRtpH263PayClass * klass)404 gst_rtp_h263_pay_class_init (GstRtpH263PayClass * klass)
405 {
406 GObjectClass *gobject_class;
407 GstElementClass *gstelement_class;
408 GstRTPBasePayloadClass *gstrtpbasepayload_class;
409
410 gobject_class = (GObjectClass *) klass;
411 gstelement_class = (GstElementClass *) klass;
412 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
413
414 gobject_class->finalize = gst_rtp_h263_pay_finalize;
415
416 gstrtpbasepayload_class->set_caps = gst_rtp_h263_pay_setcaps;
417 gstrtpbasepayload_class->handle_buffer = gst_rtp_h263_pay_handle_buffer;
418 gobject_class->set_property = gst_rtp_h263_pay_set_property;
419 gobject_class->get_property = gst_rtp_h263_pay_get_property;
420
421 g_object_class_install_property (G_OBJECT_CLASS (klass),
422 PROP_MODE_A_ONLY, g_param_spec_boolean ("modea-only",
423 "Fragment packets in mode A Only",
424 "Disable packetization modes B and C", DEFAULT_MODE_A,
425 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426
427 gst_element_class_add_static_pad_template (gstelement_class,
428 &gst_rtp_h263_pay_src_template);
429 gst_element_class_add_static_pad_template (gstelement_class,
430 &gst_rtp_h263_pay_sink_template);
431
432 gst_element_class_set_static_metadata (gstelement_class,
433 "RTP H263 packet payloader", "Codec/Payloader/Network/RTP",
434 "Payload-encodes H263 video in RTP packets (RFC 2190)",
435 "Neil Stratford <neils@vipadia.com>"
436 "Dejan Sakelsak <dejan.sakelsak@marand.si>");
437
438 GST_DEBUG_CATEGORY_INIT (rtph263pay_debug, "rtph263pay", 0,
439 "H263 RTP Payloader");
440 }
441
442 static void
gst_rtp_h263_pay_init(GstRtpH263Pay * rtph263pay)443 gst_rtp_h263_pay_init (GstRtpH263Pay * rtph263pay)
444 {
445 GST_RTP_BASE_PAYLOAD_PT (rtph263pay) = GST_RTP_PAYLOAD_H263;
446 rtph263pay->prop_payload_mode = DEFAULT_MODE_A;
447 }
448
449 static void
gst_rtp_h263_pay_finalize(GObject * object)450 gst_rtp_h263_pay_finalize (GObject * object)
451 {
452 GstRtpH263Pay *rtph263pay;
453
454 rtph263pay = GST_RTP_H263_PAY (object);
455
456 gst_buffer_replace (&rtph263pay->current_buffer, NULL);
457
458 G_OBJECT_CLASS (parent_class)->finalize (object);
459 }
460
461 static gboolean
gst_rtp_h263_pay_setcaps(GstRTPBasePayload * payload,GstCaps * caps)462 gst_rtp_h263_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
463 {
464 GstStructure *s = gst_caps_get_structure (caps, 0);
465 gint width, height;
466 gchar *framesize = NULL;
467 gboolean res;
468
469 if (gst_structure_has_field (s, "width") &&
470 gst_structure_has_field (s, "height")) {
471 if (!gst_structure_get_int (s, "width", &width) || width <= 0) {
472 goto invalid_dimension;
473 }
474
475 if (!gst_structure_get_int (s, "height", &height) || height <= 0) {
476 goto invalid_dimension;
477 }
478
479 framesize = g_strdup_printf ("%d-%d", width, height);
480 }
481
482 gst_rtp_base_payload_set_options (payload, "video",
483 payload->pt != GST_RTP_PAYLOAD_H263, "H263", 90000);
484
485 if (framesize != NULL) {
486 res = gst_rtp_base_payload_set_outcaps (payload,
487 "a-framesize", G_TYPE_STRING, framesize, NULL);
488 } else {
489 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
490 }
491 g_free (framesize);
492
493 return res;
494
495 /* ERRORS */
496 invalid_dimension:
497 {
498 GST_ERROR_OBJECT (payload, "Invalid width/height from caps");
499 return FALSE;
500 }
501 }
502
503 static void
gst_rtp_h263_pay_context_destroy(GstRtpH263PayContext * context,guint ind)504 gst_rtp_h263_pay_context_destroy (GstRtpH263PayContext * context, guint ind)
505 {
506 if (!context)
507 return;
508
509 if (context->gobs) {
510 guint i;
511
512 for (i = 0; i < format_props[ind][0]; i++) {
513 if (context->gobs[i]) {
514 gst_rtp_h263_pay_gob_destroy (context->gobs[i], ind);
515 }
516 }
517
518 g_free (context->gobs);
519 }
520
521 g_free (context);
522 }
523
524 static void
gst_rtp_h263_pay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)525 gst_rtp_h263_pay_set_property (GObject * object, guint prop_id,
526 const GValue * value, GParamSpec * pspec)
527 {
528 GstRtpH263Pay *rtph263pay;
529
530 rtph263pay = GST_RTP_H263_PAY (object);
531
532 switch (prop_id) {
533 case PROP_MODE_A_ONLY:
534 rtph263pay->prop_payload_mode = g_value_get_boolean (value);
535 break;
536 default:
537 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
538 break;
539 }
540 }
541
542 static void
gst_rtp_h263_pay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)543 gst_rtp_h263_pay_get_property (GObject * object, guint prop_id,
544 GValue * value, GParamSpec * pspec)
545 {
546 GstRtpH263Pay *rtph263pay;
547
548 rtph263pay = GST_RTP_H263_PAY (object);
549
550 switch (prop_id) {
551 case PROP_MODE_A_ONLY:
552 g_value_set_boolean (value, rtph263pay->prop_payload_mode);
553 break;
554 default:
555 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
556 break;
557 }
558 }
559
560 static GstRtpH263PayPackage *
gst_rtp_h263_pay_package_new_empty(void)561 gst_rtp_h263_pay_package_new_empty (void)
562 {
563 return (GstRtpH263PayPackage *) g_malloc0 (sizeof (GstRtpH263PayPackage));
564 }
565
566 static GstRtpH263PayPackage *
gst_rtp_h263_pay_package_new(guint8 * start,guint8 * end,guint length,guint8 sbit,guint8 ebit,GstBuffer * outbuf,gboolean marker)567 gst_rtp_h263_pay_package_new (guint8 * start, guint8 * end, guint length,
568 guint8 sbit, guint8 ebit, GstBuffer * outbuf, gboolean marker)
569 {
570
571 GstRtpH263PayPackage *package;
572
573 package = gst_rtp_h263_pay_package_new_empty ();
574
575 package->payload_start = start;
576 package->payload_end = end;
577 package->payload_len = length;
578 package->sbit = sbit;
579 package->ebit = ebit;
580 package->outbuf = outbuf;
581 package->marker = marker;
582
583 return package;
584 }
585
586 static void
gst_rtp_h263_pay_package_destroy(GstRtpH263PayPackage * pack)587 gst_rtp_h263_pay_package_destroy (GstRtpH263PayPackage * pack)
588 {
589 if (pack)
590 g_free (pack);
591 }
592
593 static void
gst_rtp_h263_pay_boundry_init(GstRtpH263PayBoundry * boundry,guint8 * start,guint8 * end,guint8 sbit,guint8 ebit)594 gst_rtp_h263_pay_boundry_init (GstRtpH263PayBoundry * boundry,
595 guint8 * start, guint8 * end, guint8 sbit, guint8 ebit)
596 {
597 boundry->start = start;
598 boundry->end = end;
599 boundry->sbit = sbit;
600 boundry->ebit = ebit;
601 }
602
603 static void
gst_rtp_h263_pay_splat_header_A(guint8 * header,GstRtpH263PayPackage * package,GstRtpH263PayPic * piclayer)604 gst_rtp_h263_pay_splat_header_A (guint8 * header,
605 GstRtpH263PayPackage * package, GstRtpH263PayPic * piclayer)
606 {
607
608 GstRtpH263PayAHeader *a_header;
609
610 a_header = (GstRtpH263PayAHeader *) header;
611
612 a_header->f = 0;
613 a_header->p = 0;
614 a_header->sbit = package->sbit;
615 a_header->ebit = package->ebit;
616 a_header->src = GST_H263_PICTURELAYER_PLSRC (piclayer);
617 a_header->i = GST_H263_PICTURELAYER_PLTYPE (piclayer);
618 a_header->u = GST_H263_PICTURELAYER_PLUMV (piclayer);
619 a_header->s = GST_H263_PICTURELAYER_PLSAC (piclayer);
620 a_header->a = GST_H263_PICTURELAYER_PLAP (piclayer);
621 a_header->r1 = 0;
622 a_header->r2 = 0;
623 a_header->dbq = 0;
624 a_header->trb = 0;
625 a_header->tr = 0;
626
627 }
628
629 static void
gst_rtp_h263_pay_splat_header_B(guint8 * header,GstRtpH263PayPackage * package,GstRtpH263PayPic * piclayer)630 gst_rtp_h263_pay_splat_header_B (guint8 * header,
631 GstRtpH263PayPackage * package, GstRtpH263PayPic * piclayer)
632 {
633
634 GstRtpH263PayBHeader *b_header;
635
636 b_header = (GstRtpH263PayBHeader *) header;
637
638 b_header->f = 1;
639 b_header->p = 0;
640 b_header->sbit = package->sbit;
641 b_header->ebit = package->ebit;
642 b_header->src = GST_H263_PICTURELAYER_PLSRC (piclayer);
643 b_header->quant = package->quant;
644 b_header->gobn = package->gobn;
645 b_header->mba1 = package->mba >> 6;
646 b_header->mba2 = package->mba & 0x003f;
647 b_header->r = 0;
648 b_header->i = GST_H263_PICTURELAYER_PLTYPE (piclayer);
649 b_header->u = GST_H263_PICTURELAYER_PLUMV (piclayer);
650 b_header->s = GST_H263_PICTURELAYER_PLSAC (piclayer);
651 b_header->a = GST_H263_PICTURELAYER_PLAP (piclayer);
652
653 b_header->hmv11 = 0;
654 b_header->hmv12 = 0;
655 b_header->vmv11 = 0;
656 b_header->vmv12 = 0;
657 b_header->hmv21 = 0;
658 b_header->hmv22 = 0;
659 b_header->vmv21 = 0;
660
661 if (package->nmvd > 0) {
662 //mvd[0]
663 b_header->hmv11 = (package->mvd[0] & 0x7f) >> 3;
664 b_header->hmv12 = (package->mvd[0] & 0x07);
665 //mvd[1]
666 b_header->vmv11 = (package->mvd[1] & 0x07f) >> 2;
667 b_header->vmv12 = (package->mvd[1] & 0x03);
668
669 if (package->nmvd == 8) {
670 //mvd[4]
671 b_header->hmv21 = (package->mvd[4] & 0x7f) >> 1;
672 b_header->hmv22 = (package->mvd[4] & 0x01);
673 //mvd[5]
674 b_header->vmv21 = (package->mvd[5] & 0x7f);
675 }
676 }
677
678 }
679
680 static gboolean
gst_rtp_h263_pay_gobfinder(GstRtpH263Pay * rtph263pay,GstRtpH263PayBoundry * boundry)681 gst_rtp_h263_pay_gobfinder (GstRtpH263Pay * rtph263pay,
682 GstRtpH263PayBoundry * boundry)
683 {
684 guint8 *current;
685 guint range;
686 guint i;
687
688 current = boundry->end + 1;
689 range = (rtph263pay->data - current) + rtph263pay->available_data;
690
691
692 GST_DEBUG_OBJECT (rtph263pay,
693 "Searching for next GOB, data:%p, len:%u, payload_len:%p,"
694 " current:%p, range:%u", rtph263pay->data, rtph263pay->available_data,
695 boundry->end + 1, current, range);
696
697 /* If we are past the end, stop */
698 if (current >= rtph263pay->data + rtph263pay->available_data)
699 return FALSE;
700
701 for (i = 3; i < range - 3; i++) {
702 if ((current[i] == 0x0) &&
703 (current[i + 1] == 0x0) && (current[i + 2] >> 7 == 0x1)) {
704 GST_LOG_OBJECT (rtph263pay, "GOB end found at: %p start: %p len: %u",
705 current + i - 1, boundry->end + 1,
706 (guint) (current + i - boundry->end + 2));
707 gst_rtp_h263_pay_boundry_init (boundry, boundry->end + 1, current + i - 1,
708 0, 0);
709
710 return TRUE;
711 }
712 }
713
714 GST_DEBUG_OBJECT (rtph263pay,
715 "Couldn't find any new GBSC in this frame, range:%u", range);
716
717 gst_rtp_h263_pay_boundry_init (boundry, boundry->end + 1,
718 (guint8 *) (rtph263pay->data + rtph263pay->available_data - 1), 0, 0);
719
720 return TRUE;
721 }
722
723 static GstRtpH263PayGob *
gst_rtp_h263_pay_gob_new(GstRtpH263PayBoundry * boundry,guint gobn)724 gst_rtp_h263_pay_gob_new (GstRtpH263PayBoundry * boundry, guint gobn)
725 {
726 GstRtpH263PayGob *gob;
727
728 gob = (GstRtpH263PayGob *) g_malloc0 (sizeof (GstRtpH263PayGob));
729
730 gob->start = boundry->start;
731 gob->end = boundry->end;
732 gob->length = boundry->end - boundry->start + 1;
733 gob->ebit = boundry->ebit;
734 gob->sbit = boundry->sbit;
735 gob->gobn = gobn;
736 gob->quant = 0;
737 gob->macroblocks = NULL;
738 gob->nmacroblocs = 0;
739
740 return gob;
741 }
742
743 static void
gst_rtp_h263_pay_gob_destroy(GstRtpH263PayGob * gob,guint ind)744 gst_rtp_h263_pay_gob_destroy (GstRtpH263PayGob * gob, guint ind)
745 {
746
747 if (!gob)
748 return;
749
750 if (gob->macroblocks) {
751
752 guint i;
753
754 for (i = 0; i < gob->nmacroblocs; i++) {
755 gst_rtp_h263_pay_mb_destroy (gob->macroblocks[i]);
756 }
757
758 g_free (gob->macroblocks);
759 }
760
761 g_free (gob);
762 }
763
764 /*
765 * decode MCBPC for I frames and return index in table or -1 if not found
766 */
767 static gint
gst_rtp_h263_pay_decode_mcbpc_I(GstRtpH263Pay * rtph263pay,guint32 value)768 gst_rtp_h263_pay_decode_mcbpc_I (GstRtpH263Pay * rtph263pay, guint32 value)
769 {
770
771 gint i;
772 guint16 code;
773
774 code = value >> 16;
775
776 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
777
778 for (i = 0; i < MCBPC_I_LEN; i++) {
779 if ((code & mcbpc_I[i][1]) == mcbpc_I[i][0]) {
780 return i;
781 }
782 }
783
784 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
785
786 return -1;
787 }
788
789 /*
790 * decode MCBPC for P frames and return index in table or -1 if not found
791 */
792 static gint
gst_rtp_h263_pay_decode_mcbpc_P(GstRtpH263Pay * rtph263pay,guint32 value)793 gst_rtp_h263_pay_decode_mcbpc_P (GstRtpH263Pay * rtph263pay, guint32 value)
794 {
795
796 gint i;
797 guint16 code;
798
799 code = value >> 16;
800
801 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
802
803 for (i = 0; i < MCBPC_P_LEN; i++) {
804 if ((code & mcbpc_P[i][1]) == mcbpc_P[i][0]) {
805 return i;
806 }
807 }
808
809 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
810
811 return -1;
812 }
813
814 /*
815 * decode CBPY and return index in table or -1 if not found
816 */
817 static gint
gst_rtp_h263_pay_decode_cbpy(GstRtpH263Pay * rtph263pay,guint32 value,const guint8 cbpy_table[16][7])818 gst_rtp_h263_pay_decode_cbpy (GstRtpH263Pay * rtph263pay, guint32 value,
819 const guint8 cbpy_table[16][7])
820 {
821
822 gint i;
823 guint8 code;
824
825 code = value >> 24;
826
827 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
828
829 for (i = 0; i < CBPY_LEN; i++) {
830 if ((code & cbpy_table[i][1]) == cbpy_table[i][0]) {
831 return i;
832 }
833 }
834
835 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
836
837 return -1;
838 }
839
840 /*
841 * decode MVD and return index in table or -1 if not found
842 */
843 static gint
gst_rtp_h263_pay_decode_mvd(GstRtpH263Pay * rtph263pay,guint32 value)844 gst_rtp_h263_pay_decode_mvd (GstRtpH263Pay * rtph263pay, guint32 value)
845 {
846
847 gint i;
848 guint16 code;
849
850 code = value >> 16;
851
852 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
853
854 for (i = 0; i < MVD_LEN; i++) {
855 if ((code & mvd[i][1]) == mvd[i][0]) {
856 return i;
857 }
858 }
859
860 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
861
862 return -1;
863 }
864
865 /*
866 * decode TCOEF and return index in table or -1 if not found
867 */
868 static gint
gst_rtp_h263_pay_decode_tcoef(GstRtpH263Pay * rtph263pay,guint32 value)869 gst_rtp_h263_pay_decode_tcoef (GstRtpH263Pay * rtph263pay, guint32 value)
870 {
871
872 gint i;
873 guint16 code;
874
875 code = value >> 16;
876
877 GST_TRACE_OBJECT (rtph263pay, "value:0x%08x, code:0x%04x", value, code);
878
879 for (i = 0; i < TCOEF_LEN; i++) {
880 if ((code & tcoef[i][1]) == tcoef[i][0]) {
881 GST_TRACE_OBJECT (rtph263pay, "tcoef is %d", i);
882 return i;
883 }
884 }
885
886 GST_WARNING_OBJECT (rtph263pay, "Couldn't find code, returning -1");
887
888 return -1;
889 }
890
891 /*
892 * the 32-bit register is like a window that we move right for "move_bits" to get the next v "data" h263 field
893 * "rest_bits" tells how many bits in the "data" byte address are still not used
894 */
895
896 static gint
gst_rtp_h263_pay_move_window_right(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,guint n,guint rest_bits,guint8 ** orig_data,guint8 ** data_end)897 gst_rtp_h263_pay_move_window_right (GstRtpH263Pay * rtph263pay,
898 GstRtpH263PayContext * context, guint n, guint rest_bits,
899 guint8 ** orig_data, guint8 ** data_end)
900 {
901
902 GST_TRACE_OBJECT (rtph263pay,
903 "Moving window: 0x%08x from: %p for %d bits, rest_bits: %d, data_end %p",
904 context->window, context->win_end, n, rest_bits, *data_end);
905
906 if (n == 0)
907 return rest_bits;
908
909 while (n != 0 || context->win_end == ((*data_end) + 1)) {
910 guint8 b = context->win_end <= *data_end ? *context->win_end : 0;
911 if (rest_bits == 0) {
912 if (n > 8) {
913 context->window = (context->window << 8) | b;
914 n -= 8;
915 } else {
916 context->window = (context->window << n) | (b >> (8 - n));
917 rest_bits = 8 - n;
918 if (rest_bits == 0)
919 context->win_end++;
920 break;
921 }
922 } else {
923 if (n > rest_bits) {
924 context->window = (context->window << rest_bits) |
925 (b & (((guint) pow (2.0, (double) rest_bits)) - 1));
926 n -= rest_bits;
927 rest_bits = 0;
928 } else {
929 context->window = (context->window << n) |
930 ((b & (((guint) pow (2.0, (double) rest_bits)) - 1)) >>
931 (rest_bits - n));
932 rest_bits -= n;
933 if (rest_bits == 0)
934 context->win_end++;
935 break;
936 }
937 }
938
939 context->win_end++;
940 }
941
942 *orig_data = context->win_end - 4;
943
944 GST_TRACE_OBJECT (rtph263pay,
945 "Window moved to %p with value: 0x%08x and orig_data: %p rest_bits: %d",
946 context->win_end, context->window, *orig_data, rest_bits);
947 return rest_bits;
948 }
949
950 /*
951 * Find the start of the next MB (end of the current MB)
952 * returns the number of excess bits and stores the end of the MB in end
953 * data must be placed on first MB byte
954 */
955 static GstRtpH263PayMB *
gst_rtp_h263_pay_B_mbfinder(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,GstRtpH263PayGob * gob,GstRtpH263PayMB * macroblock,guint mba)956 gst_rtp_h263_pay_B_mbfinder (GstRtpH263Pay * rtph263pay,
957 GstRtpH263PayContext * context, GstRtpH263PayGob * gob,
958 GstRtpH263PayMB * macroblock, guint mba)
959 {
960 guint mb_type_index;
961 guint cbpy_type_index;
962 guint tcoef_type_index;
963 GstRtpH263PayMB *mac;
964 GstRtpH263PayBoundry boundry;
965
966 gst_rtp_h263_pay_boundry_init (&boundry, macroblock->end,
967 macroblock->end, 8 - macroblock->ebit, macroblock->ebit);
968 mac = gst_rtp_h263_pay_mb_new (&boundry, mba);
969
970 if (mac->sbit == 8) {
971 mac->start++;
972 // mac->end++;
973 mac->sbit = 0;
974 }
975
976 GST_LOG_OBJECT (rtph263pay,
977 "current_pos:%p, end:%p, rest_bits:%d, window:0x%08x", mac->start,
978 mac->end, macroblock->ebit, context->window);
979
980 if (context->piclayer->ptype_pictype == 0) {
981 //We have an I frame
982 gint i;
983 guint last;
984 guint ind;
985
986 //Step 2 decode MCBPC I
987 mb_type_index =
988 gst_rtp_h263_pay_decode_mcbpc_I (rtph263pay, context->window);
989
990 GST_TRACE_OBJECT (rtph263pay, "MCBPC index: %d", mb_type_index);
991 if (mb_type_index == -1) {
992 GST_ERROR_OBJECT (rtph263pay, "MB index shouldn't be -1 in window: %08x",
993 context->window);
994 goto beach;
995 }
996
997 mac->ebit =
998 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
999 mcbpc_I[mb_type_index][2], mac->ebit, &mac->end, &gob->end);
1000
1001 mac->mb_type = mcbpc_I[mb_type_index][5];
1002
1003 if (mb_type_index == 8) {
1004 GST_TRACE_OBJECT (rtph263pay, "Stuffing skipping rest of MB header");
1005 return mac;
1006 }
1007 //Step 3 decode CBPY I
1008 cbpy_type_index =
1009 gst_rtp_h263_pay_decode_cbpy (rtph263pay, context->window, cbpy_I);
1010
1011 GST_TRACE_OBJECT (rtph263pay, "CBPY index: %d", cbpy_type_index);
1012 if (cbpy_type_index == -1) {
1013 GST_ERROR_OBJECT (rtph263pay,
1014 "CBPY index shouldn't be -1 in window: %08x", context->window);
1015 goto beach;
1016 }
1017
1018 mac->ebit =
1019 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1020 cbpy_I[cbpy_type_index][2], mac->ebit, &mac->end, &gob->end);
1021
1022 //Step 4 decode rest of MB
1023 //MB type 1 and 4 have DQUANT - we store it for packaging purposes
1024 if (mcbpc_I[mb_type_index][5] == 4) {
1025 GST_TRACE_OBJECT (rtph263pay, "Shifting DQUANT");
1026
1027 mac->quant = (context->window >> 30);
1028
1029 mac->ebit =
1030 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 2, mac->ebit,
1031 &mac->end, &gob->end);
1032 }
1033 //Step 5 go trough the blocks - decode DC and TCOEF
1034 last = 0;
1035 for (i = 0; i < N_BLOCKS; i++) {
1036
1037 GST_TRACE_OBJECT (rtph263pay, "Decoding INTRADC and TCOEF, i:%d", i);
1038 mac->ebit =
1039 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 8, mac->ebit,
1040 &mac->end, &gob->end);
1041
1042 if (i > 3) {
1043 ind = mcbpc_I[mb_type_index][i - 1];
1044 } else {
1045 ind = cbpy_I[cbpy_type_index][i + 3];
1046 }
1047
1048 if (ind == 1) {
1049 while (last == 0) {
1050 tcoef_type_index =
1051 gst_rtp_h263_pay_decode_tcoef (rtph263pay, context->window);
1052
1053 GST_TRACE_OBJECT (rtph263pay, "TCOEF index: %d", tcoef_type_index);
1054 if (tcoef_type_index == -1) {
1055 GST_ERROR_OBJECT (rtph263pay,
1056 "TCOEF index shouldn't be -1 in window: %08x", context->window);
1057 goto beach;
1058 }
1059 mac->ebit =
1060 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1061 tcoef[tcoef_type_index][2], mac->ebit, &mac->end, &gob->end);
1062
1063 last = tcoef[tcoef_type_index][3];
1064 if (tcoef_type_index == 102) {
1065 if ((context->window & 0x80000000) == 0x80000000)
1066 last = 1;
1067 else
1068 last = 0;
1069
1070 mac->ebit =
1071 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 15,
1072 mac->ebit, &mac->end, &gob->end);
1073 }
1074 }
1075 last = 0;
1076 }
1077 }
1078
1079 } else {
1080 //We have a P frame
1081 guint i;
1082 guint last;
1083 guint ind;
1084
1085 //Step 1 check COD
1086 GST_TRACE_OBJECT (rtph263pay, "Checking for COD");
1087 if ((context->window & 0x80000000) == 0x80000000) {
1088 //The MB is not coded
1089 mac->ebit =
1090 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 1, mac->ebit,
1091 &mac->end, &gob->end);
1092 GST_TRACE_OBJECT (rtph263pay, "COOOOOOOOOOOD = 1");
1093
1094 return mac;
1095 } else {
1096 //The MB is coded
1097 mac->ebit =
1098 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 1, mac->ebit,
1099 &mac->end, &gob->end);
1100 }
1101
1102 //Step 2 decode MCBPC P
1103 mb_type_index =
1104 gst_rtp_h263_pay_decode_mcbpc_P (rtph263pay, context->window);
1105
1106 GST_TRACE_OBJECT (rtph263pay, "MCBPC index: %d", mb_type_index);
1107 if (mb_type_index == -1) {
1108 GST_ERROR_OBJECT (rtph263pay, "MB index shouldn't be -1 in window: %08x",
1109 context->window);
1110 goto beach;
1111 }
1112 mac->ebit =
1113 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1114 mcbpc_P[mb_type_index][2], mac->ebit, &mac->end, &gob->end);
1115
1116 mac->mb_type = mcbpc_P[mb_type_index][5];
1117
1118 if (mb_type_index == 20) {
1119 GST_TRACE_OBJECT (rtph263pay, "Stuffing skipping rest of MB header");
1120 return mac;
1121 }
1122 //Step 3 decode CBPY P
1123 cbpy_type_index =
1124 gst_rtp_h263_pay_decode_cbpy (rtph263pay, context->window, cbpy_P);
1125
1126 GST_TRACE_OBJECT (rtph263pay, "CBPY index: %d", cbpy_type_index);
1127 if (cbpy_type_index == -1) {
1128 GST_ERROR_OBJECT (rtph263pay,
1129 "CBPY index shouldn't be -1 in window: %08x", context->window);
1130 goto beach;
1131 }
1132 mac->ebit =
1133 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1134 cbpy_P[cbpy_type_index][2], mac->ebit, &mac->end, &gob->end);
1135
1136 //MB type 1 and 4 have DQUANT - we add it to MB object and jump over
1137 if (mcbpc_P[mb_type_index][5] == 4 || mcbpc_P[mb_type_index][5] == 1) {
1138 GST_TRACE_OBJECT (rtph263pay, "Shifting DQUANT");
1139
1140 mac->quant = context->window >> 30;
1141
1142 mac->ebit =
1143 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 2, mac->ebit,
1144 &mac->end, &gob->end);
1145 }
1146 //MB types < 3 have MVD1-4
1147 if (mcbpc_P[mb_type_index][5] < 3) {
1148
1149 guint nmvd;
1150 gint j;
1151
1152 nmvd = 2;
1153 if (mcbpc_P[mb_type_index][5] == 2)
1154 nmvd = 8;
1155
1156 for (j = 0; j < nmvd; j++) {
1157 guint mvd_type;
1158
1159 mvd_type = gst_rtp_h263_pay_decode_mvd (rtph263pay, context->window);
1160
1161 if (mvd_type == -1) {
1162 GST_ERROR_OBJECT (rtph263pay,
1163 "MVD1-4 index shouldn't be -1 in window: %08x", context->window);
1164 goto beach;
1165 }
1166 //set the MB mvd values
1167 mac->mvd[j] = mvd[mvd_type][3];
1168
1169 mac->ebit =
1170 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1171 mvd[mvd_type][2], mac->ebit, &mac->end, &gob->end);
1172 }
1173
1174
1175 }
1176 //Step 5 go trough the blocks - decode DC and TCOEF
1177 last = 0;
1178 for (i = 0; i < N_BLOCKS; i++) {
1179
1180 //if MB type 3 or 4 then INTRADC coef are present in blocks
1181 if (mcbpc_P[mb_type_index][5] > 2) {
1182 GST_TRACE_OBJECT (rtph263pay, "INTRADC coef: %d", i);
1183 mac->ebit =
1184 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 8,
1185 mac->ebit, &mac->end, &gob->end);
1186 } else {
1187 GST_TRACE_OBJECT (rtph263pay, "INTRADC coef is not present");
1188 }
1189
1190 //check if the block has TCOEF
1191 if (i > 3) {
1192 ind = mcbpc_P[mb_type_index][i - 1];
1193 } else {
1194 if (mcbpc_P[mb_type_index][5] > 2) {
1195 ind = cbpy_I[cbpy_type_index][i + 3];
1196 } else {
1197 ind = cbpy_P[cbpy_type_index][i + 3];
1198 }
1199 }
1200
1201 if (ind == 1) {
1202 while (last == 0) {
1203 tcoef_type_index =
1204 gst_rtp_h263_pay_decode_tcoef (rtph263pay, context->window);
1205
1206 GST_TRACE_OBJECT (rtph263pay, "TCOEF index: %d", tcoef_type_index);
1207 if (tcoef_type_index == -1) {
1208 GST_ERROR_OBJECT (rtph263pay,
1209 "TCOEF index shouldn't be -1 in window: %08x", context->window);
1210 goto beach;
1211 }
1212
1213 mac->ebit =
1214 gst_rtp_h263_pay_move_window_right (rtph263pay, context,
1215 tcoef[tcoef_type_index][2], mac->ebit, &mac->end, &gob->end);
1216
1217 last = tcoef[tcoef_type_index][3];
1218 if (tcoef_type_index == 102) {
1219 if ((context->window & 0x80000000) == 0x80000000)
1220 last = 1;
1221 else
1222 last = 0;
1223
1224 mac->ebit =
1225 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 15,
1226 mac->ebit, &mac->end, &gob->end);
1227 }
1228 }
1229 last = 0;
1230 }
1231 }
1232 }
1233
1234 mac->length = mac->end - mac->start + 1;
1235
1236 return mac;
1237
1238 beach:
1239 gst_rtp_h263_pay_mb_destroy (mac);
1240 return NULL;
1241 }
1242
1243 static GstRtpH263PayMB *
gst_rtp_h263_pay_mb_new(GstRtpH263PayBoundry * boundry,guint mba)1244 gst_rtp_h263_pay_mb_new (GstRtpH263PayBoundry * boundry, guint mba)
1245 {
1246 GstRtpH263PayMB *mb;
1247 gint i;
1248
1249 mb = (GstRtpH263PayMB *) g_malloc0 (sizeof (GstRtpH263PayMB));
1250
1251 mb->start = boundry->start;
1252 mb->end = boundry->end;
1253 mb->length = boundry->end - boundry->start + 1;
1254 mb->sbit = boundry->sbit;
1255 mb->ebit = boundry->ebit;
1256 mb->mba = mba;
1257
1258 for (i = 0; i < 5; i++)
1259 mb->mvd[i] = 0;
1260
1261 return mb;
1262 }
1263
1264 static void
gst_rtp_h263_pay_mb_destroy(GstRtpH263PayMB * mb)1265 gst_rtp_h263_pay_mb_destroy (GstRtpH263PayMB * mb)
1266 {
1267 if (!mb)
1268 return;
1269
1270 g_free (mb);
1271 }
1272
1273 static GstFlowReturn
gst_rtp_h263_pay_push(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,GstRtpH263PayPackage * package)1274 gst_rtp_h263_pay_push (GstRtpH263Pay * rtph263pay,
1275 GstRtpH263PayContext * context, GstRtpH263PayPackage * package)
1276 {
1277
1278 /*
1279 * Splat the payload header values
1280 */
1281 guint8 *header;
1282 GstFlowReturn ret;
1283 GstRTPBuffer rtp = { NULL };
1284
1285 gst_rtp_buffer_map (package->outbuf, GST_MAP_WRITE, &rtp);
1286
1287 header = gst_rtp_buffer_get_payload (&rtp);
1288
1289 switch (package->mode) {
1290 case GST_RTP_H263_PAYLOAD_HEADER_MODE_A:
1291 GST_LOG_OBJECT (rtph263pay, "Pushing A packet");
1292 gst_rtp_h263_pay_splat_header_A (header, package, context->piclayer);
1293 break;
1294 case GST_RTP_H263_PAYLOAD_HEADER_MODE_B:
1295 GST_LOG_OBJECT (rtph263pay, "Pushing B packet");
1296 gst_rtp_h263_pay_splat_header_B (header, package, context->piclayer);
1297 break;
1298 case GST_RTP_H263_PAYLOAD_HEADER_MODE_C:
1299 //gst_rtp_h263_pay_splat_header_C(header, package, context->piclayer);
1300 //break;
1301 default:
1302 return GST_FLOW_ERROR;
1303 }
1304
1305 /*
1306 * timestamp the buffer
1307 */
1308 GST_BUFFER_PTS (package->outbuf) = rtph263pay->first_ts;
1309
1310 gst_rtp_buffer_set_marker (&rtp, package->marker);
1311 if (package->marker)
1312 GST_DEBUG_OBJECT (rtph263pay, "Marker set!");
1313
1314 gst_rtp_buffer_unmap (&rtp);
1315
1316 /*
1317 * Copy the payload data in the buffer
1318 */
1319 GST_DEBUG_OBJECT (rtph263pay, "Copying memory");
1320 gst_buffer_copy_into (package->outbuf, rtph263pay->current_buffer,
1321 GST_BUFFER_COPY_MEMORY, package->payload_start - rtph263pay->map.data,
1322 package->payload_len);
1323 gst_rtp_copy_video_meta (rtph263pay, package->outbuf,
1324 rtph263pay->current_buffer);
1325
1326 ret =
1327 gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtph263pay),
1328 package->outbuf);
1329 GST_DEBUG_OBJECT (rtph263pay, "Package pushed, returning");
1330
1331 gst_rtp_h263_pay_package_destroy (package);
1332
1333 return ret;
1334 }
1335
1336 static GstFlowReturn
gst_rtp_h263_pay_A_fragment_push(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,guint first,guint last)1337 gst_rtp_h263_pay_A_fragment_push (GstRtpH263Pay * rtph263pay,
1338 GstRtpH263PayContext * context, guint first, guint last)
1339 {
1340
1341 GstRtpH263PayPackage *pack;
1342
1343 pack = gst_rtp_h263_pay_package_new_empty ();
1344
1345 pack->payload_start = context->gobs[first]->start;
1346 pack->sbit = context->gobs[first]->sbit;
1347 pack->ebit = context->gobs[last]->ebit;
1348 pack->payload_len =
1349 (context->gobs[last]->end - context->gobs[first]->start) + 1;
1350 pack->marker = FALSE;
1351
1352 if (last == context->no_gobs - 1) {
1353 pack->marker = TRUE;
1354 }
1355
1356 pack->gobn = context->gobs[first]->gobn;
1357 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_A;
1358 pack->outbuf = gst_rtp_buffer_new_allocate (pack->mode, 0, 0);
1359
1360 GST_DEBUG_OBJECT (rtph263pay, "Sending len:%d data to push function",
1361 pack->payload_len);
1362
1363 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1364 }
1365
1366 static GstFlowReturn
gst_rtp_h263_pay_B_fragment_push(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,GstRtpH263PayGob * gob,guint first,guint last,GstRtpH263PayBoundry * boundry)1367 gst_rtp_h263_pay_B_fragment_push (GstRtpH263Pay * rtph263pay,
1368 GstRtpH263PayContext * context, GstRtpH263PayGob * gob, guint first,
1369 guint last, GstRtpH263PayBoundry * boundry)
1370 {
1371
1372 GstRtpH263PayPackage *pack;
1373 guint mv;
1374
1375 pack = gst_rtp_h263_pay_package_new_empty ();
1376
1377 pack->payload_start = gob->macroblocks[first]->start;
1378 pack->sbit = gob->macroblocks[first]->sbit;
1379 if (first == 0) {
1380 pack->payload_start = boundry->start;
1381 pack->sbit = boundry->sbit;
1382 pack->quant = gob->quant;
1383 } else {
1384 pack->quant = gob->macroblocks[first]->quant;
1385 }
1386 pack->payload_end = gob->macroblocks[last]->end;
1387
1388 pack->ebit = gob->macroblocks[last]->ebit;
1389 pack->mba = gob->macroblocks[first]->mba;
1390 pack->gobn = gob->gobn;
1391 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_B;
1392 pack->nmvd = 0;
1393
1394 if (gob->macroblocks[first]->mb_type < 3) {
1395 if (gob->macroblocks[first]->mb_type == 2)
1396 pack->nmvd = 8;
1397 else if (gob->macroblocks[first]->mb_type < 2)
1398 pack->nmvd = 2;
1399
1400 for (mv = 0; mv < pack->nmvd; mv++)
1401 pack->mvd[mv] = gob->macroblocks[first]->mvd[mv];
1402 }
1403
1404 pack->marker = FALSE;
1405 if (last == gob->nmacroblocs - 1) {
1406 pack->ebit = 0;
1407 }
1408
1409 if ((format_props[context->piclayer->ptype_srcformat][0] - 1 == gob->gobn)
1410 && (last == gob->nmacroblocs - 1)) {
1411 pack->marker = TRUE;
1412 }
1413
1414 pack->payload_len = pack->payload_end - pack->payload_start + 1;
1415 pack->outbuf = gst_rtp_buffer_new_allocate (pack->mode, 0, 0);
1416
1417 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1418 }
1419
1420
1421 static gboolean
gst_rtp_h263_pay_mode_B_fragment(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context,GstRtpH263PayGob * gob)1422 gst_rtp_h263_pay_mode_B_fragment (GstRtpH263Pay * rtph263pay,
1423 GstRtpH263PayContext * context, GstRtpH263PayGob * gob)
1424 {
1425
1426
1427 /*---------- MODE B MODE FRAGMENTATION ----------*/
1428 GstRtpH263PayMB *mac, *mac0;
1429 guint max_payload_size;
1430 GstRtpH263PayBoundry boundry;
1431 guint mb;
1432 guint8 ebit;
1433
1434 guint first = 0;
1435 guint payload_len;
1436
1437 max_payload_size =
1438 context->mtu - GST_RTP_H263_PAYLOAD_HEADER_MODE_B - GST_RTP_HEADER_LEN;
1439
1440 gst_rtp_h263_pay_boundry_init (&boundry, gob->start, gob->start, gob->sbit,
1441 0);
1442
1443 gob->macroblocks =
1444 (GstRtpH263PayMB **) g_malloc0 (sizeof (GstRtpH263PayMB *) *
1445 format_props[context->piclayer->ptype_srcformat][1]);
1446
1447 GST_LOG_OBJECT (rtph263pay, "GOB isn't PB frame, applying mode B");
1448
1449 //initializing window
1450 context->win_end = boundry.end;
1451 if (gst_rtp_h263_pay_move_window_right (rtph263pay, context, 32, boundry.ebit,
1452 &boundry.end, &gob->end) != 0) {
1453 GST_ERROR_OBJECT (rtph263pay,
1454 "The rest of the bits should be 0, exiting, because something bad happend");
1455 goto decode_error;
1456 }
1457 //The first GOB of a frame "has no" actual header - PICTURE header is his header
1458 if (gob->gobn == 0) {
1459 guint shift;
1460 GST_LOG_OBJECT (rtph263pay, "Initial GOB");
1461 shift = 43;
1462
1463 boundry.ebit =
1464 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1465 boundry.ebit, &boundry.end, &gob->end);
1466
1467 //We need PQUANT for mode B packages - so we store it
1468 gob->quant = context->window >> 27;
1469
1470 //if PCM == 1, then PSBI is present - header has 51 bits
1471 //shift for PQUANT (5) and PCM (1) = 6 bits
1472 shift = 6;
1473 if (context->cpm == 1)
1474 shift += 2;
1475 boundry.ebit =
1476 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1477 boundry.ebit, &boundry.end, &gob->end);
1478
1479 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1480
1481 //Shifting the PEI and PSPARE fields
1482 while ((context->window & 0x80000000) == 0x80000000) {
1483 boundry.ebit =
1484 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 9,
1485 boundry.ebit, &boundry.end, &gob->end);
1486 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1487 }
1488
1489 //shift the last PEI field
1490 boundry.ebit =
1491 gst_rtp_h263_pay_move_window_right (rtph263pay, context, 1,
1492 boundry.ebit, &boundry.end, &gob->end);
1493
1494 } else {
1495 //skipping GOBs 24 header bits + 5 GQUANT
1496 guint shift = 24;
1497
1498 GST_TRACE_OBJECT (rtph263pay, "INTER GOB");
1499
1500 //if CPM == 1, there are 2 more bits in the header - GSBI header is 31 bits long
1501 if (context->cpm == 1)
1502 shift += 2;
1503
1504 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1505 boundry.ebit =
1506 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1507 boundry.ebit, &boundry.end, &gob->end);
1508
1509 //We need GQUANT for mode B packages - so we store it
1510 gob->quant = context->window >> 27;
1511
1512 shift = 5;
1513 boundry.ebit =
1514 gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1515 boundry.ebit, &boundry.end, &gob->end);
1516
1517 GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1518 }
1519
1520 GST_TRACE_OBJECT (rtph263pay, "GQUANT IS: %08x", gob->quant);
1521
1522 // We are on MB layer
1523
1524 mac = mac0 = gst_rtp_h263_pay_mb_new (&boundry, 0);
1525 for (mb = 0; mb < format_props[context->piclayer->ptype_srcformat][1]; mb++) {
1526
1527 GST_TRACE_OBJECT (rtph263pay,
1528 "================ START MB %d =================", mb);
1529
1530 //Find next macroblock boundaries
1531 ebit = mac->ebit;
1532 if (!(mac =
1533 gst_rtp_h263_pay_B_mbfinder (rtph263pay, context, gob, mac, mb))) {
1534
1535 GST_LOG_OBJECT (rtph263pay, "Error decoding MB - sbit: %d", 8 - ebit);
1536 GST_ERROR_OBJECT (rtph263pay, "Error decoding in GOB");
1537
1538 gst_rtp_h263_pay_mb_destroy (mac0);
1539 goto decode_error;
1540 }
1541
1542 /* Store macroblock for further processing and delete old MB if any */
1543 gst_rtp_h263_pay_mb_destroy (gob->macroblocks[mb]);
1544 gob->macroblocks[mb] = mac;
1545
1546 //If mb_type == stuffing, don't increment the mb address
1547 if (mac->mb_type == 10) {
1548 mb--;
1549 continue;
1550 } else {
1551 gob->nmacroblocs++;
1552 }
1553
1554 if (mac->end >= gob->end) {
1555 GST_LOG_OBJECT (rtph263pay, "No more MBs in this GOB");
1556 if (!mac->ebit) {
1557 mac->end--;
1558 }
1559 gob->end = mac->end;
1560 break;
1561 }
1562 GST_DEBUG_OBJECT (rtph263pay,
1563 "Found MB: mba: %d start: %p end: %p len: %d sbit: %d ebit: %d",
1564 mac->mba, mac->start, mac->end, mac->length, mac->sbit, mac->ebit);
1565 GST_TRACE_OBJECT (rtph263pay,
1566 "================ END MB %d =================", mb);
1567 }
1568 gst_rtp_h263_pay_mb_destroy (mac0);
1569
1570 mb = 0;
1571 first = 0;
1572 payload_len = boundry.end - boundry.start + 1;
1573 GST_DEBUG_OBJECT (rtph263pay,
1574 "------------------------- NEW PACKAGE ----------------------");
1575 while (mb < gob->nmacroblocs) {
1576 if (payload_len + gob->macroblocks[mb]->length < max_payload_size) {
1577
1578 //FIXME: payload_len is not the real length -> ignoring sbit/ebit
1579 payload_len += gob->macroblocks[mb]->length;
1580 mb++;
1581
1582 } else {
1583 //FIXME: we should include the last few bits of the GOB in the package - do we do that now?
1584 //GST_DEBUG_OBJECT (rtph263pay, "Pushing GOBS %d to %d because payload size is %d", first,
1585 // first == mb - 1, payload_len);
1586
1587 // FIXME: segfault if mb == 0 (first MB is larger than max_payload_size)
1588 GST_DEBUG_OBJECT (rtph263pay, "Push B mode fragment from mb %d to %d",
1589 first, mb - 1);
1590 if (gst_rtp_h263_pay_B_fragment_push (rtph263pay, context, gob, first,
1591 mb - 1, &boundry)) {
1592 GST_ERROR_OBJECT (rtph263pay, "Oooops, there was an error sending");
1593 goto decode_error;
1594 }
1595
1596 payload_len = 0;
1597 first = mb;
1598 GST_DEBUG_OBJECT (rtph263pay,
1599 "------------------------- END PACKAGE ----------------------");
1600 GST_DEBUG_OBJECT (rtph263pay,
1601 "------------------------- NEW PACKAGE ----------------------");
1602 }
1603 }
1604
1605 /* Push rest */
1606 GST_DEBUG_OBJECT (rtph263pay, "Remainder first: %d, MB: %d", first, mb);
1607 if (payload_len != 0) {
1608 GST_DEBUG_OBJECT (rtph263pay, "Push B mode fragment from mb %d to %d",
1609 first, mb - 1);
1610 if (gst_rtp_h263_pay_B_fragment_push (rtph263pay, context, gob, first,
1611 mb - 1, &boundry)) {
1612 GST_ERROR_OBJECT (rtph263pay, "Oooops, there was an error sending!");
1613 goto decode_error;
1614 }
1615 }
1616
1617 /*---------- END OF MODE B FRAGMENTATION ----------*/
1618
1619 return TRUE;
1620
1621 decode_error:
1622 return FALSE;
1623 }
1624
1625 static GstFlowReturn
gst_rtp_h263_send_entire_frame(GstRtpH263Pay * rtph263pay,GstRtpH263PayContext * context)1626 gst_rtp_h263_send_entire_frame (GstRtpH263Pay * rtph263pay,
1627 GstRtpH263PayContext * context)
1628 {
1629 GstRtpH263PayPackage *pack;
1630 pack =
1631 gst_rtp_h263_pay_package_new (rtph263pay->data,
1632 rtph263pay->data + rtph263pay->available_data,
1633 rtph263pay->available_data, 0, 0, NULL, TRUE);
1634 pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_A;
1635
1636 GST_DEBUG_OBJECT (rtph263pay, "Available data: %d",
1637 rtph263pay->available_data);
1638
1639 pack->outbuf =
1640 gst_rtp_buffer_new_allocate (GST_RTP_H263_PAYLOAD_HEADER_MODE_A, 0, 0);
1641
1642 return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1643 }
1644
1645 static GstFlowReturn
gst_rtp_h263_pay_flush(GstRtpH263Pay * rtph263pay)1646 gst_rtp_h263_pay_flush (GstRtpH263Pay * rtph263pay)
1647 {
1648
1649 /*
1650 * FIXME: GSTUF bits are ignored right now,
1651 * - not using EBIT/SBIT payload header fields in mode A fragmentation - ffmpeg doesn't need them, but others?
1652 */
1653
1654 GstFlowReturn ret;
1655 GstRtpH263PayContext *context;
1656 gint i;
1657
1658 ret = 0;
1659 context = (GstRtpH263PayContext *) g_malloc0 (sizeof (GstRtpH263PayContext));
1660 context->mtu =
1661 rtph263pay->payload.mtu - (MTU_SECURITY_OFFSET + GST_RTP_HEADER_LEN +
1662 GST_RTP_H263_PAYLOAD_HEADER_MODE_C);
1663
1664 GST_DEBUG_OBJECT (rtph263pay, "MTU: %d", context->mtu);
1665 rtph263pay->available_data = gst_buffer_get_size (rtph263pay->current_buffer);
1666 if (rtph263pay->available_data == 0) {
1667 ret = GST_FLOW_OK;
1668 goto end;
1669 }
1670
1671 /* Get a pointer to all the data for the frame */
1672 gst_buffer_map (rtph263pay->current_buffer, &rtph263pay->map, GST_MAP_READ);
1673 rtph263pay->data = (guint8 *) rtph263pay->map.data;
1674
1675 /* Picture header */
1676 context->piclayer = (GstRtpH263PayPic *) rtph263pay->data;
1677
1678 if (context->piclayer->ptype_pictype == 0)
1679 GST_DEBUG_OBJECT (rtph263pay, "We got an I-frame");
1680 else
1681 GST_DEBUG_OBJECT (rtph263pay, "We got a P-frame");
1682
1683 context->cpm = rtph263pay->data[6] >> 7;
1684
1685 GST_DEBUG_OBJECT (rtph263pay, "CPM: %d", context->cpm);
1686
1687 GST_DEBUG_OBJECT (rtph263pay, "Payload length is: %d",
1688 rtph263pay->available_data);
1689
1690 /*
1691 * - MODE A - If normal, I and P frames, -> mode A
1692 * - GOB layer fragmentation
1693 * - MODE B - If normal, I and P frames, but GOBs > mtu
1694 * - MB layer fragmentation
1695 * - MODE C - For P frames with PB option, but GOBs > mtu
1696 * - MB layer fragmentation
1697 */
1698 if (rtph263pay->available_data + GST_RTP_H263_PAYLOAD_HEADER_MODE_A +
1699 GST_RTP_HEADER_LEN < context->mtu) {
1700 ret = gst_rtp_h263_send_entire_frame (rtph263pay, context);
1701 } else {
1702
1703 /*---------- FRAGMENTING THE FRAME BECAUSE TOO LARGE TO FIT IN MTU ----------*/
1704 GstRtpH263PayBoundry bound;
1705 gint first;
1706 guint payload_len;
1707 gboolean forcea = FALSE;
1708
1709 GST_DEBUG_OBJECT (rtph263pay, "Frame too large for MTU");
1710 /*
1711 * Let's go trough all the data and fragment it untill end is reached
1712 */
1713
1714 gst_rtp_h263_pay_boundry_init (&bound, NULL, rtph263pay->data - 1, 0, 0);
1715 context->gobs =
1716 (GstRtpH263PayGob **) g_malloc0 (format_props[context->piclayer->
1717 ptype_srcformat][0] * sizeof (GstRtpH263PayGob *));
1718
1719
1720 for (i = 0; i < format_props[context->piclayer->ptype_srcformat][0]; i++) {
1721 GST_DEBUG_OBJECT (rtph263pay, "Searching for gob %d", i);
1722 if (!gst_rtp_h263_pay_gobfinder (rtph263pay, &bound)) {
1723 if (i <= 1) {
1724 GST_WARNING_OBJECT (rtph263pay,
1725 "No GOB's were found in data stream! Please enable RTP mode in encoder. Forcing mode A for now.");
1726 ret = gst_rtp_h263_send_entire_frame (rtph263pay, context);
1727 goto end;
1728 } else {
1729 /* try to send fragments corresponding to found GOBs */
1730 forcea = TRUE;
1731 break;
1732 }
1733 }
1734
1735 context->gobs[i] = gst_rtp_h263_pay_gob_new (&bound, i);
1736 //FIXME - encoders may generate an EOS gob that has to be processed
1737 GST_DEBUG_OBJECT (rtph263pay,
1738 "Gob values are: gobn: %d, start: %p len: %d ebit: %d sbit: %d", i,
1739 context->gobs[i]->start, context->gobs[i]->length,
1740 context->gobs[i]->ebit, context->gobs[i]->sbit);
1741 }
1742 /* NOTE some places may still assume this to be the max possible */
1743 context->no_gobs = i;
1744 GST_DEBUG_OBJECT (rtph263pay, "Found %d GOBS of maximum %d",
1745 context->no_gobs, format_props[context->piclayer->ptype_srcformat][0]);
1746
1747 // Make packages smaller than MTU
1748 // A mode
1749 // - if ( GOB > MTU) -> B mode || C mode
1750 // Push packages
1751
1752 first = 0;
1753 payload_len = 0;
1754 i = 0;
1755 while (i < context->no_gobs) {
1756
1757 if (context->gobs[i]->length >= context->mtu) {
1758 if (payload_len == 0) {
1759
1760 GST_DEBUG_OBJECT (rtph263pay, "GOB len > MTU");
1761 if (rtph263pay->prop_payload_mode || forcea) {
1762 payload_len = context->gobs[i]->length;
1763 goto force_a;
1764 }
1765 if (!context->piclayer->ptype_pbmode) {
1766 GST_DEBUG_OBJECT (rtph263pay, "MODE B on GOB %d needed", i);
1767 if (!gst_rtp_h263_pay_mode_B_fragment (rtph263pay, context,
1768 context->gobs[i])) {
1769 GST_ERROR_OBJECT (rtph263pay,
1770 "There was an error fragmenting in mode B");
1771 ret = GST_FLOW_ERROR;
1772 goto end;
1773 }
1774 } else {
1775 //IMPLEMENT C mode
1776 GST_ERROR_OBJECT (rtph263pay,
1777 "MODE C on GOB %d needed, but not supported yet", i);
1778 /*if(!gst_rtp_h263_pay_mode_C_fragment(rtph263pay, context, context->gobs[i])) {
1779 ret = GST_FLOW_OK;
1780 GST_ERROR("There was an error fragmenting in mode C");
1781 goto decode_error;
1782 } */
1783 goto decode_error;
1784 }
1785 decode_error:
1786 i++;
1787 first = i;
1788 continue;
1789
1790 } else {
1791 goto payload_a_push;
1792 }
1793 }
1794
1795 if (payload_len + context->gobs[i]->length < context->mtu) {
1796 GST_DEBUG_OBJECT (rtph263pay, "GOB %d fills mtu", i);
1797 payload_len += context->gobs[i]->length;
1798 i++;
1799 if (i == context->no_gobs) {
1800 GST_DEBUG_OBJECT (rtph263pay, "LAST GOB %d", i);
1801 goto payload_a_push;
1802 }
1803
1804 } else {
1805 payload_a_push:
1806 GST_DEBUG_OBJECT (rtph263pay,
1807 "Pushing GOBS %d to %d because payload size is %d", first,
1808 first == i ? i : i - 1, payload_len);
1809 gst_rtp_h263_pay_A_fragment_push (rtph263pay, context, first,
1810 first == i ? i : i - 1);
1811 payload_len = 0;
1812 first = i;
1813 }
1814 continue;
1815
1816 force_a:
1817 GST_DEBUG_OBJECT (rtph263pay,
1818 "Pushing GOBS %d to %d because payload size is %d", first, i,
1819 payload_len);
1820 gst_rtp_h263_pay_A_fragment_push (rtph263pay, context, first, i);
1821 payload_len = 0;
1822 i++;
1823 first = i;
1824 }
1825
1826
1827 }/*---------- END OF FRAGMENTATION ----------*/
1828
1829 /* Flush the input buffer data */
1830
1831 end:
1832 gst_rtp_h263_pay_context_destroy (context,
1833 context->piclayer->ptype_srcformat);
1834 gst_buffer_unmap (rtph263pay->current_buffer, &rtph263pay->map);
1835 gst_buffer_replace (&rtph263pay->current_buffer, NULL);
1836
1837 return ret;
1838 }
1839
1840 static GstFlowReturn
gst_rtp_h263_pay_handle_buffer(GstRTPBasePayload * payload,GstBuffer * buffer)1841 gst_rtp_h263_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
1842 {
1843
1844 GstRtpH263Pay *rtph263pay;
1845 GstFlowReturn ret;
1846
1847 rtph263pay = GST_RTP_H263_PAY (payload);
1848 GST_DEBUG_OBJECT (rtph263pay,
1849 "-------------------- NEW FRAME ---------------");
1850
1851 rtph263pay->first_ts = GST_BUFFER_PTS (buffer);
1852
1853 gst_buffer_replace (&rtph263pay->current_buffer, buffer);
1854 gst_buffer_unref (buffer);
1855
1856 /* we always encode and flush a full picture */
1857 ret = gst_rtp_h263_pay_flush (rtph263pay);
1858 GST_DEBUG_OBJECT (rtph263pay,
1859 "-------------------- END FRAME ---------------");
1860
1861 return ret;
1862 }
1863
1864 gboolean
gst_rtp_h263_pay_plugin_init(GstPlugin * plugin)1865 gst_rtp_h263_pay_plugin_init (GstPlugin * plugin)
1866 {
1867 return gst_element_register (plugin, "rtph263pay",
1868 GST_RANK_SECONDARY, GST_TYPE_RTP_H263_PAY);
1869 }
1870