yading@11: /* yading@11: * RTP MPEG2TS depacketizer yading@11: * Copyright (c) 2003 Fabrice Bellard 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 "mpegts.h" yading@11: #include "rtpdec_formats.h" yading@11: yading@11: struct PayloadContext { yading@11: struct MpegTSContext *ts; yading@11: int read_buf_index; yading@11: int read_buf_size; yading@11: uint8_t buf[RTP_MAX_PACKET_LENGTH]; yading@11: }; yading@11: yading@11: static PayloadContext *mpegts_new_context(void) yading@11: { yading@11: return av_mallocz(sizeof(PayloadContext)); yading@11: } yading@11: yading@11: static void mpegts_free_context(PayloadContext *data) yading@11: { yading@11: if (!data) yading@11: return; yading@11: if (data->ts) yading@11: ff_mpegts_parse_close(data->ts); yading@11: av_free(data); yading@11: } yading@11: yading@11: static int mpegts_init(AVFormatContext *ctx, int st_index, PayloadContext *data) yading@11: { yading@11: data->ts = ff_mpegts_parse_open(ctx); yading@11: if (!data->ts) yading@11: return AVERROR(ENOMEM); yading@11: return 0; yading@11: } yading@11: yading@11: static int mpegts_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, yading@11: int flags) yading@11: { yading@11: int ret; yading@11: yading@11: // We don't want to use the RTP timestamps at all. If the mpegts demuxer yading@11: // doesn't set any pts/dts, the generic rtpdec code shouldn't try to yading@11: // fill it in either, since the mpegts and RTP timestamps are in totally yading@11: // different ranges. yading@11: *timestamp = RTP_NOTS_VALUE; yading@11: yading@11: if (!data->ts) yading@11: return AVERROR(EINVAL); yading@11: yading@11: if (!buf) { yading@11: if (data->read_buf_index >= data->read_buf_size) yading@11: return AVERROR(EAGAIN); yading@11: ret = ff_mpegts_parse_packet(data->ts, pkt, data->buf + data->read_buf_index, yading@11: data->read_buf_size - data->read_buf_index); yading@11: if (ret < 0) yading@11: return AVERROR(EAGAIN); yading@11: data->read_buf_index += ret; yading@11: if (data->read_buf_index < data->read_buf_size) yading@11: return 1; yading@11: else yading@11: return 0; yading@11: } yading@11: yading@11: ret = ff_mpegts_parse_packet(data->ts, pkt, buf, len); yading@11: /* The only error that can be returned from ff_mpegts_parse_packet yading@11: * is "no more data to return from the provided buffer", so return yading@11: * AVERROR(EAGAIN) for all errors */ yading@11: if (ret < 0) yading@11: return AVERROR(EAGAIN); yading@11: if (ret < len) { yading@11: data->read_buf_size = FFMIN(len - ret, sizeof(data->buf)); yading@11: memcpy(data->buf, buf + ret, data->read_buf_size); yading@11: data->read_buf_index = 0; yading@11: return 1; yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: RTPDynamicProtocolHandler ff_mpegts_dynamic_handler = { yading@11: .codec_type = AVMEDIA_TYPE_DATA, yading@11: .parse_packet = mpegts_handle_packet, yading@11: .alloc = mpegts_new_context, yading@11: .init = mpegts_init, yading@11: .free = mpegts_free_context, yading@11: .static_payload_id = 33, yading@11: };