yading@11: /* yading@11: * RTP H.263 Depacketizer, RFC 4629 yading@11: * Copyright (c) 2010 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 "rtpdec_formats.h" yading@11: #include "libavutil/intreadwrite.h" yading@11: yading@11: static int h263_init(AVFormatContext *ctx, int st_index, PayloadContext *data) yading@11: { yading@11: if (st_index < 0) yading@11: return 0; yading@11: ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL; yading@11: return 0; yading@11: } yading@11: yading@11: int ff_h263_handle_packet(AVFormatContext *ctx, PayloadContext *data, yading@11: AVStream *st, AVPacket *pkt, uint32_t *timestamp, yading@11: const uint8_t *buf, int len, uint16_t seq, int flags) yading@11: { yading@11: uint8_t *ptr; yading@11: uint16_t header; yading@11: int startcode, vrc, picture_header; yading@11: yading@11: if (len < 2) { yading@11: av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet\n"); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: yading@11: /* Decode the 16 bit H.263+ payload header, as described in section yading@11: * 5.1 of RFC 4629. The fields of this header are: yading@11: * - 5 reserved bits, should be ignored. yading@11: * - One bit (P, startcode), indicating a picture start, picture segment yading@11: * start or video sequence end. If set, two zero bytes should be yading@11: * prepended to the payload. yading@11: * - One bit (V, vrc), indicating the presence of an 8 bit Video yading@11: * Redundancy Coding field after this 16 bit header. yading@11: * - 6 bits (PLEN, picture_header), the length (in bytes) of an extra yading@11: * picture header, following the VRC field. yading@11: * - 3 bits (PEBIT), the number of bits to ignore of the last byte yading@11: * of the extra picture header. (Not used at the moment.) yading@11: */ yading@11: header = AV_RB16(buf); yading@11: startcode = (header & 0x0400) >> 9; yading@11: vrc = header & 0x0200; yading@11: picture_header = (header & 0x01f8) >> 3; yading@11: buf += 2; yading@11: len -= 2; yading@11: yading@11: if (vrc) { yading@11: /* Skip VRC header if present, not used at the moment. */ yading@11: buf += 1; yading@11: len -= 1; yading@11: } yading@11: if (picture_header) { yading@11: /* Skip extra picture header if present, not used at the moment. */ yading@11: buf += picture_header; yading@11: len -= picture_header; yading@11: } yading@11: yading@11: if (len < 0) { yading@11: av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet\n"); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: yading@11: if (av_new_packet(pkt, len + startcode)) { yading@11: av_log(ctx, AV_LOG_ERROR, "Out of memory\n"); yading@11: return AVERROR(ENOMEM); yading@11: } yading@11: pkt->stream_index = st->index; yading@11: ptr = pkt->data; yading@11: yading@11: if (startcode) { yading@11: *ptr++ = 0; yading@11: *ptr++ = 0; yading@11: } yading@11: memcpy(ptr, buf, len); yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: RTPDynamicProtocolHandler ff_h263_1998_dynamic_handler = { yading@11: .enc_name = "H263-1998", yading@11: .codec_type = AVMEDIA_TYPE_VIDEO, yading@11: .codec_id = AV_CODEC_ID_H263, yading@11: .init = h263_init, yading@11: .parse_packet = ff_h263_handle_packet, yading@11: }; yading@11: yading@11: RTPDynamicProtocolHandler ff_h263_2000_dynamic_handler = { yading@11: .enc_name = "H263-2000", yading@11: .codec_type = AVMEDIA_TYPE_VIDEO, yading@11: .codec_id = AV_CODEC_ID_H263, yading@11: .init = h263_init, yading@11: .parse_packet = ff_h263_handle_packet, yading@11: };