2 * Copyright (C) <2014> Stian Selnes <stian@pexip.com>
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.
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.
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.
22 * SECTION:element-rtph261pay
24 * @see_also: rtph261depay
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
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
39 * ## Example launch line
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.
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>
58 GST_DEBUG_CATEGORY_STATIC (rtph261pay_debug);
59 #define GST_CAT_DEFAULT (rtph261pay_debug)
61 #define GST_RTP_HEADER_LEN 12
62 #define GST_RTP_H261_PAYLOAD_HEADER_LEN 4
64 static GstStaticPadTemplate gst_rtp_h261_pay_sink_template =
65 GST_STATIC_PAD_TEMPLATE ("sink",
68 GST_STATIC_CAPS ("video/x-h261")
71 static GstStaticPadTemplate gst_rtp_h261_pay_src_template =
72 GST_STATIC_PAD_TEMPLATE ("src",
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\"; "
80 "media = (string) \"video\", "
81 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
82 "clock-rate = (int) 90000, " "encoding-name = (string) \"H261\"")
85 G_DEFINE_TYPE (GstRtpH261Pay, gst_rtp_h261_pay, GST_TYPE_RTP_BASE_PAYLOAD);
86 #define parent_class gst_rtp_h261_pay_parent_class
117 #define MAX_NUM_GOB 12
121 PARSE_END_OF_BUFFER = -2,
129 #define SKIP_BITS(br,nbits) G_STMT_START { \
130 if (!gst_bit_reader_skip (br, nbits)) \
131 return PARSE_END_OF_BUFFER; \
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; \
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); \
145 #define MBA_STUFFING 34
146 #define MBA_START_CODE 35
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},
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)
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},
202 MTYPE_INTER | MTYPE_MC | MTYPE_FIL | MTYPE_MVD | MTYPE_CBP |
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},
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},
212 MTYPE_INTER | MTYPE_MC | MTYPE_MVD | MTYPE_CBP | MTYPE_TCOEFF},
213 {0x0080, 0xff80, 9, MTYPE_INTER | MTYPE_MC | MTYPE_MVD},
215 MTYPE_INTER | MTYPE_MC | MTYPE_MQUANT | MTYPE_MVD | MTYPE_CBP |
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},
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},
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},
399 decode_mba (GstBitReader * br, gint * mba)
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];
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)
418 } while (*mba == MBA_STUFFING);
420 /* 0x0 indicates end of frame since we appended 0-bytes */
422 return PARSE_END_OF_FRAME;
428 decode_mtype (GstBitReader * br, guint * mtype)
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];
446 decode_mvd (GstBitReader * br, gint * mvd1, gint * mvd2)
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];
465 decode_cbp (GstBitReader * br, guint * cbp)
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];
483 decode_tcoeff (GstBitReader * br, guint mtype)
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);
495 GST_TRACE ("tcoeff first inter special");
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. */
501 /* Intra, first coeff is fixed 8-bit */
502 GST_TRACE ("tcoeff first intra special");
506 /* Block must end with 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],
514 SKIP_BITS (br, tcoeff_table[i][2]);
515 if (tcoeff_table[i][3] == TCOEFF_EOB) {
517 } else if (tcoeff_table[i][3] == TCOEFF_ESC) {
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));
523 SKIP_BITS (br, 6 + 8);
529 /* No matching VLC */
537 find_picture_header_offset (const guint8 * data, gsize size)
545 val = GST_READ_UINT32_BE (data);
546 for (i = 0; i < 8; i++) {
547 if ((val >> (12 - i)) == 0x10)
555 parse_picture_header (GstRtpH261Pay * pay, GstBitReader * br, gint * num_gob)
559 GET_BITS (br, &val, PSC_LEN);
562 SKIP_BITS (br, TR_LEN);
563 GET_BITS (br, &val, PTYPE_LEN);
564 *num_gob = (val & 0x04) == 0 ? 3 : 12;
570 parse_gob_header (GstRtpH261Pay * pay, GstBitReader * br, Gob * gob)
574 GET_BITS (br, &val, GBSC_LEN);
577 GET_BITS (br, &gob->gn, GN_LEN);
578 GST_TRACE_OBJECT (pay, "Parsing GOB %d", gob->gn);
580 GET_BITS (br, &gob->gquant, GQUANT_LEN);
581 GST_TRACE_OBJECT (pay, "GQUANT %d", gob->gquant);
582 GET_BITS (br, &val, GEI_LEN);
584 SKIP_BITS (br, GSPARE_LEN);
585 GET_BITS (br, &val, GEI_LEN);
592 parse_mb (GstRtpH261Pay * pay, GstBitReader * br, const Macroblock * prev,
600 mb->quant = prev->quant;
602 if ((ret = decode_mba (br, &mba_diff)) != PARSE_OK)
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);
607 if ((ret = decode_mtype (br, &mb->mtype)) != PARSE_OK)
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);
616 if (mb->mtype & MTYPE_MQUANT) {
617 GET_BITS (br, &mb->quant, MQUANT_LEN);
618 GST_TRACE_OBJECT (pay, "MQUANT: %d", mb->quant);
621 if (mb->mtype & MTYPE_MVD) {
622 gint i, pmv[2], mv[2];
624 if (mb->mba == 1 || mb->mba == 12 || mb->mba == 23 || mba_diff != 1 ||
625 (prev->mtype & MTYPE_INTER) == 0) {
632 for (i = 0; i < 2; i++) {
634 if ((ret = decode_mvd (br, &mvd1, &mvd2)) != PARSE_OK)
636 if (ABS (pmv[i] + mvd1) <= 15)
637 mv[i] = pmv[i] + mvd1;
639 mv[i] = pmv[i] + mvd2;
648 if (mb->mtype & MTYPE_CBP) {
649 if ((ret = decode_cbp (br, &cbp)) != PARSE_OK)
654 if (mb->mtype & MTYPE_TCOEFF) {
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)
665 mb->endpos = gst_bit_reader_get_pos (br);
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. */
673 parse_mb_until_pos (GstRtpH261Pay * pay, GstBitReader * br, Gob * gob,
678 gboolean stop = FALSE;
679 guint maxpos = *endpos;
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);
686 ret = parse_mb (pay, br, &gob->last, &mb);
690 if (mb.endpos > maxpos && count > 0) {
691 /* Don't include current MB */
694 /* Update to include current MB */
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);
708 case PARSE_END_OF_GOB:
709 /* Note that a GOB can contain nothing, so we may get here on the first
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);
718 case PARSE_END_OF_BUFFER:
720 GST_WARNING_OBJECT (pay, "Failed to parse stream (reason %d)", ret);
725 g_assert_not_reached ();
729 gob->last.gobn = gob->gn;
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);
742 bitrange_to_bytes (guint first, guint last)
744 return (GST_ROUND_UP_8 (last) - GST_ROUND_DOWN_8 (first)) / 8;
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. */
750 find_gob (GstRtpH261Pay * pay, const guint8 * data, guint size, guint pos)
755 GST_LOG_OBJECT (pay, "Search for GOB from pos %u", pos);
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;
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]);
776 /* Scans after all GOB start codes and initializes the GOB structure with start
777 * and end positions. */
779 gst_rtp_h261_pay_init_gobs (GstRtpH261Pay * pay, Gob * gobs, gint num_gobs,
780 const guint8 * bits, gint len, guint pos)
784 for (i = 0; i < num_gobs; i++) {
785 gssize gobpos = find_gob (pay, bits, len, pos);
787 GST_WARNING_OBJECT (pay, "Found only %d of %d GOBs", i, num_gobs);
790 GST_LOG_OBJECT (pay, "Found GOB %d at pos %" G_GSSIZE_FORMAT, i, gobpos);
791 pos = gobpos + GBSC_LEN;
793 gobs[i].startpos = gobpos;
795 gobs[i - 1].endpos = gobpos;
797 gobs[num_gobs - 1].endpos = len * 8;
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)
809 GstRtpH261PayHeader *header;
811 const Macroblock *last = last_mb_in_previous_packet;
812 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
814 nbytes = bitrange_to_bytes (start, end);
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;
822 memset (header, 0, GST_RTP_H261_PAYLOAD_HEADER_LEN);
824 header->sbit = start & 7;
825 header->ebit = (8 - (end & 7)) & 7;
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;
839 memcpy (payload + GST_RTP_H261_PAYLOAD_HEADER_LEN,
840 bits + GST_ROUND_DOWN_8 (start) / 8, nbytes);
842 GST_BUFFER_TIMESTAMP (outbuf) = pay->timestamp;
843 gst_rtp_buffer_set_marker (&rtp, marker);
844 pay->offset = end & 7;
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);
851 gst_rtp_buffer_unmap (&rtp);
853 gst_rtp_copy_video_meta (pay, outbuf, buffer);
855 return gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD_CAST (pay), outbuf);
859 gst_rtp_h261_packetize_and_push (GstRtpH261Pay * pay, GstBuffer * buffer,
860 const guint8 * bits, gsize len)
862 GstFlowReturn ret = GST_FLOW_OK;
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);
870 Gob gobs[MAX_NUM_GOB];
872 Macroblock last_mb_in_previous_packet = { 0 };
876 gst_bit_reader_init (br, bits, len);
877 gst_bit_reader_set_pos (br, pay->offset);
878 startpos = pay->offset;
880 if (parse_picture_header (pay, br, &num_gobs) < PARSE_OK) {
881 GST_WARNING_OBJECT (pay, "Failed to parse picture header");
885 if (gst_rtp_h261_pay_init_gobs (pay, gobs, num_gobs, bits, len,
886 gst_bit_reader_get_pos (br)) < PARSE_OK)
889 /* Split, create and push packets */
892 while (marker == FALSE && ret == GST_FLOW_OK) {
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)
900 GST_LOG_OBJECT (pay, "Next packet startpos %u maxpos %u", startpos, endpos);
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);
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");
919 gob->last.gobn = gob->gn;
920 gob->last.quant = gob->gquant;
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)
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);
933 last_mb_in_previous_packet = gob->last;
934 if (endpos == gob->endpos)
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. */
946 gst_rtp_h261_pay_shift_buffer (GstRtpH261Pay * pay, const guint8 * data,
947 gsize size, gint offset, gsize * newsize)
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. */
953 gsize allocsize = size + pad;
954 guint8 *bits = g_malloc (allocsize);
958 memcpy (bits, data, size);
960 } else if (offset > 0) {
962 for (i = 0; i < size; i++) {
963 bits[i] |= data[i] >> offset;
964 bits[i + 1] = data[i] << (8 - offset);
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;
974 for (i = *newsize; i < allocsize; i++)
981 gst_rtp_h261_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
983 GstFlowReturn ret = GST_FLOW_OK;
984 GstRtpH261Pay *pay = GST_RTP_H261_PAY (payload);
987 gint psc_offset, shift;
990 GST_DEBUG_OBJECT (pay, "Handle buffer of size %" G_GSIZE_FORMAT,
991 gst_buffer_get_size (buffer));
993 pay->timestamp = GST_BUFFER_TIMESTAMP (buffer);
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;
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");
1005 GST_DEBUG_OBJECT (pay, "Picture header offset: %d", psc_offset);
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);
1014 gst_buffer_unmap (buffer, &map);
1015 gst_buffer_unref (buffer);
1021 gst_rtp_h261_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
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);
1033 gst_rtp_h261_pay_init (GstRtpH261Pay * pay)
1035 GstRTPBasePayload *payload = GST_RTP_BASE_PAYLOAD (pay);
1036 payload->pt = GST_RTP_PAYLOAD_H261;
1041 gst_rtp_h261_pay_class_init (GstRtpH261PayClass * klass)
1043 GstElementClass *element_class;
1044 GstRTPBasePayloadClass *gstrtpbasepayload_class;
1046 element_class = GST_ELEMENT_CLASS (klass);
1047 gstrtpbasepayload_class = GST_RTP_BASE_PAYLOAD_CLASS (klass);
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);
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>");
1059 gstrtpbasepayload_class->set_caps = gst_rtp_h261_pay_setcaps;
1060 gstrtpbasepayload_class->handle_buffer = gst_rtp_h261_pay_handle_buffer;
1062 GST_DEBUG_CATEGORY_INIT (rtph261pay_debug, "rtph261pay", 0,
1063 "H261 RTP Payloader");
1067 gst_rtp_h261_pay_plugin_init (GstPlugin * plugin)
1069 return gst_element_register (plugin, "rtph261pay",
1070 GST_RANK_SECONDARY, GST_TYPE_RTP_H261_PAY);