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