yading@11: /* yading@11: * RTP packetization for H.263 video yading@11: * Copyright (c) 2009 Luca Abeni yading@11: * Copyright (c) 2009 Martin Storsjo yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "avformat.h" yading@11: #include "rtpenc.h" yading@11: yading@11: const uint8_t *ff_h263_find_resync_marker_reverse(const uint8_t *av_restrict start, yading@11: const uint8_t *av_restrict end) yading@11: { yading@11: const uint8_t *p = end - 1; yading@11: start += 1; /* Make sure we never return the original start. */ yading@11: for (; p > start; p -= 2) { yading@11: if (!*p) { yading@11: if (!p[ 1] && p[2]) return p; yading@11: else if (!p[-1] && p[1]) return p - 1; yading@11: } yading@11: } yading@11: return end; yading@11: } yading@11: yading@11: /** yading@11: * Packetize H.263 frames into RTP packets according to RFC 4629 yading@11: */ yading@11: void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size) yading@11: { yading@11: RTPMuxContext *s = s1->priv_data; yading@11: int len, max_packet_size; yading@11: uint8_t *q; yading@11: yading@11: max_packet_size = s->max_payload_size; yading@11: yading@11: while (size > 0) { yading@11: q = s->buf; yading@11: if (size >= 2 && (buf1[0] == 0) && (buf1[1] == 0)) { yading@11: *q++ = 0x04; yading@11: buf1 += 2; yading@11: size -= 2; yading@11: } else { yading@11: *q++ = 0; yading@11: } yading@11: *q++ = 0; yading@11: yading@11: len = FFMIN(max_packet_size - 2, size); yading@11: yading@11: /* Look for a better place to split the frame into packets. */ yading@11: if (len < size) { yading@11: const uint8_t *end = ff_h263_find_resync_marker_reverse(buf1, yading@11: buf1 + len); yading@11: len = end - buf1; yading@11: } yading@11: yading@11: memcpy(q, buf1, len); yading@11: q += len; yading@11: yading@11: /* 90 KHz time stamp */ yading@11: s->timestamp = s->cur_timestamp; yading@11: ff_rtp_send_data(s1, s->buf, q - s->buf, (len == size)); yading@11: yading@11: buf1 += len; yading@11: size -= len; yading@11: } yading@11: }