rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtph261pay.c
1 /* GStreamer
2  * Copyright (C) <2014> Stian Selnes <stian@pexip.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  */
20
21 /**
22  * SECTION:element-rtph261pay
23  * @title: rtph261pay
24  * @see_also: rtph261depay
25  *
26  * Payload encoded H.261 video frames into RTP packets according to RFC 4587.
27  * For detailed information see: https://www.rfc-editor.org/rfc/rfc4587.txt
28  *
29  * The payloader takes a H.261 frame, parses it and splits it into fragments
30  * on MB boundaries in order to match configured MTU size. For each fragment
31  * an RTP packet is constructed with an RTP packet header followed by the
32  * fragment. In addition the payloader will make sure the packetized H.261
33  * stream appears as a continuous bit-stream after depacketization by shifting
34  * the encoded bit-stream of a frame to align with the last significant bit of
35  * the previous frame. This helps interoperability in the case where the
36  * encoder does not produce a continuous bit-stream but the decoder requires
37  * it.
38  *
39  * ## Example launch line
40  * |[
41  * gst-launch-1.0 videotestsrc ! avenc_h261 ! rtph261pay ! udpsink
42  * ]| This will encode a test video and payload it. Refer to the rtph261depay
43  * example to depayload and play the RTP stream.
44  *
45  */
46
47 #ifdef HAVE_CONFIG_H
48 #  include "config.h"
49 #endif
50
51 #include "gstrtph261pay.h"
52 #include "gstrtputils.h"
53 #include <gst/rtp/gstrtpbuffer.h>
54 #include <gst/video/video.h>
55 #include <gst/base/gstbitreader.h>
56 #include <string.h>
57
58 GST_DEBUG_CATEGORY_STATIC (rtph261pay_debug);
59 #define GST_CAT_DEFAULT (rtph261pay_debug)
60
61 #define GST_RTP_HEADER_LEN 12
62 #define GST_RTP_H261_PAYLOAD_HEADER_LEN 4
63
64 static GstStaticPadTemplate gst_rtp_h261_pay_sink_template =
65 GST_STATIC_PAD_TEMPLATE ("sink",
66     GST_PAD_SINK,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS ("video/x-h261")
69     );
70
71 static GstStaticPadTemplate gst_rtp_h261_pay_src_template =
72     GST_STATIC_PAD_TEMPLATE ("src",
73     GST_PAD_SRC,
74     GST_PAD_ALWAYS,
75     GST_STATIC_CAPS ("application/x-rtp, "
76         "media = (string) \"video\", "
77         "payload = (int) " GST_RTP_PAYLOAD_H261_STRING ", "
78         "clock-rate = (int) 90000, " "encoding-name = (string) \"H261\"; "
79         "application/x-rtp, "
80         "media = (string) \"video\", "
81         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
82         "clock-rate = (int) 90000, " "encoding-name = (string) \"H261\"")
83     );
84
85 G_DEFINE_TYPE (GstRtpH261Pay, gst_rtp_h261_pay, GST_TYPE_RTP_BASE_PAYLOAD);
86 #define parent_class gst_rtp_h261_pay_parent_class
87
88 typedef struct
89 {
90   guint32 mba;
91   guint32 mtype;
92   guint32 quant;
93   gint mvx;
94   gint mvy;
95   guint endpos;
96   gint gobn;
97 } Macroblock;
98
99 typedef struct
100 {
101   Macroblock last;
102   guint startpos;
103   guint endpos;
104   guint32 gn;
105   guint32 gquant;
106 } Gob;
107
108 #define PSC_LEN 20
109 #define TR_LEN 5
110 #define PTYPE_LEN 6
111 #define GBSC_LEN 16
112 #define GN_LEN 4
113 #define GQUANT_LEN 5
114 #define GEI_LEN 1
115 #define GSPARE_LEN 8
116 #define MQUANT_LEN 5
117 #define MAX_NUM_GOB 12
118
119 typedef enum
120 {
121   PARSE_END_OF_BUFFER = -2,
122   PARSE_ERROR = -1,
123   PARSE_OK = 0,
124   PARSE_END_OF_FRAME,
125   PARSE_END_OF_GOB,
126 } ParseReturn;
127
128
129 #define SKIP_BITS(br,nbits) G_STMT_START {      \
130     if (!gst_bit_reader_skip (br, nbits))       \
131       return PARSE_END_OF_BUFFER;               \
132   } G_STMT_END
133
134 #define GET_BITS(br,val,nbits) G_STMT_START {             \
135     if (!gst_bit_reader_get_bits_uint32 (br, val, nbits)) \
136       return PARSE_END_OF_BUFFER;                         \
137   } G_STMT_END
138
139 /* Unchecked since we peek outside the buffer. Ok because of padding. */
140 #define PEEK_BITS(br,val,nbits) G_STMT_START {                    \
141     *val = gst_bit_reader_peek_bits_uint16_unchecked (br, nbits); \
142   } G_STMT_END
143
144
145 #define MBA_STUFFING 34
146 #define MBA_START_CODE 35
147 #define MBA_LEN 35
148 #define MBA_WID 4
149 /* [code, mask, nbits, mba] */
150 static const guint16 mba_table[MBA_LEN][MBA_WID] = {
151   {0x8000, 0x8000, 1, 1},
152   {0x6000, 0xe000, 3, 2},
153   {0x4000, 0xe000, 3, 3},
154   {0x3000, 0xf000, 4, 4},
155   {0x2000, 0xf000, 4, 5},
156   {0x1800, 0xf800, 5, 6},
157   {0x1000, 0xf800, 5, 7},
158   {0x0e00, 0xfe00, 7, 8},
159   {0x0c00, 0xfe00, 7, 9},
160   {0x0b00, 0xff00, 8, 10},
161   {0x0a00, 0xff00, 8, 11},
162   {0x0900, 0xff00, 8, 12},
163   {0x0800, 0xff00, 8, 13},
164   {0x0700, 0xff00, 8, 14},
165   {0x0600, 0xff00, 8, 15},
166   {0x05c0, 0xffc0, 10, 16},
167   {0x0580, 0xffc0, 10, 17},
168   {0x0540, 0xffc0, 10, 18},
169   {0x0500, 0xffc0, 10, 19},
170   {0x04c0, 0xffc0, 10, 20},
171   {0x0480, 0xffc0, 10, 21},
172   {0x0460, 0xffe0, 11, 22},
173   {0x0440, 0xffe0, 11, 23},
174   {0x0420, 0xffe0, 11, 24},
175   {0x0400, 0xffe0, 11, 25},
176   {0x03e0, 0xffe0, 11, 26},
177   {0x03c0, 0xffe0, 11, 27},
178   {0x03a0, 0xffe0, 11, 28},
179   {0x0380, 0xffe0, 11, 29},
180   {0x0360, 0xffe0, 11, 30},
181   {0x0340, 0xffe0, 11, 31},
182   {0x0320, 0xffe0, 11, 32},
183   {0x0300, 0xffe0, 11, 33},
184   {0x01e0, 0xffe0, 11, MBA_STUFFING},
185   {0x0001, 0xffff, 16, MBA_START_CODE},
186 };
187
188 #define MTYPE_INTRA    (1 << 0)
189 #define MTYPE_INTER    (1 << 1)
190 #define MTYPE_MC       (1 << 2)
191 #define MTYPE_FIL      (1 << 3)
192 #define MTYPE_MQUANT   (1 << 4)
193 #define MTYPE_MVD      (1 << 5)
194 #define MTYPE_CBP      (1 << 6)
195 #define MTYPE_TCOEFF   (1 << 7)
196 #define MTYPE_LEN 10
197 #define MTYPE_WID 4
198 /* [code, mask, nbits, flags] */
199 static const guint16 mtype_table[MTYPE_LEN][MTYPE_WID] = {
200   {0x8000, 0x8000, 1, MTYPE_INTER | MTYPE_CBP | MTYPE_TCOEFF},
201   {0x4000, 0xc000, 2,
202       MTYPE_INTER | MTYPE_MC | MTYPE_FIL | MTYPE_MVD | MTYPE_CBP |
203         MTYPE_TCOEFF},
204   {0x2000, 0xe000, 3, MTYPE_INTER | MTYPE_MC | MTYPE_FIL | MTYPE_MVD},
205   {0x1000, 0xf000, 4, MTYPE_INTRA | MTYPE_TCOEFF},
206   {0x0800, 0xf800, 5, MTYPE_INTER | MTYPE_MQUANT | MTYPE_CBP | MTYPE_TCOEFF},
207   {0x0400, 0xfc00, 6,
208       MTYPE_INTER | MTYPE_MC | MTYPE_FIL | MTYPE_MQUANT | MTYPE_MVD |
209         MTYPE_CBP | MTYPE_TCOEFF},
210   {0x0200, 0xfe00, 7, MTYPE_INTRA | MTYPE_MQUANT | MTYPE_TCOEFF},
211   {0x0100, 0xff00, 8,
212       MTYPE_INTER | MTYPE_MC | MTYPE_MVD | MTYPE_CBP | MTYPE_TCOEFF},
213   {0x0080, 0xff80, 9, MTYPE_INTER | MTYPE_MC | MTYPE_MVD},
214   {0x0040, 0xffc0, 10,
215       MTYPE_INTER | MTYPE_MC | MTYPE_MQUANT | MTYPE_MVD | MTYPE_CBP |
216         MTYPE_TCOEFF},
217 };
218
219 #define MVD_LEN 32
220 #define MVD_WID 5
221 /* [code, mask, nbits, mvd1, mvd2] */
222 static const guint16 mvd_table[MVD_LEN][MVD_WID] = {
223   {0x8000, 0x8000, 1, 0, 0},
224   {0x6000, 0xe000, 3, -1, -1},
225   {0x4000, 0xe000, 3, 1, 1},
226   {0x3000, 0xf000, 4, -2, 30},
227   {0x2000, 0xf000, 4, 2, -30},
228   {0x1800, 0xf800, 5, -3, 29},
229   {0x1000, 0xf800, 5, 3, -29},
230   {0x0e00, 0xfe00, 7, -4, 28},
231   {0x0c00, 0xfe00, 7, 4, -28},
232   {0x0700, 0xff00, 8, -7, 25},
233   {0x0900, 0xff00, 8, -6, 26},
234   {0x0b00, 0xff00, 8, -5, 27},
235   {0x0a00, 0xff00, 8, 5, -27},
236   {0x0800, 0xff00, 8, 6, -26},
237   {0x0600, 0xff00, 8, 7, -25},
238   {0x04c0, 0xffc0, 10, -10, 22},
239   {0x0540, 0xffc0, 10, -9, 23},
240   {0x05c0, 0xffc0, 10, -8, 24},
241   {0x0580, 0xffc0, 10, 8, -24},
242   {0x0500, 0xffc0, 10, 9, -23},
243   {0x0480, 0xffc0, 10, 10, -22},
244   {0x0320, 0xffe0, 11, -16, 16},
245   {0x0360, 0xffe0, 11, -15, 17},
246   {0x03a0, 0xffe0, 11, -14, 18},
247   {0x03e0, 0xffe0, 11, -13, 19},
248   {0x0420, 0xffe0, 11, -12, 20},
249   {0x0460, 0xffe0, 11, -11, 21},
250   {0x0440, 0xffe0, 11, 11, -21},
251   {0x0400, 0xffe0, 11, 12, -20},
252   {0x03c0, 0xffe0, 11, 13, -19},
253   {0x0380, 0xffe0, 11, 14, -18},
254   {0x0340, 0xffe0, 11, 15, -17},
255 };
256
257 #define CBP_LEN 63
258 /* [code, mask, nbits, cbp] */
259 static const guint16 cbp_table[CBP_LEN][4] = {
260   {0xe000, 0xe000, 3, 60},
261   {0xd000, 0xf000, 4, 4},
262   {0xc000, 0xf000, 4, 8},
263   {0xb000, 0xf000, 4, 16},
264   {0xa000, 0xf000, 4, 32},
265   {0x9800, 0xf800, 5, 12},
266   {0x9000, 0xf800, 5, 48},
267   {0x8800, 0xf800, 5, 20},
268   {0x8000, 0xf800, 5, 40},
269   {0x7800, 0xf800, 5, 28},
270   {0x7000, 0xf800, 5, 44},
271   {0x6800, 0xf800, 5, 52},
272   {0x6000, 0xf800, 5, 56},
273   {0x5800, 0xf800, 5, 1},
274   {0x5000, 0xf800, 5, 61},
275   {0x4800, 0xf800, 5, 2},
276   {0x4000, 0xf800, 5, 62},
277   {0x3c00, 0xfc00, 6, 24},
278   {0x3800, 0xfc00, 6, 36},
279   {0x3400, 0xfc00, 6, 3},
280   {0x3000, 0xfc00, 6, 63},
281   {0x2e00, 0xfe00, 7, 5},
282   {0x2c00, 0xfe00, 7, 9},
283   {0x2a00, 0xfe00, 7, 17},
284   {0x2800, 0xfe00, 7, 33},
285   {0x2600, 0xfe00, 7, 6},
286   {0x2400, 0xfe00, 7, 10},
287   {0x2200, 0xfe00, 7, 18},
288   {0x2000, 0xfe00, 7, 34},
289   {0x1f00, 0xff00, 8, 7},
290   {0x1e00, 0xff00, 8, 11},
291   {0x1d00, 0xff00, 8, 19},
292   {0x1c00, 0xff00, 8, 35},
293   {0x1b00, 0xff00, 8, 13},
294   {0x1a00, 0xff00, 8, 49},
295   {0x1900, 0xff00, 8, 21},
296   {0x1800, 0xff00, 8, 41},
297   {0x1700, 0xff00, 8, 14},
298   {0x1600, 0xff00, 8, 50},
299   {0x1500, 0xff00, 8, 22},
300   {0x1400, 0xff00, 8, 42},
301   {0x1300, 0xff00, 8, 15},
302   {0x1200, 0xff00, 8, 51},
303   {0x1100, 0xff00, 8, 23},
304   {0x1000, 0xff00, 8, 43},
305   {0x0f00, 0xff00, 8, 25},
306   {0x0e00, 0xff00, 8, 37},
307   {0x0d00, 0xff00, 8, 26},
308   {0x0c00, 0xff00, 8, 38},
309   {0x0b00, 0xff00, 8, 29},
310   {0x0a00, 0xff00, 8, 45},
311   {0x0900, 0xff00, 8, 53},
312   {0x0800, 0xff00, 8, 57},
313   {0x0700, 0xff00, 8, 30},
314   {0x0600, 0xff00, 8, 46},
315   {0x0500, 0xff00, 8, 54},
316   {0x0400, 0xff00, 8, 58},
317   {0x0380, 0xff80, 9, 31},
318   {0x0300, 0xff80, 9, 47},
319   {0x0280, 0xff80, 9, 55},
320   {0x0200, 0xff80, 9, 59},
321   {0x0180, 0xff80, 9, 27},
322   {0x0100, 0xff80, 9, 39},
323 };
324
325 #define TCOEFF_EOB 0xffff
326 #define TCOEFF_ESC 0xfffe
327 #define TCOEFF_LEN 65
328 /* [code, mask, nbits, run, level] */
329 static const guint16 tcoeff_table[TCOEFF_LEN][5] = {
330   {0x8000, 0xc000, 2, TCOEFF_EOB, 0},   /* Not available for first coeff */
331   /* {0x8000, 0x8000,  2,  0,  1}, *//* Available only for first Inter coeff */
332   {0xc000, 0xc000, 3, 0, 1},    /* Not available for first coeff */
333   {0x6000, 0xe000, 4, 1, 1},
334   {0x4000, 0xf000, 5, 0, 2},
335   {0x5000, 0xf000, 5, 2, 1},
336   {0x2800, 0xf800, 6, 0, 3},
337   {0x3800, 0xf800, 6, 3, 1},
338   {0x3000, 0xf800, 6, 4, 1},
339   {0x0400, 0xfc00, 6, TCOEFF_ESC, 0},
340   {0x1800, 0xfc00, 7, 1, 2},
341   {0x1c00, 0xfc00, 7, 5, 1},
342   {0x1400, 0xfc00, 7, 6, 1},
343   {0x1000, 0xfc00, 7, 7, 1},
344   {0x0c00, 0xfe00, 8, 0, 4},
345   {0x0800, 0xfe00, 8, 2, 2},
346   {0x0e00, 0xfe00, 8, 8, 1},
347   {0x0a00, 0xfe00, 8, 9, 1},
348   {0x2600, 0xff00, 9, 0, 5},
349   {0x2100, 0xff00, 9, 0, 6},
350   {0x2500, 0xff00, 9, 1, 3},
351   {0x2400, 0xff00, 9, 3, 2},
352   {0x2700, 0xff00, 9, 10, 1},
353   {0x2300, 0xff00, 9, 11, 1},
354   {0x2200, 0xff00, 9, 12, 1},
355   {0x2000, 0xff00, 9, 13, 1},
356   {0x0280, 0xffc0, 11, 0, 7},
357   {0x0300, 0xffc0, 11, 1, 4},
358   {0x02c0, 0xffc0, 11, 2, 3},
359   {0x03c0, 0xffc0, 11, 4, 2},
360   {0x0240, 0xffc0, 11, 5, 2},
361   {0x0380, 0xffc0, 11, 14, 1},
362   {0x0340, 0xffc0, 11, 15, 1},
363   {0x0200, 0xffc0, 11, 16, 1},
364   {0x01d0, 0xfff0, 13, 0, 8},
365   {0x0180, 0xfff0, 13, 0, 9},
366   {0x0130, 0xfff0, 13, 0, 10},
367   {0x0100, 0xfff0, 13, 0, 11},
368   {0x01b0, 0xfff0, 13, 1, 5},
369   {0x0140, 0xfff0, 13, 2, 4},
370   {0x01c0, 0xfff0, 13, 3, 3},
371   {0x0120, 0xfff0, 13, 4, 3},
372   {0x01e0, 0xfff0, 13, 6, 2},
373   {0x0150, 0xfff0, 13, 7, 2},
374   {0x0110, 0xfff0, 13, 8, 2},
375   {0x01f0, 0xfff0, 13, 17, 1},
376   {0x01a0, 0xfff0, 13, 18, 1},
377   {0x0190, 0xfff0, 13, 19, 1},
378   {0x0170, 0xfff0, 13, 20, 1},
379   {0x0160, 0xfff0, 13, 21, 1},
380   {0x00d0, 0xfff8, 14, 0, 12},
381   {0x00c8, 0xfff8, 14, 0, 13},
382   {0x00c0, 0xfff8, 14, 0, 14},
383   {0x00b8, 0xfff8, 14, 0, 15},
384   {0x00b0, 0xfff8, 14, 1, 6},
385   {0x00a8, 0xfff8, 14, 1, 7},
386   {0x00a0, 0xfff8, 14, 2, 5},
387   {0x0098, 0xfff8, 14, 3, 4},
388   {0x0090, 0xfff8, 14, 5, 3},
389   {0x0088, 0xfff8, 14, 9, 2},
390   {0x0080, 0xfff8, 14, 10, 2},
391   {0x00f8, 0xfff8, 14, 22, 1},
392   {0x00f0, 0xfff8, 14, 23, 1},
393   {0x00e8, 0xfff8, 14, 24, 1},
394   {0x00e0, 0xfff8, 14, 25, 1},
395   {0x00d8, 0xfff8, 14, 26, 1},
396 };
397
398 static ParseReturn
399 decode_mba (GstBitReader * br, gint * mba)
400 {
401   gint i;
402   guint16 code;
403
404   *mba = -1;
405   do {
406     PEEK_BITS (br, &code, 16);
407     for (i = 0; i < MBA_LEN; i++) {
408       if ((code & mba_table[i][1]) == mba_table[i][0]) {
409         *mba = mba_table[i][3];
410
411         if (*mba == MBA_START_CODE)
412           return PARSE_END_OF_GOB;
413         SKIP_BITS (br, mba_table[i][2]);
414         if (*mba != MBA_STUFFING)
415           return PARSE_OK;
416       }
417     }
418   } while (*mba == MBA_STUFFING);
419
420   /* 0x0 indicates end of frame since we appended 0-bytes */
421   if (code == 0x0)
422     return PARSE_END_OF_FRAME;
423
424   return PARSE_ERROR;
425 }
426
427 static ParseReturn
428 decode_mtype (GstBitReader * br, guint * mtype)
429 {
430   gint i;
431   guint16 code;
432
433   PEEK_BITS (br, &code, 16);
434   for (i = 0; i < MTYPE_LEN; i++) {
435     if ((code & mtype_table[i][1]) == mtype_table[i][0]) {
436       SKIP_BITS (br, mtype_table[i][2]);
437       *mtype = mtype_table[i][3];
438       return PARSE_OK;
439     }
440   }
441
442   return PARSE_ERROR;
443 }
444
445 static ParseReturn
446 decode_mvd (GstBitReader * br, gint * mvd1, gint * mvd2)
447 {
448   gint i;
449   guint16 code;
450
451   PEEK_BITS (br, &code, 16);
452   for (i = 0; i < MVD_LEN; i++) {
453     if ((code & mvd_table[i][1]) == mvd_table[i][0]) {
454       SKIP_BITS (br, mvd_table[i][2]);
455       *mvd1 = (gint16) mvd_table[i][3];
456       *mvd2 = (gint16) mvd_table[i][4];
457       return PARSE_OK;
458     }
459   }
460
461   return PARSE_ERROR;
462 }
463
464 static ParseReturn
465 decode_cbp (GstBitReader * br, guint * cbp)
466 {
467   gint i;
468   guint16 code;
469
470   PEEK_BITS (br, &code, 16);
471   for (i = 0; i < CBP_LEN; i++) {
472     if ((code & cbp_table[i][1]) == cbp_table[i][0]) {
473       SKIP_BITS (br, cbp_table[i][2]);
474       *cbp = cbp_table[i][3];
475       return PARSE_OK;
476     }
477   }
478
479   return PARSE_ERROR;
480 }
481
482 static ParseReturn
483 decode_tcoeff (GstBitReader * br, guint mtype)
484 {
485   gint i;
486   guint16 code;
487   gboolean eob;
488
489   /* Special handling of first coeff */
490   if (mtype & MTYPE_INTER) {
491     /* Inter, different vlc since EOB is not allowed */
492     PEEK_BITS (br, &code, 16);
493     if (code & 0x8000) {
494       SKIP_BITS (br, 2);
495       GST_TRACE ("tcoeff first inter special");
496     } else {
497       /* Fallthrough. Let the first coeff be handled like other coeffs since
498        * the vlc is the same as long as the first bit is not set. */
499     }
500   } else {
501     /* Intra, first coeff is fixed 8-bit */
502     GST_TRACE ("tcoeff first intra special");
503     SKIP_BITS (br, 8);
504   }
505
506   /* Block must end with EOB. */
507   eob = FALSE;
508   while (!eob) {
509     PEEK_BITS (br, &code, 16);
510     for (i = 0; i < TCOEFF_LEN; i++) {
511       if ((code & tcoeff_table[i][1]) == tcoeff_table[i][0]) {
512         GST_TRACE ("tcoeff vlc[%d], run=%d, level=%d", i, tcoeff_table[i][3],
513             tcoeff_table[i][4]);
514         SKIP_BITS (br, tcoeff_table[i][2]);
515         if (tcoeff_table[i][3] == TCOEFF_EOB) {
516           eob = TRUE;
517         } else if (tcoeff_table[i][3] == TCOEFF_ESC) {
518 #if 0
519           guint16 val;
520           val = gst_bit_reader_peek_bits_uint16_unchecked (br, 6 + 8);
521           GST_TRACE ("esc run=%d, level=%d", val >> 8, (gint8) (val & 0xff));
522 #endif
523           SKIP_BITS (br, 6 + 8);
524         }
525         break;
526       }
527     }
528     if (i == TCOEFF_LEN)
529       /* No matching VLC */
530       return PARSE_ERROR;
531   }
532
533   return PARSE_OK;
534 }
535
536 static gint
537 find_picture_header_offset (const guint8 * data, gsize size)
538 {
539   gint i;
540   guint32 val;
541
542   if (size < 4)
543     return -1;
544
545   val = GST_READ_UINT32_BE (data);
546   for (i = 0; i < 8; i++) {
547     if ((val >> (12 - i)) == 0x10)
548       return i;
549   }
550
551   return -1;
552 }
553
554 static ParseReturn
555 parse_picture_header (GstRtpH261Pay * pay, GstBitReader * br, gint * num_gob)
556 {
557   guint32 val;
558
559   GET_BITS (br, &val, PSC_LEN);
560   if (val != 0x10)
561     return PARSE_ERROR;
562   SKIP_BITS (br, TR_LEN);
563   GET_BITS (br, &val, PTYPE_LEN);
564   *num_gob = (val & 0x04) == 0 ? 3 : 12;
565
566   return PARSE_OK;
567 }
568
569 static ParseReturn
570 parse_gob_header (GstRtpH261Pay * pay, GstBitReader * br, Gob * gob)
571 {
572   guint32 val;
573
574   GET_BITS (br, &val, GBSC_LEN);
575   if (val != 0x01)
576     return PARSE_ERROR;
577   GET_BITS (br, &gob->gn, GN_LEN);
578   GST_TRACE_OBJECT (pay, "Parsing GOB %d", gob->gn);
579
580   GET_BITS (br, &gob->gquant, GQUANT_LEN);
581   GST_TRACE_OBJECT (pay, "GQUANT %d", gob->gquant);
582   GET_BITS (br, &val, GEI_LEN);
583   while (val != 0) {
584     SKIP_BITS (br, GSPARE_LEN);
585     GET_BITS (br, &val, GEI_LEN);
586   }
587
588   return PARSE_OK;
589 }
590
591 static ParseReturn
592 parse_mb (GstRtpH261Pay * pay, GstBitReader * br, const Macroblock * prev,
593     Macroblock * mb)
594 {
595   gint mba_diff;
596   guint cbp;
597   ParseReturn ret;
598
599   cbp = 0x3f;
600   mb->quant = prev->quant;
601
602   if ((ret = decode_mba (br, &mba_diff)) != PARSE_OK)
603     return ret;
604   mb->mba = prev->mba == 0 ? mba_diff : prev->mba + mba_diff;
605   GST_TRACE_OBJECT (pay, "Parse MBA %d (mba_diff %d)", mb->mba, mba_diff);
606
607   if ((ret = decode_mtype (br, &mb->mtype)) != PARSE_OK)
608     return ret;
609   GST_TRACE_OBJECT (pay,
610       "MTYPE: inter %d, mc %d, fil %d, mquant %d, mvd %d, cbp %d, tcoeff %d",
611       (mb->mtype & MTYPE_INTER) != 0, (mb->mtype & MTYPE_MC) != 0,
612       (mb->mtype & MTYPE_FIL) != 0, (mb->mtype & MTYPE_MQUANT) != 0,
613       (mb->mtype & MTYPE_MVD) != 0, (mb->mtype & MTYPE_CBP) != 0,
614       (mb->mtype & MTYPE_TCOEFF) != 0);
615
616   if (mb->mtype & MTYPE_MQUANT) {
617     GET_BITS (br, &mb->quant, MQUANT_LEN);
618     GST_TRACE_OBJECT (pay, "MQUANT: %d", mb->quant);
619   }
620
621   if (mb->mtype & MTYPE_MVD) {
622     gint i, pmv[2], mv[2];
623
624     if (mb->mba == 1 || mb->mba == 12 || mb->mba == 23 || mba_diff != 1 ||
625         (prev->mtype & MTYPE_INTER) == 0) {
626       pmv[0] = 0;
627       pmv[1] = 0;
628     } else {
629       pmv[0] = prev->mvx;
630       pmv[1] = prev->mvy;
631     }
632     for (i = 0; i < 2; i++) {
633       gint mvd1, mvd2;
634       if ((ret = decode_mvd (br, &mvd1, &mvd2)) != PARSE_OK)
635         return ret;
636       if (ABS (pmv[i] + mvd1) <= 15)
637         mv[i] = pmv[i] + mvd1;
638       else
639         mv[i] = pmv[i] + mvd2;
640     }
641     mb->mvx = mv[0];
642     mb->mvy = mv[1];
643   } else {
644     mb->mvx = 0;
645     mb->mvy = 0;
646   }
647
648   if (mb->mtype & MTYPE_CBP) {
649     if ((ret = decode_cbp (br, &cbp)) != PARSE_OK)
650       return ret;
651   }
652
653   /* Block layer */
654   if (mb->mtype & MTYPE_TCOEFF) {
655     gint block;
656     for (block = 0; block < 6; block++) {
657       if (cbp & (1 << (5 - block))) {
658         GST_TRACE_OBJECT (pay, "Decode TCOEFF for block %d", block);
659         if ((ret = decode_tcoeff (br, mb->mtype)) != PARSE_OK)
660           return ret;
661       }
662     }
663   }
664
665   mb->endpos = gst_bit_reader_get_pos (br);
666
667   return ret;
668 }
669
670 /* Parse macroblocks until the next MB that exceeds maxpos. At least one MB is
671  * included even if it exceeds maxpos. Returns endpos of last included MB. */
672 static ParseReturn
673 parse_mb_until_pos (GstRtpH261Pay * pay, GstBitReader * br, Gob * gob,
674     guint * endpos)
675 {
676   ParseReturn ret;
677   gint count = 0;
678   gboolean stop = FALSE;
679   guint maxpos = *endpos;
680   Macroblock mb;
681
682   GST_LOG_OBJECT (pay, "Parse until pos %u, start at pos %u, gobn %d, mba %d",
683       maxpos, gst_bit_reader_get_pos (br), gob->gn, gob->last.mba);
684
685   while (!stop) {
686     ret = parse_mb (pay, br, &gob->last, &mb);
687
688     switch (ret) {
689       case PARSE_OK:
690         if (mb.endpos > maxpos && count > 0) {
691           /* Don't include current MB */
692           stop = TRUE;
693         } else {
694           /* Update to include current MB */
695           *endpos = mb.endpos;
696           gob->last = mb;
697           count++;
698         }
699         break;
700
701       case PARSE_END_OF_FRAME:
702         *endpos = gst_bit_reader_get_pos (br);
703         GST_DEBUG_OBJECT (pay, "End of frame at pos %u (last GOBN %d MBA %d)",
704             *endpos, gob->gn, gob->last.mba);
705         stop = TRUE;
706         break;
707
708       case PARSE_END_OF_GOB:
709         /* Note that a GOB can contain nothing, so we may get here on the first
710          * iteration. */
711         *endpos = gob->last.mba == 0 ?
712             gob->startpos : gst_bit_reader_get_pos (br);
713         GST_DEBUG_OBJECT (pay, "End of gob at pos %u (last GOBN %d MBA %d)",
714             *endpos, gob->gn, gob->last.mba);
715         stop = TRUE;
716         break;
717
718       case PARSE_END_OF_BUFFER:
719       case PARSE_ERROR:
720         GST_WARNING_OBJECT (pay, "Failed to parse stream (reason %d)", ret);
721         return ret;
722         break;
723
724       default:
725         g_assert_not_reached ();
726         break;
727     }
728   }
729   gob->last.gobn = gob->gn;
730
731   if (ret == PARSE_OK) {
732     GST_DEBUG_OBJECT (pay,
733         "Split GOBN %d after MBA %d (endpos %u, maxpos %u, nextpos %u)",
734         gob->gn, gob->last.mba, *endpos, maxpos, mb.endpos);
735     gst_bit_reader_set_pos (br, *endpos);
736   }
737
738   return ret;
739 }
740
741 static guint
742 bitrange_to_bytes (guint first, guint last)
743 {
744   return (GST_ROUND_UP_8 (last) - GST_ROUND_DOWN_8 (first)) / 8;
745 }
746
747 /* Find next 16-bit GOB start code (0x0001), which may not be byte aligned.
748  * Returns the bit offset of the first bit of GBSC. */
749 static gssize
750 find_gob (GstRtpH261Pay * pay, const guint8 * data, guint size, guint pos)
751 {
752   gssize ret = -1;
753   guint offset;
754
755   GST_LOG_OBJECT (pay, "Search for GOB from pos %u", pos);
756
757   for (offset = pos / 8; offset < size - 1; offset++) {
758     if (data[offset] == 0x0) {
759       gint msb = g_bit_nth_msf (data[offset + 1], 8);
760       gint lsb = offset > 0 ? g_bit_nth_lsf (data[offset - 1], -1) : 0;
761       if (lsb == -1)
762         lsb = 8;
763       if (msb >= 0 && lsb >= msb) {
764         ret = offset * 8 - msb;
765         GST_LOG_OBJECT (pay, "Found GOB start code at bitpos %"
766             G_GSSIZE_FORMAT " (%02x %02x %02x)", ret,
767             offset > 0 ? data[offset - 1] : 0, data[offset], data[offset + 1]);
768         break;
769       }
770     }
771   }
772
773   return ret;
774 }
775
776 /* Scans after all GOB start codes and initializes the GOB structure with start
777  * and end positions. */
778 static ParseReturn
779 gst_rtp_h261_pay_init_gobs (GstRtpH261Pay * pay, Gob * gobs, gint num_gobs,
780     const guint8 * bits, gint len, guint pos)
781 {
782   gint i;
783
784   for (i = 0; i < num_gobs; i++) {
785     gssize gobpos = find_gob (pay, bits, len, pos);
786     if (gobpos == -1) {
787       GST_WARNING_OBJECT (pay, "Found only %d of %d GOBs", i, num_gobs);
788       return PARSE_ERROR;
789     }
790     GST_LOG_OBJECT (pay, "Found GOB %d at pos %" G_GSSIZE_FORMAT, i, gobpos);
791     pos = gobpos + GBSC_LEN;
792
793     gobs[i].startpos = gobpos;
794     if (i > 0)
795       gobs[i - 1].endpos = gobpos;
796   }
797   gobs[num_gobs - 1].endpos = len * 8;
798
799   return PARSE_OK;
800 }
801
802 static GstFlowReturn
803 gst_rtp_h261_pay_fragment_push (GstRtpH261Pay * pay, GstBuffer * buffer,
804     const guint8 * bits, guint start, guint end,
805     const Macroblock * last_mb_in_previous_packet, gboolean marker)
806 {
807   GstBuffer *outbuf;
808   guint8 *payload;
809   GstRtpH261PayHeader *header;
810   gint nbytes;
811   const Macroblock *last = last_mb_in_previous_packet;
812   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
813
814   nbytes = bitrange_to_bytes (start, end);
815
816   outbuf = gst_rtp_buffer_new_allocate (nbytes +
817       GST_RTP_H261_PAYLOAD_HEADER_LEN, 0, 0);
818   gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
819   payload = gst_rtp_buffer_get_payload (&rtp);
820   header = (GstRtpH261PayHeader *) payload;
821
822   memset (header, 0, GST_RTP_H261_PAYLOAD_HEADER_LEN);
823   header->v = 1;
824   header->sbit = start & 7;
825   header->ebit = (8 - (end & 7)) & 7;
826
827   if (last != NULL && last->mba != 0 && last->mba != 33) {
828     /* NOTE: MVD assumes that we're running on 2's complement architecture */
829     guint mbap = last->mba - 1;
830     header->gobn = last->gobn;
831     header->mbap1 = mbap >> 1;
832     header->mbap2 = mbap & 1;
833     header->quant = last->quant;
834     header->hmvd1 = last->mvx >> 3;
835     header->hmvd2 = last->mvx & 7;
836     header->vmvd = last->mvy;
837   }
838
839   memcpy (payload + GST_RTP_H261_PAYLOAD_HEADER_LEN,
840       bits + GST_ROUND_DOWN_8 (start) / 8, nbytes);
841
842   GST_BUFFER_TIMESTAMP (outbuf) = pay->timestamp;
843   gst_rtp_buffer_set_marker (&rtp, marker);
844   pay->offset = end & 7;
845
846   GST_DEBUG_OBJECT (pay,
847       "Push fragment, bytes %d, sbit %d, ebit %d, gobn %d, mbap %d, marker %d",
848       nbytes, header->sbit, header->ebit, last != NULL ? last->gobn : 0,
849       last != NULL ? MAX (last->mba - 1, 0) : 0, marker);
850
851   gst_rtp_buffer_unmap (&rtp);
852
853   gst_rtp_copy_video_meta (pay, outbuf, buffer);
854
855   return gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD_CAST (pay), outbuf);
856 }
857
858 static GstFlowReturn
859 gst_rtp_h261_packetize_and_push (GstRtpH261Pay * pay, GstBuffer * buffer,
860     const guint8 * bits, gsize len)
861 {
862   GstFlowReturn ret = GST_FLOW_OK;
863   GstBitReader br_;
864   GstBitReader *br = &br_;
865   guint max_payload_size =
866       gst_rtp_buffer_calc_payload_len (GST_RTP_BASE_PAYLOAD_MTU (pay) -
867       GST_RTP_H261_PAYLOAD_HEADER_LEN, 0, 0);
868   guint startpos;
869   gint num_gobs = 0;
870   Gob gobs[MAX_NUM_GOB];
871   Gob *gob;
872   Macroblock last_mb_in_previous_packet = { 0 };
873   gboolean marker;
874   ParseReturn result;
875
876   gst_bit_reader_init (br, bits, len);
877   gst_bit_reader_set_pos (br, pay->offset);
878   startpos = pay->offset;
879
880   if (parse_picture_header (pay, br, &num_gobs) < PARSE_OK) {
881     GST_WARNING_OBJECT (pay, "Failed to parse picture header");
882     goto beach;
883   }
884
885   if (gst_rtp_h261_pay_init_gobs (pay, gobs, num_gobs, bits, len,
886           gst_bit_reader_get_pos (br)) < PARSE_OK)
887     goto beach;
888
889   /* Split, create and push packets */
890   gob = gobs;
891   marker = FALSE;
892   while (marker == FALSE && ret == GST_FLOW_OK) {
893     guint endpos;
894
895     /* Check if there is wrap around because of extremely high MTU */
896     endpos = GST_ROUND_DOWN_8 (startpos) + max_payload_size * 8;
897     if (endpos < startpos)
898       endpos = G_MAXUINT;
899
900     GST_LOG_OBJECT (pay, "Next packet startpos %u maxpos %u", startpos, endpos);
901
902     /* Find the last GOB that does not completely fit in packet */
903     for (; gob < &gobs[num_gobs - 1]; gob++) {
904       if (bitrange_to_bytes (startpos, gob->endpos) > max_payload_size) {
905         GST_LOG_OBJECT (pay, "Split gob (start %u, end %u)",
906             gob->startpos, gob->endpos);
907         break;
908       }
909     }
910
911     if (startpos <= gob->startpos) {
912       /* Fast-forward until start of GOB */
913       gst_bit_reader_set_pos (br, gob->startpos);
914       if (parse_gob_header (pay, br, gob) < PARSE_OK) {
915         GST_WARNING_OBJECT (pay, "Failed to parse GOB header");
916         goto beach;
917       }
918       gob->last.mba = 0;
919       gob->last.gobn = gob->gn;
920       gob->last.quant = gob->gquant;
921     }
922
923     /* Parse MBs to find position where to split. Can only be done on after MB
924      * or at GOB boundary. */
925     result = parse_mb_until_pos (pay, br, gob, &endpos);
926     if (result < PARSE_OK)
927       goto beach;
928
929     marker = result == PARSE_END_OF_FRAME;
930     ret = gst_rtp_h261_pay_fragment_push (pay, buffer, bits, startpos, endpos,
931         &last_mb_in_previous_packet, marker);
932
933     last_mb_in_previous_packet = gob->last;
934     if (endpos == gob->endpos)
935       gob++;
936     startpos = endpos;
937   }
938
939 beach:
940   return ret;
941 }
942
943 /* Shift buffer to packetize a continuous stream of bits (not bytes). Some
944  * payloaders/decoders are very picky about correct sbit/ebit for frames. */
945 static guint8 *
946 gst_rtp_h261_pay_shift_buffer (GstRtpH261Pay * pay, const guint8 * data,
947     gsize size, gint offset, gsize * newsize)
948 {
949   /* In order to read variable length codes at the very end of the buffer
950    * without peeking into possibly unallocated data, we pad with extra 0's
951    * which will generate an invalid code at the end of the buffer. */
952   guint pad = 4;
953   gsize allocsize = size + pad;
954   guint8 *bits = g_malloc (allocsize);
955   gint i;
956
957   if (offset == 0) {
958     memcpy (bits, data, size);
959     *newsize = size;
960   } else if (offset > 0) {
961     bits[0] = 0;
962     for (i = 0; i < size; i++) {
963       bits[i] |= data[i] >> offset;
964       bits[i + 1] = data[i] << (8 - offset);
965     }
966     *newsize = size + 1;
967   } else {
968     gint shift = -offset;
969     for (i = 0; i < size - 1; i++)
970       bits[i] = (data[i] << shift) | (data[i + 1] >> (8 - shift));
971     bits[i] = data[i] << shift;
972     *newsize = size;
973   }
974   for (i = *newsize; i < allocsize; i++)
975     bits[i] = 0;
976
977   return bits;
978 }
979
980 static GstFlowReturn
981 gst_rtp_h261_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
982 {
983   GstFlowReturn ret = GST_FLOW_OK;
984   GstRtpH261Pay *pay = GST_RTP_H261_PAY (payload);
985   gsize len;
986   guint8 *bits;
987   gint psc_offset, shift;
988   GstMapInfo map;
989
990   GST_DEBUG_OBJECT (pay, "Handle buffer of size %" G_GSIZE_FORMAT,
991       gst_buffer_get_size (buffer));
992
993   pay->timestamp = GST_BUFFER_TIMESTAMP (buffer);
994
995   if (!gst_buffer_map (buffer, &map, GST_MAP_READ) || !map.data) {
996     GST_WARNING_OBJECT (pay, "Failed to map buffer");
997     return GST_FLOW_ERROR;
998   }
999
1000   psc_offset = find_picture_header_offset (map.data, map.size);
1001   if (psc_offset < 0) {
1002     GST_WARNING_OBJECT (pay, "Failed to find picture header offset");
1003     goto beach;
1004   } else {
1005     GST_DEBUG_OBJECT (pay, "Picture header offset: %d", psc_offset);
1006   }
1007
1008   shift = pay->offset - psc_offset;
1009   bits = gst_rtp_h261_pay_shift_buffer (pay, map.data, map.size, shift, &len);
1010   ret = gst_rtp_h261_packetize_and_push (pay, buffer, bits, len);
1011   g_free (bits);
1012
1013 beach:
1014   gst_buffer_unmap (buffer, &map);
1015   gst_buffer_unref (buffer);
1016   return ret;
1017 }
1018
1019
1020 static gboolean
1021 gst_rtp_h261_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
1022 {
1023   gboolean res;
1024
1025   gst_rtp_base_payload_set_options (payload, "video",
1026       payload->pt != GST_RTP_PAYLOAD_H261, "H261", 90000);
1027   res = gst_rtp_base_payload_set_outcaps (payload, NULL);
1028
1029   return res;
1030 }
1031
1032 static void
1033 gst_rtp_h261_pay_init (GstRtpH261Pay * pay)
1034 {
1035   GstRTPBasePayload *payload = GST_RTP_BASE_PAYLOAD (pay);
1036   payload->pt = GST_RTP_PAYLOAD_H261;
1037   pay->offset = 0;
1038 }
1039
1040 static void
1041 gst_rtp_h261_pay_class_init (GstRtpH261PayClass * klass)
1042 {
1043   GstElementClass *element_class;
1044   GstRTPBasePayloadClass *gstrtpbasepayload_class;
1045
1046   element_class = GST_ELEMENT_CLASS (klass);
1047   gstrtpbasepayload_class = GST_RTP_BASE_PAYLOAD_CLASS (klass);
1048
1049   gst_element_class_add_static_pad_template (element_class,
1050       &gst_rtp_h261_pay_src_template);
1051   gst_element_class_add_static_pad_template (element_class,
1052       &gst_rtp_h261_pay_sink_template);
1053
1054   gst_element_class_set_static_metadata (element_class,
1055       "RTP H261 packet payloader", "Codec/Payloader/Network/RTP",
1056       "Payload-encodes H261 video in RTP packets (RFC 4587)",
1057       "Stian Selnes <stian@pexip.com>");
1058
1059   gstrtpbasepayload_class->set_caps = gst_rtp_h261_pay_setcaps;
1060   gstrtpbasepayload_class->handle_buffer = gst_rtp_h261_pay_handle_buffer;
1061
1062   GST_DEBUG_CATEGORY_INIT (rtph261pay_debug, "rtph261pay", 0,
1063       "H261 RTP Payloader");
1064 }
1065
1066 gboolean
1067 gst_rtp_h261_pay_plugin_init (GstPlugin * plugin)
1068 {
1069   return gst_element_register (plugin, "rtph261pay",
1070       GST_RANK_SECONDARY, GST_TYPE_RTP_H261_PAY);
1071 }