yading@11
|
1 /*
|
yading@11
|
2 * RTP MPEG2TS depacketizer
|
yading@11
|
3 * Copyright (c) 2003 Fabrice Bellard
|
yading@11
|
4 *
|
yading@11
|
5 * This file is part of FFmpeg.
|
yading@11
|
6 *
|
yading@11
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
9 * License as published by the Free Software Foundation; either
|
yading@11
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
11 *
|
yading@11
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
15 * Lesser General Public License for more details.
|
yading@11
|
16 *
|
yading@11
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
20 */
|
yading@11
|
21
|
yading@11
|
22 #include "mpegts.h"
|
yading@11
|
23 #include "rtpdec_formats.h"
|
yading@11
|
24
|
yading@11
|
25 struct PayloadContext {
|
yading@11
|
26 struct MpegTSContext *ts;
|
yading@11
|
27 int read_buf_index;
|
yading@11
|
28 int read_buf_size;
|
yading@11
|
29 uint8_t buf[RTP_MAX_PACKET_LENGTH];
|
yading@11
|
30 };
|
yading@11
|
31
|
yading@11
|
32 static PayloadContext *mpegts_new_context(void)
|
yading@11
|
33 {
|
yading@11
|
34 return av_mallocz(sizeof(PayloadContext));
|
yading@11
|
35 }
|
yading@11
|
36
|
yading@11
|
37 static void mpegts_free_context(PayloadContext *data)
|
yading@11
|
38 {
|
yading@11
|
39 if (!data)
|
yading@11
|
40 return;
|
yading@11
|
41 if (data->ts)
|
yading@11
|
42 ff_mpegts_parse_close(data->ts);
|
yading@11
|
43 av_free(data);
|
yading@11
|
44 }
|
yading@11
|
45
|
yading@11
|
46 static int mpegts_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
|
yading@11
|
47 {
|
yading@11
|
48 data->ts = ff_mpegts_parse_open(ctx);
|
yading@11
|
49 if (!data->ts)
|
yading@11
|
50 return AVERROR(ENOMEM);
|
yading@11
|
51 return 0;
|
yading@11
|
52 }
|
yading@11
|
53
|
yading@11
|
54 static int mpegts_handle_packet(AVFormatContext *ctx, PayloadContext *data,
|
yading@11
|
55 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
|
yading@11
|
56 const uint8_t *buf, int len, uint16_t seq,
|
yading@11
|
57 int flags)
|
yading@11
|
58 {
|
yading@11
|
59 int ret;
|
yading@11
|
60
|
yading@11
|
61 // We don't want to use the RTP timestamps at all. If the mpegts demuxer
|
yading@11
|
62 // doesn't set any pts/dts, the generic rtpdec code shouldn't try to
|
yading@11
|
63 // fill it in either, since the mpegts and RTP timestamps are in totally
|
yading@11
|
64 // different ranges.
|
yading@11
|
65 *timestamp = RTP_NOTS_VALUE;
|
yading@11
|
66
|
yading@11
|
67 if (!data->ts)
|
yading@11
|
68 return AVERROR(EINVAL);
|
yading@11
|
69
|
yading@11
|
70 if (!buf) {
|
yading@11
|
71 if (data->read_buf_index >= data->read_buf_size)
|
yading@11
|
72 return AVERROR(EAGAIN);
|
yading@11
|
73 ret = ff_mpegts_parse_packet(data->ts, pkt, data->buf + data->read_buf_index,
|
yading@11
|
74 data->read_buf_size - data->read_buf_index);
|
yading@11
|
75 if (ret < 0)
|
yading@11
|
76 return AVERROR(EAGAIN);
|
yading@11
|
77 data->read_buf_index += ret;
|
yading@11
|
78 if (data->read_buf_index < data->read_buf_size)
|
yading@11
|
79 return 1;
|
yading@11
|
80 else
|
yading@11
|
81 return 0;
|
yading@11
|
82 }
|
yading@11
|
83
|
yading@11
|
84 ret = ff_mpegts_parse_packet(data->ts, pkt, buf, len);
|
yading@11
|
85 /* The only error that can be returned from ff_mpegts_parse_packet
|
yading@11
|
86 * is "no more data to return from the provided buffer", so return
|
yading@11
|
87 * AVERROR(EAGAIN) for all errors */
|
yading@11
|
88 if (ret < 0)
|
yading@11
|
89 return AVERROR(EAGAIN);
|
yading@11
|
90 if (ret < len) {
|
yading@11
|
91 data->read_buf_size = FFMIN(len - ret, sizeof(data->buf));
|
yading@11
|
92 memcpy(data->buf, buf + ret, data->read_buf_size);
|
yading@11
|
93 data->read_buf_index = 0;
|
yading@11
|
94 return 1;
|
yading@11
|
95 }
|
yading@11
|
96 return 0;
|
yading@11
|
97 }
|
yading@11
|
98
|
yading@11
|
99 RTPDynamicProtocolHandler ff_mpegts_dynamic_handler = {
|
yading@11
|
100 .codec_type = AVMEDIA_TYPE_DATA,
|
yading@11
|
101 .parse_packet = mpegts_handle_packet,
|
yading@11
|
102 .alloc = mpegts_new_context,
|
yading@11
|
103 .init = mpegts_init,
|
yading@11
|
104 .free = mpegts_free_context,
|
yading@11
|
105 .static_payload_id = 33,
|
yading@11
|
106 };
|