vp8rtpfmt.c 34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
vp8rtpfmt.c

mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2011-2012 Belledonne Communications, Grenoble, France

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


#include "vp8rtpfmt.h"

25
#include <limits.h>
26

jehan's avatar
jehan committed
27
/*#define VP8RTPFMT_DEBUG*/
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#if (defined(__GNUC__) && __GNUC__) || defined(__SUNPRO_C)
#define DECLARE_ALIGNED(n,typ,val)  typ val __attribute__ ((aligned (n)))
#elif defined(_MSC_VER)
#define DECLARE_ALIGNED(n,typ,val)  __declspec(align(n)) typ val
#else
#warning No alignment directives known for this compiler.
#define DECLARE_ALIGNED(n,typ,val)  typ val
#endif

typedef size_t VP8_BD_VALUE;

#define VP8_BD_VALUE_SIZE ((int)sizeof(VP8_BD_VALUE)*CHAR_BIT)

#define VP8_LOTS_OF_BITS (0x40000000)

#define MB_LVL_MAX 2
#define MB_FEATURE_TREE_PROBS 3
#define MAX_MB_SEGMENTS 4
#define MAX_REF_LF_DELTAS 4
#define MAX_MODE_LF_DELTAS 4

typedef void (vp8_decrypt_cb)(void *decrypt_state, const unsigned char *input,
                              unsigned char *output, int count);

typedef struct
{
    const unsigned char *user_buffer_end;
    const unsigned char *user_buffer;
    VP8_BD_VALUE         value;
    int                  count;
    unsigned int         range;
    vp8_decrypt_cb      *decrypt_cb;
    void                *decrypt_state;
} BOOL_DECODER;

typedef unsigned char vp8_prob;
#define vp8_prob_half ( (vp8_prob) 128)
#define vp8_read vp8dx_decode_bool
#define vp8_read_literal vp8_decode_value
#define vp8_read_bit(R) vp8_read(R, vp8_prob_half)

Simon Morlat's avatar
Simon Morlat committed
70
DECLARE_ALIGNED(16, static const unsigned char, vp8_norm[256]) =
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
{
    0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

static const int vp8_mb_feature_data_bits[MB_LVL_MAX] = {7, 6};

static void vp8dx_bool_decoder_fill(BOOL_DECODER *br)
{
    const unsigned char *bufptr = br->user_buffer;
    VP8_BD_VALUE value = br->value;
    int count = br->count;
    int shift = VP8_BD_VALUE_SIZE - 8 - (count + 8);
    size_t bytes_left = br->user_buffer_end - bufptr;
    size_t bits_left = bytes_left * CHAR_BIT;
    int x = (int)(shift + CHAR_BIT - bits_left);
    int loop_end = 0;
    unsigned char decrypted[sizeof(VP8_BD_VALUE) + 1];

    if (br->decrypt_cb) {
        size_t n = bytes_left > sizeof(decrypted) ? sizeof(decrypted) : bytes_left;
        br->decrypt_cb(br->decrypt_state, bufptr, decrypted, (int)n);
        bufptr = decrypted;
    }

    if(x >= 0)
    {
        count += VP8_LOTS_OF_BITS;
        loop_end = x;
    }

    if (x < 0 || bits_left)
    {
        while(shift >= loop_end)
        {
            count += CHAR_BIT;
            value |= (VP8_BD_VALUE)*bufptr << shift;
            ++bufptr;
            ++br->user_buffer;
            shift -= CHAR_BIT;
        }
    }

    br->value = value;
    br->count = count;
}

static int vp8dx_start_decode(BOOL_DECODER *br,
                       const unsigned char *source,
                       unsigned int source_sz,
                       vp8_decrypt_cb *decrypt_cb,
                       void *decrypt_state)
{
    br->user_buffer_end = source+source_sz;
    br->user_buffer     = source;
    br->value    = 0;
    br->count    = -8;
    br->range    = 255;
    br->decrypt_cb = decrypt_cb;
    br->decrypt_state = decrypt_state;

    if (source_sz && !source)
        return 1;

    /* Populate the buffer */
    vp8dx_bool_decoder_fill(br);

    return 0;
}

static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) {
    unsigned int bit = 0;
    VP8_BD_VALUE value;
    unsigned int split;
    VP8_BD_VALUE bigsplit;
    int count;
    unsigned int range;

    split = 1 + (((br->range - 1) * probability) >> 8);

    if(br->count < 0)
        vp8dx_bool_decoder_fill(br);

    value = br->value;
    count = br->count;

    bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);

    range = split;

    if (value >= bigsplit)
    {
        range = br->range - split;
        value = value - bigsplit;
        bit = 1;
    }

    {
        register unsigned int shift = vp8_norm[range];
        range <<= shift;
        value <<= shift;
        count -= shift;
    }
    br->value = value;
    br->count = count;
    br->range = range;

    return bit;
}

static int vp8_decode_value(BOOL_DECODER *br, int bits)
{
    int z = 0;
    int bit;

    for (bit = bits - 1; bit >= 0; bit--)
    {
        z |= (vp8dx_decode_bool(br, 0x80) << bit);
    }

    return z;
}

static mblk_t * concat_packets_of_partition(Vp8RtpFmtPartition *partition) {
	Vp8RtpFmtPacket *packet;
	int nb_packets = ms_list_size(partition->packets_list);
	int i;

	if (partition->m != NULL) return partition->m;
	for (i = 0; i < nb_packets; i++) {
		packet = ms_list_nth_data(partition->packets_list, i);
		if (partition->m == NULL) {
			partition->m = packet->m;
		} else {
			concatb(partition->m, packet->m);
		}
		packet->m = NULL;
	}
	if (partition->m != NULL) {
		msgpullup(partition->m, -1);
	}
	return partition->m;
}

static bool_t parse_frame_header(Vp8RtpFmtFrame *frame) {
	BOOL_DECODER bc;
	mblk_t *m = frame->partitions[0]->m;
	unsigned char *data;
	unsigned char *data_end;
	uint32_t size;
	uint32_t first_partition_length_in_bytes;
	uint32_t partition_size;
	bool_t segmentation_enabled;
	bool_t mode_ref_lf_delta_enabled;
	int i, j;
	int nb_partitions;

	if (m == NULL) {
		m = concat_packets_of_partition(frame->partitions[0]);
	}
	if (m == NULL) return FALSE;
	data = m->b_rptr;
	data_end = m->b_wptr;

	if (msgdsize(m) < 3) return FALSE;
	frame->keyframe = !(data[0] & 1);
	first_partition_length_in_bytes = (data[0] | (data[1] << 8) | (data[2] << 16)) >> 5;
	data += 3;
	if (frame->keyframe) {
		if (msgdsize(m) < 10) return FALSE;
		data += 7;
	}

	size = (unsigned int)(data_end - data);
	if (vp8dx_start_decode(&bc, data, size, NULL, NULL)) return FALSE;
	if (frame->keyframe) {
		(void)vp8_read_bit(&bc); /* color space */
		(void)vp8_read_bit(&bc); /* clamping type */
	}
	segmentation_enabled = vp8_read_bit(&bc);
	if (segmentation_enabled) {
		bool_t update_mb_segmentation_map = (bool_t)vp8_read_bit(&bc);
		bool_t update_mb_segmentation_data = (bool_t)vp8_read_bit(&bc);
		if (update_mb_segmentation_data) {
			(void)vp8_read_bit(&bc); /* segment feature mode */
			for (i = 0; i < MB_LVL_MAX; i++) {
				for (j = 0; j < MAX_MB_SEGMENTS; j++) {
					if (vp8_read_bit(&bc)) {
						(void)vp8_read_literal(&bc, vp8_mb_feature_data_bits[i]);
						(void)vp8_read_bit(&bc); /* sign */
					}
				}
			}
		}
		if (update_mb_segmentation_map) {
			for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) {
				if (vp8_read_bit(&bc))
					(void)vp8_read_literal(&bc, 8);
            }
		}
	}
	(void)vp8_read_bit(&bc); /* filter type */
	(void)vp8_read_literal(&bc, 6); /* filter level */
	(void)vp8_read_literal(&bc, 3); /* sharpness level */
	mode_ref_lf_delta_enabled = (bool_t)vp8_read_bit(&bc);
	if (mode_ref_lf_delta_enabled) {
		bool_t mode_ref_lf_delta_update = (bool_t)vp8_read_bit(&bc);
		if (mode_ref_lf_delta_update) {
			for (i = 0; i < MAX_REF_LF_DELTAS; i++) {
				if (vp8_read_bit(&bc)) {
					(void)vp8_read_literal(&bc, 6);
					(void)vp8_read_bit(&bc); /* sign */
				}
			}
			for (i = 0; i < MAX_MODE_LF_DELTAS; i++) {
				if (vp8_read_bit(&bc)) {
					(void)vp8_read_literal(&bc, 6);
					(void)vp8_read_bit(&bc); /* sign */
				}
			}
		}
	}
	nb_partitions = (1 << vp8_read_literal(&bc, 2));
	if (nb_partitions > 8) return FALSE;
	frame->partitions_info.nb_partitions = nb_partitions;
	partition_size = data + first_partition_length_in_bytes - m->b_rptr + (3 * (nb_partitions - 1));
311
	if (msgdsize(m) < partition_size) return FALSE;
312 313 314 315 316 317 318 319 320 321
	frame->partitions_info.partition_sizes[0] = partition_size;
	for (i = 1; i < nb_partitions; i++) {
		const unsigned char *partition_sizes = data + first_partition_length_in_bytes;
		const unsigned char *partition_size_ptr = partition_sizes + (i - 1) * 3;
		frame->partitions_info.partition_sizes[i] = partition_size_ptr[0] + (partition_size_ptr[1] << 8) + (partition_size_ptr[2] << 16);
	}
	return TRUE;
}


322

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
#ifdef VP8RTPFMT_DEBUG
static uint16_t get_partition_pictureid(Vp8RtpFmtPartition *partition) {
	Vp8RtpFmtPacket *packet = ms_list_nth_data(partition->packets_list, 0);
	return packet->pd->pictureid;
}

static uint8_t get_partition_id(Vp8RtpFmtPartition *partition) {
	Vp8RtpFmtPacket *packet = ms_list_nth_data(partition->packets_list, 0);
	return packet->pd->pid;
}

static int is_partition_non_reference(Vp8RtpFmtPartition *partition) {
	Vp8RtpFmtPacket *packet = ms_list_nth_data(partition->packets_list, 0);
	return packet->pd->non_reference_frame;
}

static uint32_t get_partition_ts(Vp8RtpFmtPartition *partition) {
	return mblk_get_timestamp_info(partition->m);
}

static uint16_t get_frame_pictureid(Vp8RtpFmtFrame *frame) {
	return get_partition_pictureid(ms_list_nth_data(frame->partitions_list, 0));
}

static int is_frame_non_reference(Vp8RtpFmtFrame *frame) {
	return is_partition_non_reference(ms_list_nth_data(frame->partitions_list, 0));
}

static uint32_t get_frame_ts(Vp8RtpFmtFrame *frame) {
	return get_partition_ts(ms_list_nth_data(frame->partitions_list, 0));
}

static void print_packet(void *data) {
	Vp8RtpFmtPacket *packet = (Vp8RtpFmtPacket *)data;
	ms_message("\t\tcseq=%10u\tS=%d\terror=%d",
		packet->extended_cseq, packet->pd->start_of_partition, packet->error);
}

static void print_partition(void *data) {
	Vp8RtpFmtPartition *partition = (Vp8RtpFmtPartition *)data;
363 364
	ms_message("\tpartition [%p]:\tpid=%d\terror=%d",
		partition, get_partition_id(partition), partition->error);
365 366 367 368 369
	ms_list_for_each(partition->packets_list, print_packet);
}

static void print_frame(void *data) {
	Vp8RtpFmtFrame *frame = (Vp8RtpFmtFrame *)data;
370
	ms_message("frame [%p]:\tts=%u\tpictureid=0x%04x\tN=%d\terror=%d",
371
		frame, get_frame_ts(frame), get_frame_pictureid(frame), is_frame_non_reference(frame), frame->error);
372 373 374 375
	ms_list_for_each(frame->partitions_list, print_partition);
}
#endif /* VP8RTPFMT_DEBUG */

376 377
static void free_packet(void *data) {
	Vp8RtpFmtPacket *packet = (Vp8RtpFmtPacket *)data;
378
	if (packet->m != NULL) {
379 380 381 382 383 384
		freemsg(packet->m);
	}
	ms_free(packet->pd);
	ms_free(packet);
}

385
static void free_partition(void *data) {
386
	Vp8RtpFmtPartition *partition = (Vp8RtpFmtPartition *)data;
387
	if ((partition->m != NULL) && (partition->outputted != TRUE)) {
388 389
		freemsg(partition->m);
	}
390
	ms_list_for_each(partition->packets_list, free_packet);
391 392 393 394
	ms_list_free(partition->packets_list);
	ms_free(partition);
}

395 396 397 398 399 400 401 402 403
static int free_outputted_frame(const void *data, const void *b) {
	Vp8RtpFmtFrame *frame = (Vp8RtpFmtFrame *)data;
	bool_t *outputted = (bool_t *)b;
	bool_t foutputted = frame->outputted;
	if (foutputted == *outputted) ms_free(frame);
	return (foutputted != *outputted);
}

static int free_discarded_frame(const void *data, const void *d) {
404
	Vp8RtpFmtFrame *frame = (Vp8RtpFmtFrame *)data;
405 406 407 408
	bool_t *discarded = (bool_t *)d;
	bool_t fdiscarded = frame->discarded;
	if (fdiscarded == *discarded) ms_free(frame);
	return (fdiscarded != *discarded);
409 410
}

411 412 413 414 415 416
static int cseq_compare(const void *d1, const void *d2) {
	Vp8RtpFmtPacket *p1 = (Vp8RtpFmtPacket *)d1;
	Vp8RtpFmtPacket *p2 = (Vp8RtpFmtPacket *)d2;
	return (p1->extended_cseq > p2->extended_cseq);
}

417 418
static MSVideoSize get_size_from_key_frame(Vp8RtpFmtFrame *frame) {
	MSVideoSize vs;
419
	Vp8RtpFmtPartition *partition = frame->partitions[0];
420 421 422 423 424 425 426 427 428 429
	vs.width = ((partition->m->b_rptr[7] << 8) | (partition->m->b_rptr[6])) & 0x3FFF;
	vs.height = ((partition->m->b_rptr[9] << 8) | (partition->m->b_rptr[8])) & 0x3FFF;
	return vs;
}

static void send_pli(Vp8RtpFmtUnpackerCtx *ctx) {
	if (ctx->avpf_enabled == TRUE) {
		ms_filter_notify_no_arg(ctx->filter, MS_VIDEO_DECODER_SEND_PLI);
	} else {
		ms_filter_notify_no_arg(ctx->filter, MS_VIDEO_DECODER_DECODING_ERRORS);
430
		ctx->error_notified = TRUE;
431 432 433
	}
}

434
static void send_sli(Vp8RtpFmtUnpackerCtx *ctx, Vp8RtpFmtFrame *frame) {
435
	if (ctx->avpf_enabled == TRUE) {
436
		if (frame->pictureid_present == TRUE) {
437 438 439
			MSVideoCodecSLI sli;
			sli.first = 0;
			sli.number = (ctx->video_size.width * ctx->video_size.height) / (16 * 16);
440
			sli.picture_id = frame->pictureid & 0x3F;
441
			ms_filter_notify(ctx->filter, MS_VIDEO_DECODER_SEND_SLI, &sli);
442 443
		} else {
			send_pli(ctx);
444 445 446
		}
	} else {
		ms_filter_notify_no_arg(ctx->filter, MS_VIDEO_DECODER_DECODING_ERRORS);
447
		ctx->error_notified = TRUE;
448 449 450 451 452
	}
}

static void send_rpsi_if_reference_frame(Vp8RtpFmtUnpackerCtx *ctx, Vp8RtpFmtFrame *frame) {
	if (ctx->avpf_enabled == TRUE) {
453
		if (frame->reference && frame->pictureid_present) {
454 455 456
			MSVideoCodecRPSI rpsi;
			uint16_t picture_id16;
			uint8_t picture_id8;
457 458
			if (frame->pictureid & 0x8000) {
				picture_id16 = htons(frame->pictureid);
459 460 461
				rpsi.bit_string = (uint8_t *)&picture_id16;
				rpsi.bit_string_len = 16;
			} else {
462
				picture_id8 = frame->pictureid & 0xFF;
463 464 465 466 467 468 469 470
				rpsi.bit_string = (uint8_t *)&picture_id8;
				rpsi.bit_string_len = 8;
			}
			ms_filter_notify(ctx->filter, MS_VIDEO_DECODER_SEND_RPSI, &rpsi);
		}
	}
}

471 472 473 474 475
static Vp8RtpFmtPartition * create_empty_partition(void) {
	Vp8RtpFmtPartition *partition = ms_new0(Vp8RtpFmtPartition, 1);
	partition->m = allocb(0, 0);
	return partition;
}
476

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
static void add_packet_to_frame(Vp8RtpFmtFrame *frame, Vp8RtpFmtPacket *packet) {
	uint8_t pid = packet->pd->pid;
	Vp8RtpFmtPartition *partition = frame->partitions[pid];
	if (!packet->pd->non_reference_frame) {
		frame->reference = TRUE;
	}
	if (packet->pd->pictureid_present) {
		frame->pictureid_present = TRUE;
		frame->pictureid = packet->pd->pictureid;
	}
	if (partition == NULL) {
		partition = frame->partitions[pid] = ms_new0(Vp8RtpFmtPartition, 1);
		partition->has_start = packet->pd->start_of_partition;
	}
	if (mblk_get_marker_info(packet->m)) {
		partition->has_marker = TRUE;
493
	}
494 495
	partition->packets_list = ms_list_append(partition->packets_list, (void *)packet);
	partition->size += msgdsize(packet->m);
496 497
}

498
static void generate_frame_partitions_list(Vp8RtpFmtFrame *frame, MSList *packets_list) {
499 500
	Vp8RtpFmtPacket *packet = NULL;
	int nb_packets = ms_list_size(packets_list);
501 502
	int i;

503 504 505 506 507 508 509 510
	frame->unnumbered_partitions = TRUE;
	for (i = 0; i < nb_packets; i++) {
		packet = (Vp8RtpFmtPacket *)ms_list_nth_data(packets_list, i);
		if (packet->pd->pid != 0) {
			frame->unnumbered_partitions = FALSE;
			break;
		}
	}
511
	for (i = 0; i < nb_packets; i++) {
512 513
		packet = (Vp8RtpFmtPacket *)ms_list_nth_data(packets_list, i);
		if (packet->error == Vp8RtpFmtOk) {
514
			add_packet_to_frame(frame, packet);
515
		} else {
516 517
			/* Malformed packet, ignore it. */
			free_packet(packet);
518
		}
519
	}
520 521
}

522 523
static void mark_frame_as_invalid(Vp8RtpFmtUnpackerCtx *ctx, Vp8RtpFmtFrame *frame) {
	frame->error = Vp8RtpFmtInvalidFrame;
524 525
	/*even if freeze_on_error is FALSE, if avpf is enabled it is preferable not to output an invalid frame.*/
	if (ctx->freeze_on_error || ctx->avpf_enabled) {
526 527
		ctx->waiting_for_reference_frame = TRUE;
	}
528 529
}

530 531
static void mark_frame_as_incomplete(Vp8RtpFmtUnpackerCtx *ctx, Vp8RtpFmtFrame *frame, uint8_t idx) {
	frame->error = Vp8RtpFmtIncompleteFrame;
532
	if (ctx->freeze_on_error == TRUE) {
533
		ctx->waiting_for_reference_frame = TRUE;
534 535 536
	}
}

537
static void check_frame_partitions_list(Vp8RtpFmtUnpackerCtx *ctx, Vp8RtpFmtFrame *frame) {
538
	Vp8RtpFmtPacket *packet;
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
	Vp8RtpFmtPartition *partition;
	uint32_t last_cseq;
	int i;
	bool_t gap = FALSE;

	if (frame->partitions[0] == NULL) {
		mark_frame_as_invalid(ctx, frame);
		return;
	}
	if ((frame->partitions[0] != NULL) && (!frame->partitions[0]->has_start)) {
		mark_frame_as_invalid(ctx, frame);
		return;
	}
	packet = (Vp8RtpFmtPacket *)ms_list_nth_data(frame->partitions[0]->packets_list, 0);
	last_cseq = packet->extended_cseq;
	for (i = 1; i < ms_list_size(frame->partitions[0]->packets_list); i++) {
		packet = (Vp8RtpFmtPacket *)ms_list_nth_data(frame->partitions[0]->packets_list, i);
		if (packet->extended_cseq != (last_cseq + 1)) gap = TRUE;
		last_cseq = packet->extended_cseq;
	}
	if (gap == TRUE) {
		mark_frame_as_invalid(ctx, frame);
		return;
	}
	if (parse_frame_header(frame) != TRUE) {
		mark_frame_as_invalid(ctx, frame);
		return;
	}
567

568 569 570
	/* Do not try to perform next checks if the partitions are not numbered. */
	if (frame->unnumbered_partitions == TRUE) return;

571 572 573 574 575 576 577 578 579 580 581
	/* The partition 0 has been validated, check the following ones. */
	for (i = 1; i < frame->partitions_info.nb_partitions; i++) {
		partition = frame->partitions[i];
		if (partition == NULL) {
			mark_frame_as_incomplete(ctx, frame, i);
			continue;
		}
		if (partition->size != frame->partitions_info.partition_sizes[i]) {
			mark_frame_as_incomplete(ctx, frame, i);
			continue;
		}
582 583
	}

584
	/* Check the last partition of the frame. */
585 586 587 588 589 590 591 592 593
	partition = frame->partitions[frame->partitions_info.nb_partitions];
	if ((partition == NULL) || !partition->has_start || !partition->has_marker) {
		mark_frame_as_incomplete(ctx, frame, frame->partitions_info.nb_partitions);
	} else {
		packet = (Vp8RtpFmtPacket *)ms_list_nth_data(partition->packets_list, 0);
		last_cseq = packet->extended_cseq;
		for (i = 1; i < ms_list_size(partition->packets_list); i++) {
			packet = (Vp8RtpFmtPacket *)ms_list_nth_data(partition->packets_list, i);
			if (packet->extended_cseq != (last_cseq + 1)) gap = TRUE;
594
			last_cseq = packet->extended_cseq;
595 596 597
		}
		if (gap == TRUE) {
			mark_frame_as_incomplete(ctx, frame, frame->partitions_info.nb_partitions);
598 599
		}
	}
600 601
}

602 603 604 605 606 607 608 609 610
static void notify_frame_error_if_any(Vp8RtpFmtUnpackerCtx *ctx, Vp8RtpFmtFrame *frame) {
	switch (frame->error) {
		case Vp8RtpFmtInvalidFrame:
		case Vp8RtpFmtIncompleteFrame:
			send_sli(ctx, frame);
			break;
		case Vp8RtpFmtOk:
		default:
			break;
611 612 613
	}
}

614 615 616 617 618
static void add_frame(Vp8RtpFmtUnpackerCtx *ctx, MSList **packets_list) {
	Vp8RtpFmtFrame *frame;

	if (ms_list_size(*packets_list) > 0) {
		frame = ms_new0(Vp8RtpFmtFrame, 1);
619 620 621 622
		generate_frame_partitions_list(frame, *packets_list);
		check_frame_partitions_list(ctx, frame);
		notify_frame_error_if_any(ctx, frame);
		ctx->frames_list = ms_list_append(ctx->frames_list, (void *)frame);
623
	}
624 625
	ms_list_free(*packets_list);
	*packets_list = NULL;
626 627 628 629 630
}

static void generate_frames_list(Vp8RtpFmtUnpackerCtx *ctx, MSList *packets_list) {
	Vp8RtpFmtPacket *packet;
	MSList *frame_packets_list = NULL;
631
	int nb_packets;
632
	uint32_t ts;
633 634
	int i;

635 636 637 638 639 640 641 642 643 644 645
	/* If we have some packets from the previous iteration, put them in the frame_packets_list. */
	nb_packets = ms_list_size(ctx->non_processed_packets_list);
	for (i = 0; i < nb_packets; i++) {
		packet = ms_list_nth_data(ctx->non_processed_packets_list, i);
		frame_packets_list = ms_list_append(frame_packets_list, packet);
	}
	ms_list_free(ctx->non_processed_packets_list);
	ctx->non_processed_packets_list = NULL;

	/* Process newly received packets. */
	nb_packets = ms_list_size(packets_list);
646 647 648 649 650 651 652 653
	for (i = 0; i < nb_packets; i++) {
		packet = ms_list_nth_data(packets_list, i);
		ts = mblk_get_timestamp_info(packet->m);

		if ((ctx->initialized_last_ts == TRUE) && (ts != ctx->last_ts)) {
			/* The current packet is from a frame different than the previous one
			* (that apparently is not complete). */
			add_frame(ctx, &frame_packets_list);
654
		}
655 656 657 658 659 660 661 662 663 664 665
		ctx->last_ts = ts;
		ctx->initialized_last_ts = TRUE;

		/* Add the current packet to the current frame. */
		frame_packets_list = ms_list_append(frame_packets_list, packet);

		if (mblk_get_marker_info(packet->m)) {
			/* The current packet is the last of the current frame. */
			add_frame(ctx, &frame_packets_list);
		}
	}
666

667 668 669 670 671
	/* If some packets are left, put them in a list for next iteration. */
	nb_packets = ms_list_size(frame_packets_list);
	for (i = 0; i < nb_packets; i++) {
		packet = ms_list_nth_data(frame_packets_list, i);
		ctx->non_processed_packets_list = ms_list_append(ctx->non_processed_packets_list, packet);
672
	}
673 674 675 676

	if (frame_packets_list != NULL){
		ms_list_free(frame_packets_list);
	}
677 678
}

679 680 681 682 683 684 685 686 687 688
static void output_partition(MSQueue *out, Vp8RtpFmtPartition **partition, bool_t last) {
	if (*partition == NULL) {
		*partition = create_empty_partition();
	}
	concat_packets_of_partition(*partition);
	if ((*partition)->has_marker || (last == TRUE)) {
		mblk_set_marker_info((*partition)->m, 1);
	}
	ms_queue_put(out, (void *)(*partition)->m);
	(*partition)->outputted = TRUE;
689 690
}

691 692 693 694 695 696 697 698 699
static void output_partitions_of_frame(Vp8RtpFmtUnpackerCtx *ctx, MSQueue *out, Vp8RtpFmtFrame *frame) {
	int i;
#ifdef VP8RTPFMT_DEBUG
	if (frame->pictureid_present == TRUE) {
		ms_message("VP8 decoder: Output partitions of frame with pictureID=0x%04x", frame->pictureid);
	}
#endif
	for (i = 0; i <= frame->partitions_info.nb_partitions; i++) {
		output_partition(out, &frame->partitions[i], (i == frame->partitions_info.nb_partitions) ? TRUE : FALSE);
700 701 702
	}
}

703 704
static void output_frame(MSQueue *out, Vp8RtpFmtFrame *frame) {
	Vp8RtpFmtPartition *partition;
705
	mblk_t *om = NULL;
706 707 708
	mblk_t *curm;
	int i;

709 710
	for (i = 0; i <= frame->partitions_info.nb_partitions; i++) {
		partition = frame->partitions[i];
711
		if (partition == NULL) continue;
712 713
		if (om == NULL) {
			om = concat_packets_of_partition(partition);
714 715
			curm = om;
		} else {
716
			curm = concatb(curm, concat_packets_of_partition(partition));
717 718 719
		}
		partition->outputted = TRUE;
	}
720
	if (om != NULL) {
721
		if (om->b_cont) msgpullup(om, -1);
722 723 724
		mblk_set_marker_info(om, 1);
		ms_queue_put(out, (void *)om);
	}
725 726
}

727 728 729 730 731 732 733
static void output_valid_partitions(Vp8RtpFmtUnpackerCtx *ctx, MSQueue *out) {
	Vp8RtpFmtFrame *frame;
	int nb_frames = ms_list_size(ctx->frames_list);
	int i;

	for (i = 0; i < nb_frames; i++) {
		frame = ms_list_nth_data(ctx->frames_list, i);
734 735 736 737 738 739
		switch (frame->error) {
			case Vp8RtpFmtOk:
				if (frame->keyframe == TRUE) {
					ctx->valid_keyframe_received = TRUE;
					ctx->video_size = get_size_from_key_frame(frame);
					ctx->waiting_for_reference_frame = FALSE;
740 741 742 743
					if (ctx->error_notified == TRUE) {
						ms_filter_notify_no_arg(ctx->filter, MS_VIDEO_DECODER_RECOVERED_FROM_ERRORS);
						ctx->error_notified = FALSE;
					}
744 745 746 747 748 749 750 751 752 753 754
				}
				if ((ctx->avpf_enabled == TRUE) && (frame->reference == TRUE)) {
					ctx->waiting_for_reference_frame = FALSE;
				}
				if ((ctx->valid_keyframe_received == TRUE) && (ctx->waiting_for_reference_frame == FALSE)) {
					/* Output the complete valid frame if a reference frame has been received. */
					if (ctx->output_partitions == TRUE) {
						output_partitions_of_frame(ctx, out, frame);
					} else {
						/* Output the full frame in one mblk_t. */
						output_frame(out, frame);
755
					}
756 757
					frame->outputted = TRUE;
					send_rpsi_if_reference_frame(ctx, frame);
758
				} else {
759
					frame->discarded = TRUE;
760
					if (!ctx->valid_keyframe_received) send_pli(ctx);
761 762 763
					if (ctx->waiting_for_reference_frame == TRUE) {
						/* Do not decode frames while we are waiting for a reference frame. */
						if (frame->pictureid_present == TRUE)
764
							ms_warning("VP8 decoder: Drop frame because we are waiting for reference frame: pictureID=%i", (int)frame->pictureid);
765 766
						else
							ms_warning("VP8 decoder: Drop frame because we are waiting for reference frame.");
767
					} else {
768 769
						/* Drop frames until the first keyframe is successfully received. */
						ms_warning("VP8 frame dropped because keyframe has not been received yet.");
770
					}
771 772 773 774 775
				}
				break;
			case Vp8RtpFmtIncompleteFrame:
				if (frame->keyframe == TRUE) {
					/* Incomplete keyframe. */
776
					frame->discarded = TRUE;
777
				} else {
778 779 780
					if ((ctx->output_partitions == TRUE) && (ctx->valid_keyframe_received == TRUE) && (ctx->waiting_for_reference_frame == FALSE)) {
						output_partitions_of_frame(ctx, out, frame);
						frame->outputted = TRUE;
781
					} else {
782 783
						/* Drop the frame for which some partitions are missing/invalid. */
						if (frame->pictureid_present == TRUE)
784
							ms_warning("VP8 frame with some partitions missing/invalid: pictureID=%i", (int)frame->pictureid);
785 786 787 788
						else
							ms_warning("VP8 frame with some partitions missing/invalid.");
						frame->discarded = TRUE;
					}
789
				}
790 791 792
				break;
			default:
				/* Drop the invalid frame. */
793

794
				if (frame->pictureid_present == TRUE)
795
					ms_warning("VP8 invalid frame: pictureID=%i", (int)frame->pictureid);
796 797
				else
					ms_warning("VP8 invalid frame.");
798
				frame->discarded = TRUE;
799
				break;
800 801 802 803
		}
	}
}

804
static void free_partitions_of_frame(void *data) {
805
	Vp8RtpFmtFrame *frame = (Vp8RtpFmtFrame *)data;
806 807 808 809 810 811
	int i;
	for (i = 0; i < 9; i++) {
		if (frame->partitions[i] != NULL) {
			free_partition(frame->partitions[i]);
			frame->partitions[i] = NULL;
		}
812 813 814 815 816
	}
}

static void clean_outputted_partitions(Vp8RtpFmtUnpackerCtx *ctx) {
	bool_t outputted = TRUE;
817
	ms_list_for_each(ctx->frames_list, free_partitions_of_frame);
818 819 820 821 822
	ctx->frames_list = ms_list_remove_custom(ctx->frames_list, free_outputted_frame, &outputted);
}

static void clean_discarded_partitions(Vp8RtpFmtUnpackerCtx *ctx) {
	bool_t discarded = TRUE;
823
	ms_list_for_each(ctx->frames_list, free_partitions_of_frame);
824 825 826
	ctx->frames_list = ms_list_remove_custom(ctx->frames_list, free_discarded_frame, &discarded);
}

827 828
static Vp8RtpFmtErrorCode parse_payload_descriptor(Vp8RtpFmtPacket *packet) {
	uint8_t *h = packet->m->b_rptr;
829
	Vp8RtpFmtPayloadDescriptor *pd = packet->pd;
830 831 832 833
	unsigned int packet_size = packet->m->b_wptr - packet->m->b_rptr;
	uint8_t offset = 0;

	if (packet_size == 0) return Vp8RtpFmtInvalidPayloadDescriptor;
834

835
	memset(pd, 0, sizeof(Vp8RtpFmtPayloadDescriptor));
836 837

	/* Parse mandatory first octet of payload descriptor. */
838 839 840 841 842 843
	if (h[offset] & (1 << 7)) pd->extended_control_bits_present = TRUE;
	if (h[offset] & (1 << 5)) pd->non_reference_frame = TRUE;
	if (h[offset] & (1 << 4)) pd->start_of_partition = TRUE;
	pd->pid = (h[offset] & 0x07);
	offset++;
	if (offset >= packet_size) return Vp8RtpFmtInvalidPayloadDescriptor;
844 845
	/* Parse the first extension octet if needed. */
	if (pd->extended_control_bits_present == TRUE) {
846 847 848 849
		if (h[offset] & (1 << 7)) pd->pictureid_present = TRUE;
		if (h[offset] & (1 << 6)) pd->tl0picidx_present = TRUE;
		if (h[offset] & (1 << 5)) pd->tid_present = TRUE;
		if (h[offset] & (1 << 4)) pd->keyidx_present = TRUE;
850 851 852 853
		if ((pd->tl0picidx_present == TRUE) && (pd->tid_present != TRUE)) {
			/* Invalid payload descriptor. */
			return Vp8RtpFmtInvalidPayloadDescriptor;
		}
854 855
		offset++;
		if (offset >= packet_size) return Vp8RtpFmtInvalidPayloadDescriptor;
856 857 858
	}
	/* Parse the pictureID if needed. */
	if (pd->pictureid_present == TRUE) {
859
		if (h[offset] & (1 << 7)) {
860
			/* The pictureID is 16 bits long. */
861 862 863
			if ((offset + 1) >= packet_size) return Vp8RtpFmtInvalidPayloadDescriptor;
			pd->pictureid = (h[offset] << 8) | h[offset + 1];
			offset += 2;
864 865
		} else {
			/* The pictureId is 8 bits long. */
866 867
			pd->pictureid = h[offset];
			offset++;
868
		}
869
		if (offset >= packet_size) return Vp8RtpFmtInvalidPayloadDescriptor;
870 871 872
	}
	/* Parse the tl0picidx if needed. */
	if (pd->tl0picidx_present == TRUE) {
873 874 875
		pd->tl0picidx = h[offset];
		offset++;
		if (offset >= packet_size) return Vp8RtpFmtInvalidPayloadDescriptor;
876 877 878
	}
	/* Parse the tid and/or keyidx if needed. */
	if (pd->tid_present == TRUE) {
879 880
		pd->tid = (h[offset] & 0xC0) >> 6;
		if (h[offset] & (1 << 5)) pd->layer_sync = TRUE;
881 882
	}
	if (pd->keyidx_present == TRUE) {
883
		pd->keyidx = (h[offset] & 0x1F);
884 885
	}
	if ((pd->tid_present == TRUE) || (pd->keyidx_present == TRUE)) {
886 887
		offset++;
		if (offset >= packet_size) return Vp8RtpFmtInvalidPayloadDescriptor;
888 889
	}

890
	packet->m->b_rptr = &h[offset];
891 892 893 894
	return Vp8RtpFmtOk;
}


895
void vp8rtpfmt_unpacker_init(Vp8RtpFmtUnpackerCtx *ctx, MSFilter *f, bool_t avpf_enabled, bool_t freeze_on_error, bool_t output_partitions) {
896
	ctx->filter = f;
897
	ctx->frames_list = NULL;
898
	ctx->non_processed_packets_list = NULL;
899
	ms_queue_init(&ctx->output_queue);
900
	ctx->avpf_enabled = avpf_enabled;
901
	ctx->freeze_on_error = freeze_on_error;
902 903
	ctx->output_partitions = output_partitions;
	ctx->valid_keyframe_received = FALSE;
904
	ctx->waiting_for_reference_frame = TRUE;
905
	ctx->error_notified = FALSE;
906
	ctx->initialized_last_ts = FALSE;
907 908 909
	ctx->initialized_ref_cseq = FALSE;
}

910
void vp8rtpfmt_unpacker_uninit(Vp8RtpFmtUnpackerCtx *ctx) {
911
	ms_list_free(ctx->frames_list);
912
	ms_list_free(ctx->non_processed_packets_list);
913
	ms_queue_flush(&ctx->output_queue);
914 915
}

916 917
void vp8rtpfmt_unpacker_process(Vp8RtpFmtUnpackerCtx *ctx, MSQueue *inout) {
	MSList *packets_list = NULL;
918 919 920 921
	Vp8RtpFmtPacket *packet;
	mblk_t *m;

#ifdef VP8RTPFMT_DEBUG
922
	ms_message("vp8rtpfmt_unpacker_process:");
923
#endif
924
	while ((m = ms_queue_get(inout)) != NULL) {
925
		packet = ms_new0(Vp8RtpFmtPacket, 1);
926
		packet->m = m;
927 928
		packet->extended_cseq = vp8rtpfmt_unpacker_calc_extended_cseq(ctx, mblk_get_cseq(m));
		packet->pd = ms_new0(Vp8RtpFmtPayloadDescriptor, 1);
929 930 931

		if (m->b_cont) msgpullup(m, -1);
		packet->error = parse_payload_descriptor(packet);
932
		packets_list = ms_list_insert_sorted(packets_list, (void *)packet, cseq_compare);
933 934
	}

935 936
	generate_frames_list(ctx, packets_list);
	ms_list_free(packets_list);
937
#ifdef VP8RTPFMT_DEBUG
938
	ms_list_for_each(ctx->frames_list, print_frame);
939
#endif /* VP8RTPFMT_DEBUG */
940 941 942
	output_valid_partitions(ctx, inout);
	clean_outputted_partitions(ctx);
	clean_discarded_partitions(ctx);
943

944 945 946
	if (ms_list_size(ctx->non_processed_packets_list) == 0) {
		ms_list_free(ctx->non_processed_packets_list);
		ctx->non_processed_packets_list = NULL;
947
	} else {
Simon Morlat's avatar
Simon Morlat committed
948
		ms_debug("VP8 packets are remaining for next iteration of the filter.");
949
	}
950 951
}

952
uint32_t vp8rtpfmt_unpacker_calc_extended_cseq(Vp8RtpFmtUnpackerCtx *ctx, uint16_t cseq) {
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
	uint32_t extended_cseq;
	uint32_t cseq_a;
	uint32_t cseq_b;
	uint32_t diff_a;
	uint32_t diff_b;

	if (ctx->initialized_ref_cseq != TRUE) {
		ctx->ref_cseq = cseq | 0x80000000;
		ctx->initialized_ref_cseq = TRUE;
		extended_cseq = ctx->ref_cseq;
	} else {
		cseq_a = cseq | (ctx->ref_cseq & 0xFFFF0000);
		if (ctx->ref_cseq < cseq_a) {
			cseq_b = cseq_a - 0x00010000;
			diff_a = cseq_a - ctx->ref_cseq;
			diff_b = ctx->ref_cseq - cseq_b;
		} else {
			cseq_b = cseq_a + 0x00010000;
			diff_a = ctx->ref_cseq - cseq_a;
			diff_b = cseq_b - ctx->ref_cseq;
		}
		if (diff_a < diff_b) {
			extended_cseq = cseq_a;
		} else {
			extended_cseq = cseq_b;
		}
		ctx->ref_cseq = extended_cseq;
	}

	return extended_cseq;
}
984 985 986 987 988 989



static void packer_process_frame_part(void *p, void *c) {
	Vp8RtpFmtPacket *packet = (Vp8RtpFmtPacket *)p;
	Vp8RtpFmtPackerCtx *ctx = (Vp8RtpFmtPackerCtx *)c;
Ghislain MARY's avatar
Ghislain MARY committed
990 991
	mblk_t *pdm = NULL;
	mblk_t *dm = NULL;
992 993 994 995
	uint8_t *rptr;
	uint8_t pdsize = 1;
	int max_size = ms_get_payload_max_size();
	int dlen;
996
	bool_t marker_info = mblk_get_marker_info(packet->m);
997 998 999 1000 1001 1002 1003 1004 1005 1006

	/* Calculate the payload descriptor size. */
	if (packet->pd->extended_control_bits_present == TRUE) pdsize++;
	if (packet->pd->pictureid_present == TRUE) {
		pdsize++;
		if (packet->pd->pictureid & 0x8000) pdsize++;
	}
	if (packet->pd->tl0picidx_present == TRUE) pdsize++;
	if ((packet->pd->tid_present == TRUE) || (packet->pd->keyidx_present == TRUE)) pdsize++;

Ghislain MARY's avatar
Ghislain MARY committed
1007
	mblk_set_marker_info(packet->m, FALSE);
1008 1009 1010 1011 1012
	for (rptr = packet->m->b_rptr; rptr < packet->m->b_wptr;) {
		/* Allocate the payload descriptor. */
		pdm = allocb(pdsize, 0);
		memset(pdm->b_wptr, 0, pdsize);
		mblk_set_timestamp_info(pdm, mblk_get_timestamp_info(packet->m));
1013
		mblk_set_marker_info(pdm, FALSE);
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
		/* Fill the mandatory octet of the payload descriptor. */
		if (packet->pd->extended_control_bits_present == TRUE) *pdm->b_wptr |= (1 << 7);
		if (packet->pd->non_reference_frame == TRUE) *pdm->b_wptr |= (1 << 5);
		if (packet->pd->start_of_partition == TRUE) {
			if (packet->m->b_rptr == rptr) *pdm->b_wptr |= (1 << 4);
		}
		*pdm->b_wptr |= (packet->pd->pid & 0x07);
		pdm->b_wptr++;
		/* Fill the extension bit field octet of the payload descriptor. */
		if (packet->pd->extended_control_bits_present == TRUE) {
			if (packet->pd->pictureid_present == TRUE) *pdm->b_wptr |= (1 << 7);
			if (packet->pd->tl0picidx_present == TRUE) *pdm->b_wptr |= (1 << 6);
			if (packet->pd->tid_present == TRUE) *pdm->b_wptr |= (1 << 5);
			if (packet->pd->keyidx_present == TRUE) *pdm->b_wptr |= (1 << 4);
			pdm->b_wptr++;
		}
		/* Fill the pictureID field of the payload descriptor. */
		if (packet->pd->pictureid_present == TRUE) {
			if (packet->pd->pictureid & 0x8000) {
				*pdm->b_wptr |= ((packet->pd->pictureid >> 8) & 0xFF);
				pdm->b_wptr++;
			}
			*pdm->b_wptr |= (packet->pd->pictureid & 0xFF);
			pdm->b_wptr++;
		}
		/* Fill the tl0picidx octet of the payload descriptor. */
		if (packet->pd->tl0picidx_present == TRUE) {
			*pdm->b_wptr = packet->pd->tl0picidx;
			pdm->b_wptr++;
		}
		if ((packet->pd->tid_present == TRUE) || (packet->pd->keyidx_present == TRUE)) {
			if (packet->pd->tid_present == TRUE) {
				*pdm->b_wptr |= (packet->pd->tid & 0xC0);
				if (packet->pd->layer_sync == TRUE) *pdm->b_wptr |= (1 << 5);
			}
			if (packet->pd->keyidx_present == TRUE) {
				*pdm->b_wptr |= (packet->pd->keyidx & 0x1F);
			}
			pdm->b_wptr++;
		}

		dlen = MIN((max_size - pdsize), (packet->m->b_wptr - rptr));
		dm = dupb(packet->m);
		dm->b_rptr = rptr;
		dm->b_wptr = rptr + dlen;
		dm->b_wptr = dm->b_rptr + dlen;
		pdm->b_cont = dm;
		rptr += dlen;

1063
		ms_queue_put(ctx->output_queue, pdm);
1064 1065
	}

1066
	/* Set marker bit on last packet if required. */
Ghislain MARY's avatar
Ghislain MARY committed
1067 1068
	if (pdm != NULL) mblk_set_marker_info(pdm, marker_info);
	if (dm != NULL) mblk_set_marker_info(dm, marker_info);
1069

1070 1071 1072 1073 1074
	freeb(packet->m);
	packet->m = NULL;
}


1075
void vp8rtpfmt_packer_init(Vp8RtpFmtPackerCtx *ctx) {
1076 1077 1078 1079 1080
}

void vp8rtpfmt_packer_uninit(Vp8RtpFmtPackerCtx *ctx) {
}

1081 1082
void vp8rtpfmt_packer_process(Vp8RtpFmtPackerCtx *ctx, MSList *in, MSQueue *out) {
	ctx->output_queue = out;
1083 1084 1085 1086
	ms_list_for_each2(in, packer_process_frame_part, ctx);
	ms_list_for_each(in, free_packet);
	ms_list_free(in);
}