webrtc/nice: support consent-freshness RFC7675
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtph263pay.c
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
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
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
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
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
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
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
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 *
564 gst_rtp_h263_pay_package_new_empty (void)
565 {
566   return (GstRtpH263PayPackage *) g_malloc0 (sizeof (GstRtpH263PayPackage));
567 }
568
569 static GstRtpH263PayPackage *
570 gst_rtp_h263_pay_package_new (guint8 * start, guint8 * end, guint length,
571     guint8 sbit, guint8 ebit, GstBuffer * outbuf, gboolean marker)
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
590 gst_rtp_h263_pay_package_destroy (GstRtpH263PayPackage * pack)
591 {
592   if (pack)
593     g_free (pack);
594 }
595
596 static void
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
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
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
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 *
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
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
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
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
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
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
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
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 *
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 *
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
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
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_BUFFER_FLAG_SET (package->outbuf, GST_BUFFER_FLAG_MARKER);
1316     GST_DEBUG_OBJECT (rtph263pay, "Marker set!");
1317   }
1318
1319   gst_rtp_buffer_unmap (&rtp);
1320
1321   /*
1322    * Copy the payload data in the buffer
1323    */
1324   GST_DEBUG_OBJECT (rtph263pay, "Copying memory");
1325   gst_buffer_copy_into (package->outbuf, rtph263pay->current_buffer,
1326       GST_BUFFER_COPY_MEMORY, package->payload_start - rtph263pay->map.data,
1327       package->payload_len);
1328   gst_rtp_copy_video_meta (rtph263pay, package->outbuf,
1329       rtph263pay->current_buffer);
1330
1331   ret =
1332       gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtph263pay),
1333       package->outbuf);
1334   GST_DEBUG_OBJECT (rtph263pay, "Package pushed, returning");
1335
1336   gst_rtp_h263_pay_package_destroy (package);
1337
1338   return ret;
1339 }
1340
1341 static GstFlowReturn
1342 gst_rtp_h263_pay_A_fragment_push (GstRtpH263Pay * rtph263pay,
1343     GstRtpH263PayContext * context, guint first, guint last)
1344 {
1345
1346   GstRtpH263PayPackage *pack;
1347
1348   pack = gst_rtp_h263_pay_package_new_empty ();
1349
1350   pack->payload_start = context->gobs[first]->start;
1351   pack->sbit = context->gobs[first]->sbit;
1352   pack->ebit = context->gobs[last]->ebit;
1353   pack->payload_len =
1354       (context->gobs[last]->end - context->gobs[first]->start) + 1;
1355   pack->marker = FALSE;
1356
1357   if (last == context->no_gobs - 1) {
1358     pack->marker = TRUE;
1359   }
1360
1361   pack->gobn = context->gobs[first]->gobn;
1362   pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_A;
1363   pack->outbuf =
1364       gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
1365       (rtph263pay), pack->mode, 0, 0);
1366
1367   GST_DEBUG_OBJECT (rtph263pay, "Sending len:%d data to push function",
1368       pack->payload_len);
1369
1370   return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1371 }
1372
1373 static GstFlowReturn
1374 gst_rtp_h263_pay_B_fragment_push (GstRtpH263Pay * rtph263pay,
1375     GstRtpH263PayContext * context, GstRtpH263PayGob * gob, guint first,
1376     guint last, GstRtpH263PayBoundry * boundry)
1377 {
1378
1379   GstRtpH263PayPackage *pack;
1380   guint mv;
1381
1382   pack = gst_rtp_h263_pay_package_new_empty ();
1383
1384   pack->payload_start = gob->macroblocks[first]->start;
1385   pack->sbit = gob->macroblocks[first]->sbit;
1386   if (first == 0) {
1387     pack->payload_start = boundry->start;
1388     pack->sbit = boundry->sbit;
1389     pack->quant = gob->quant;
1390   } else {
1391     pack->quant = gob->macroblocks[first]->quant;
1392   }
1393   pack->payload_end = gob->macroblocks[last]->end;
1394
1395   pack->ebit = gob->macroblocks[last]->ebit;
1396   pack->mba = gob->macroblocks[first]->mba;
1397   pack->gobn = gob->gobn;
1398   pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_B;
1399   pack->nmvd = 0;
1400
1401   if (gob->macroblocks[first]->mb_type < 3) {
1402     if (gob->macroblocks[first]->mb_type == 2)
1403       pack->nmvd = 8;
1404     else if (gob->macroblocks[first]->mb_type < 2)
1405       pack->nmvd = 2;
1406
1407     for (mv = 0; mv < pack->nmvd; mv++)
1408       pack->mvd[mv] = gob->macroblocks[first]->mvd[mv];
1409   }
1410
1411   pack->marker = FALSE;
1412   if (last == gob->nmacroblocs - 1) {
1413     pack->ebit = 0;
1414   }
1415
1416   if ((format_props[context->piclayer->ptype_srcformat][0] - 1 == gob->gobn)
1417       && (last == gob->nmacroblocs - 1)) {
1418     pack->marker = TRUE;
1419   }
1420
1421   pack->payload_len = pack->payload_end - pack->payload_start + 1;
1422   pack->outbuf =
1423       gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
1424       (rtph263pay), pack->mode, 0, 0);
1425
1426   return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1427 }
1428
1429
1430 static gboolean
1431 gst_rtp_h263_pay_mode_B_fragment (GstRtpH263Pay * rtph263pay,
1432     GstRtpH263PayContext * context, GstRtpH263PayGob * gob)
1433 {
1434
1435
1436     /*---------- MODE B MODE FRAGMENTATION ----------*/
1437   GstRtpH263PayMB *mac, *mac0;
1438   guint max_payload_size;
1439   GstRtpH263PayBoundry boundry;
1440   guint mb;
1441   guint8 ebit;
1442
1443   guint first = 0;
1444   guint payload_len;
1445
1446   max_payload_size =
1447       context->mtu - GST_RTP_H263_PAYLOAD_HEADER_MODE_B - GST_RTP_HEADER_LEN;
1448
1449   gst_rtp_h263_pay_boundry_init (&boundry, gob->start, gob->start, gob->sbit,
1450       0);
1451
1452   gob->macroblocks =
1453       (GstRtpH263PayMB **) g_malloc0 (sizeof (GstRtpH263PayMB *) *
1454       format_props[context->piclayer->ptype_srcformat][1]);
1455
1456   GST_LOG_OBJECT (rtph263pay, "GOB isn't PB frame, applying mode B");
1457
1458   //initializing window
1459   context->win_end = boundry.end;
1460   if (gst_rtp_h263_pay_move_window_right (rtph263pay, context, 32, boundry.ebit,
1461           &boundry.end, &gob->end) != 0) {
1462     GST_ERROR_OBJECT (rtph263pay,
1463         "The rest of the bits should be 0, exiting, because something bad happend");
1464     goto decode_error;
1465   }
1466   //The first GOB of a frame "has no" actual header - PICTURE header is his header
1467   if (gob->gobn == 0) {
1468     guint shift;
1469     GST_LOG_OBJECT (rtph263pay, "Initial GOB");
1470     shift = 43;
1471
1472     boundry.ebit =
1473         gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1474         boundry.ebit, &boundry.end, &gob->end);
1475
1476     //We need PQUANT for mode B packages - so we store it
1477     gob->quant = context->window >> 27;
1478
1479     //if PCM == 1, then PSBI is present - header has 51 bits
1480     //shift for PQUANT (5) and PCM (1) = 6 bits
1481     shift = 6;
1482     if (context->cpm == 1)
1483       shift += 2;
1484     boundry.ebit =
1485         gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1486         boundry.ebit, &boundry.end, &gob->end);
1487
1488     GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1489
1490     //Shifting the PEI and PSPARE fields
1491     while ((context->window & 0x80000000) == 0x80000000) {
1492       boundry.ebit =
1493           gst_rtp_h263_pay_move_window_right (rtph263pay, context, 9,
1494           boundry.ebit, &boundry.end, &gob->end);
1495       GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1496     }
1497
1498     //shift the last PEI field
1499     boundry.ebit =
1500         gst_rtp_h263_pay_move_window_right (rtph263pay, context, 1,
1501         boundry.ebit, &boundry.end, &gob->end);
1502
1503   } else {
1504     //skipping GOBs 24 header bits + 5 GQUANT
1505     guint shift = 24;
1506
1507     GST_TRACE_OBJECT (rtph263pay, "INTER GOB");
1508
1509     //if CPM == 1, there are 2 more bits in the header - GSBI header is 31 bits long
1510     if (context->cpm == 1)
1511       shift += 2;
1512
1513     GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1514     boundry.ebit =
1515         gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1516         boundry.ebit, &boundry.end, &gob->end);
1517
1518     //We need GQUANT for mode B packages - so we store it
1519     gob->quant = context->window >> 27;
1520
1521     shift = 5;
1522     boundry.ebit =
1523         gst_rtp_h263_pay_move_window_right (rtph263pay, context, shift,
1524         boundry.ebit, &boundry.end, &gob->end);
1525
1526     GST_TRACE_OBJECT (rtph263pay, "window: 0x%08x", context->window);
1527   }
1528
1529   GST_TRACE_OBJECT (rtph263pay, "GQUANT IS: %08x", gob->quant);
1530
1531   // We are on MB layer
1532
1533   mac = mac0 = gst_rtp_h263_pay_mb_new (&boundry, 0);
1534   for (mb = 0; mb < format_props[context->piclayer->ptype_srcformat][1]; mb++) {
1535
1536     GST_TRACE_OBJECT (rtph263pay,
1537         "================ START MB %d =================", mb);
1538
1539     //Find next macroblock boundaries
1540     ebit = mac->ebit;
1541     if (!(mac =
1542             gst_rtp_h263_pay_B_mbfinder (rtph263pay, context, gob, mac, mb))) {
1543
1544       GST_LOG_OBJECT (rtph263pay, "Error decoding MB - sbit: %d", 8 - ebit);
1545       GST_ERROR_OBJECT (rtph263pay, "Error decoding in GOB");
1546
1547       gst_rtp_h263_pay_mb_destroy (mac0);
1548       goto decode_error;
1549     }
1550
1551     /* Store macroblock for further processing and delete old MB if any */
1552     gst_rtp_h263_pay_mb_destroy (gob->macroblocks[mb]);
1553     gob->macroblocks[mb] = mac;
1554
1555     //If mb_type == stuffing, don't increment the mb address
1556     if (mac->mb_type == 10) {
1557       mb--;
1558       continue;
1559     } else {
1560       gob->nmacroblocs++;
1561     }
1562
1563     if (mac->end >= gob->end) {
1564       GST_LOG_OBJECT (rtph263pay, "No more MBs in this GOB");
1565       if (!mac->ebit) {
1566         mac->end--;
1567       }
1568       gob->end = mac->end;
1569       break;
1570     }
1571     GST_DEBUG_OBJECT (rtph263pay,
1572         "Found MB: mba: %d start: %p end: %p len: %d sbit: %d ebit: %d",
1573         mac->mba, mac->start, mac->end, mac->length, mac->sbit, mac->ebit);
1574     GST_TRACE_OBJECT (rtph263pay,
1575         "================ END MB %d =================", mb);
1576   }
1577   gst_rtp_h263_pay_mb_destroy (mac0);
1578
1579   mb = 0;
1580   first = 0;
1581   payload_len = boundry.end - boundry.start + 1;
1582   GST_DEBUG_OBJECT (rtph263pay,
1583       "------------------------- NEW PACKAGE ----------------------");
1584   while (mb < gob->nmacroblocs) {
1585     if (payload_len + gob->macroblocks[mb]->length < max_payload_size) {
1586
1587       //FIXME: payload_len is not the real length -> ignoring sbit/ebit
1588       payload_len += gob->macroblocks[mb]->length;
1589       mb++;
1590
1591     } else {
1592       //FIXME: we should include the last few bits of the GOB in the package - do we do that now?
1593       //GST_DEBUG_OBJECT (rtph263pay, "Pushing GOBS %d to %d because payload size is %d", first,
1594       //    first == mb - 1, payload_len);
1595
1596       // FIXME: segfault if mb == 0 (first MB is larger than max_payload_size)
1597       GST_DEBUG_OBJECT (rtph263pay, "Push B mode fragment from mb %d to %d",
1598           first, mb - 1);
1599       if (gst_rtp_h263_pay_B_fragment_push (rtph263pay, context, gob, first,
1600               mb - 1, &boundry)) {
1601         GST_ERROR_OBJECT (rtph263pay, "Oooops, there was an error sending");
1602         goto decode_error;
1603       }
1604
1605       payload_len = 0;
1606       first = mb;
1607       GST_DEBUG_OBJECT (rtph263pay,
1608           "------------------------- END PACKAGE ----------------------");
1609       GST_DEBUG_OBJECT (rtph263pay,
1610           "------------------------- NEW PACKAGE ----------------------");
1611     }
1612   }
1613
1614   /* Push rest */
1615   GST_DEBUG_OBJECT (rtph263pay, "Remainder first: %d, MB: %d", first, mb);
1616   if (payload_len != 0) {
1617     GST_DEBUG_OBJECT (rtph263pay, "Push B mode fragment from mb %d to %d",
1618         first, mb - 1);
1619     if (gst_rtp_h263_pay_B_fragment_push (rtph263pay, context, gob, first,
1620             mb - 1, &boundry)) {
1621       GST_ERROR_OBJECT (rtph263pay, "Oooops, there was an error sending!");
1622       goto decode_error;
1623     }
1624   }
1625
1626     /*---------- END OF MODE B FRAGMENTATION ----------*/
1627
1628   return TRUE;
1629
1630 decode_error:
1631   return FALSE;
1632 }
1633
1634 static GstFlowReturn
1635 gst_rtp_h263_send_entire_frame (GstRtpH263Pay * rtph263pay,
1636     GstRtpH263PayContext * context)
1637 {
1638   GstRtpH263PayPackage *pack;
1639   pack =
1640       gst_rtp_h263_pay_package_new (rtph263pay->data,
1641       rtph263pay->data + rtph263pay->available_data,
1642       rtph263pay->available_data, 0, 0, NULL, TRUE);
1643   pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_A;
1644
1645   GST_DEBUG_OBJECT (rtph263pay, "Available data: %d",
1646       rtph263pay->available_data);
1647
1648   pack->outbuf =
1649       gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
1650       (rtph263pay), GST_RTP_H263_PAYLOAD_HEADER_MODE_A, 0, 0);
1651
1652   return gst_rtp_h263_pay_push (rtph263pay, context, pack);
1653 }
1654
1655 static GstFlowReturn
1656 gst_rtp_h263_pay_flush (GstRtpH263Pay * rtph263pay)
1657 {
1658
1659   /*
1660    * FIXME: GSTUF bits are ignored right now,
1661    *  - not using EBIT/SBIT payload header fields in mode A fragmentation - ffmpeg doesn't need them, but others?
1662    */
1663
1664   GstFlowReturn ret;
1665   GstRtpH263PayContext *context;
1666   gint i;
1667
1668   ret = 0;
1669   context = (GstRtpH263PayContext *) g_malloc0 (sizeof (GstRtpH263PayContext));
1670   context->mtu =
1671       rtph263pay->payload.mtu - (MTU_SECURITY_OFFSET + GST_RTP_HEADER_LEN +
1672       GST_RTP_H263_PAYLOAD_HEADER_MODE_C);
1673
1674   GST_DEBUG_OBJECT (rtph263pay, "MTU: %d", context->mtu);
1675   rtph263pay->available_data = gst_buffer_get_size (rtph263pay->current_buffer);
1676   if (rtph263pay->available_data == 0) {
1677     ret = GST_FLOW_OK;
1678     goto end;
1679   }
1680
1681   /* Get a pointer to all the data for the frame */
1682   gst_buffer_map (rtph263pay->current_buffer, &rtph263pay->map, GST_MAP_READ);
1683   rtph263pay->data = (guint8 *) rtph263pay->map.data;
1684
1685   /* Picture header */
1686   context->piclayer = (GstRtpH263PayPic *) rtph263pay->data;
1687
1688   if (context->piclayer->ptype_pictype == 0)
1689     GST_DEBUG_OBJECT (rtph263pay, "We got an I-frame");
1690   else
1691     GST_DEBUG_OBJECT (rtph263pay, "We got a P-frame");
1692
1693   context->cpm = rtph263pay->data[6] >> 7;
1694
1695   GST_DEBUG_OBJECT (rtph263pay, "CPM: %d", context->cpm);
1696
1697   GST_DEBUG_OBJECT (rtph263pay, "Payload length is: %d",
1698       rtph263pay->available_data);
1699
1700   /*
1701    * - MODE A - If normal, I and P frames,  -> mode A
1702    *   - GOB layer fragmentation
1703    * - MODE B - If normal, I and P frames, but GOBs > mtu
1704    *   - MB layer fragmentation
1705    * - MODE C - For P frames with PB option, but GOBs > mtu
1706    *   - MB layer fragmentation
1707    */
1708   if (rtph263pay->available_data + GST_RTP_H263_PAYLOAD_HEADER_MODE_A +
1709       GST_RTP_HEADER_LEN < context->mtu) {
1710     ret = gst_rtp_h263_send_entire_frame (rtph263pay, context);
1711   } else {
1712
1713       /*---------- FRAGMENTING THE FRAME BECAUSE TOO LARGE TO FIT IN MTU ----------*/
1714     GstRtpH263PayBoundry bound;
1715     gint first;
1716     guint payload_len;
1717     gboolean forcea = FALSE;
1718
1719     GST_DEBUG_OBJECT (rtph263pay, "Frame too large for MTU");
1720     /*
1721      * Let's go trough all the data and fragment it until end is reached
1722      */
1723
1724     gst_rtp_h263_pay_boundry_init (&bound, NULL, rtph263pay->data - 1, 0, 0);
1725     context->gobs =
1726         (GstRtpH263PayGob **) g_malloc0 (format_props[context->piclayer->
1727             ptype_srcformat][0] * sizeof (GstRtpH263PayGob *));
1728
1729
1730     for (i = 0; i < format_props[context->piclayer->ptype_srcformat][0]; i++) {
1731       GST_DEBUG_OBJECT (rtph263pay, "Searching for gob %d", i);
1732       if (!gst_rtp_h263_pay_gobfinder (rtph263pay, &bound)) {
1733         if (i <= 1) {
1734           GST_WARNING_OBJECT (rtph263pay,
1735               "No GOB's were found in data stream! Please enable RTP mode in encoder. Forcing mode A for now.");
1736           ret = gst_rtp_h263_send_entire_frame (rtph263pay, context);
1737           goto end;
1738         } else {
1739           /* try to send fragments corresponding to found GOBs */
1740           forcea = TRUE;
1741           break;
1742         }
1743       }
1744
1745       context->gobs[i] = gst_rtp_h263_pay_gob_new (&bound, i);
1746       //FIXME - encoders may generate an EOS gob that has to be processed
1747       GST_DEBUG_OBJECT (rtph263pay,
1748           "Gob values are: gobn: %d, start: %p len: %d ebit: %d sbit: %d", i,
1749           context->gobs[i]->start, context->gobs[i]->length,
1750           context->gobs[i]->ebit, context->gobs[i]->sbit);
1751     }
1752     /* NOTE some places may still assume this to be the max possible */
1753     context->no_gobs = i;
1754     GST_DEBUG_OBJECT (rtph263pay, "Found %d GOBS of maximum %d",
1755         context->no_gobs, format_props[context->piclayer->ptype_srcformat][0]);
1756
1757     // Make packages smaller than MTU
1758     //   A mode
1759     // - if ( GOB > MTU) -> B mode || C mode
1760     //   Push packages
1761
1762     first = 0;
1763     payload_len = 0;
1764     i = 0;
1765     while (i < context->no_gobs) {
1766
1767       if (context->gobs[i]->length >= context->mtu) {
1768         if (payload_len == 0) {
1769
1770           GST_DEBUG_OBJECT (rtph263pay, "GOB len > MTU");
1771           if (rtph263pay->prop_payload_mode || forcea) {
1772             payload_len = context->gobs[i]->length;
1773             goto force_a;
1774           }
1775           if (!context->piclayer->ptype_pbmode) {
1776             GST_DEBUG_OBJECT (rtph263pay, "MODE B on GOB %d needed", i);
1777             if (!gst_rtp_h263_pay_mode_B_fragment (rtph263pay, context,
1778                     context->gobs[i])) {
1779               GST_ERROR_OBJECT (rtph263pay,
1780                   "There was an error fragmenting in mode B");
1781               ret = GST_FLOW_ERROR;
1782               goto end;
1783             }
1784           } else {
1785             //IMPLEMENT C mode
1786             GST_ERROR_OBJECT (rtph263pay,
1787                 "MODE C on GOB %d needed, but not supported yet", i);
1788             /*if(!gst_rtp_h263_pay_mode_C_fragment(rtph263pay, context, context->gobs[i])) {
1789                ret = GST_FLOW_OK;
1790                GST_ERROR("There was an error fragmenting in mode C");
1791                goto decode_error;
1792                } */
1793             goto decode_error;
1794           }
1795         decode_error:
1796           i++;
1797           first = i;
1798           continue;
1799
1800         } else {
1801           goto payload_a_push;
1802         }
1803       }
1804
1805       if (payload_len + context->gobs[i]->length < context->mtu) {
1806         GST_DEBUG_OBJECT (rtph263pay, "GOB %d fills mtu", i);
1807         payload_len += context->gobs[i]->length;
1808         i++;
1809         if (i == context->no_gobs) {
1810           GST_DEBUG_OBJECT (rtph263pay, "LAST GOB %d", i);
1811           goto payload_a_push;
1812         }
1813
1814       } else {
1815       payload_a_push:
1816         GST_DEBUG_OBJECT (rtph263pay,
1817             "Pushing GOBS %d to %d because payload size is %d", first,
1818             first == i ? i : i - 1, payload_len);
1819         gst_rtp_h263_pay_A_fragment_push (rtph263pay, context, first,
1820             first == i ? i : i - 1);
1821         payload_len = 0;
1822         first = i;
1823       }
1824       continue;
1825
1826     force_a:
1827       GST_DEBUG_OBJECT (rtph263pay,
1828           "Pushing GOBS %d to %d because payload size is %d", first, i,
1829           payload_len);
1830       gst_rtp_h263_pay_A_fragment_push (rtph263pay, context, first, i);
1831       payload_len = 0;
1832       i++;
1833       first = i;
1834     }
1835
1836
1837   }/*---------- END OF FRAGMENTATION ----------*/
1838
1839   /* Flush the input buffer data */
1840
1841 end:
1842   gst_rtp_h263_pay_context_destroy (context,
1843       context->piclayer->ptype_srcformat);
1844   gst_buffer_unmap (rtph263pay->current_buffer, &rtph263pay->map);
1845   gst_buffer_replace (&rtph263pay->current_buffer, NULL);
1846
1847   return ret;
1848 }
1849
1850 static GstFlowReturn
1851 gst_rtp_h263_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
1852 {
1853
1854   GstRtpH263Pay *rtph263pay;
1855   GstFlowReturn ret;
1856
1857   rtph263pay = GST_RTP_H263_PAY (payload);
1858   GST_DEBUG_OBJECT (rtph263pay,
1859       "-------------------- NEW FRAME ---------------");
1860
1861   rtph263pay->first_ts = GST_BUFFER_PTS (buffer);
1862
1863   gst_buffer_replace (&rtph263pay->current_buffer, buffer);
1864   gst_buffer_unref (buffer);
1865
1866   /* we always encode and flush a full picture */
1867   ret = gst_rtp_h263_pay_flush (rtph263pay);
1868   GST_DEBUG_OBJECT (rtph263pay,
1869       "-------------------- END FRAME ---------------");
1870
1871   return ret;
1872 }