rtpenc: Cast a rescaling parameter to int64_t
[libav.git] / libavformat / rtpenc.c
1 /*
2 * RTP output format
3 * Copyright (c) 2002 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "avformat.h"
23 #include "mpegts.h"
24 #include "internal.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/random_seed.h"
27 #include "libavutil/opt.h"
28
29 #include "rtpenc.h"
30
31 //#define DEBUG
32
33 static const AVOption options[] = {
34 FF_RTP_FLAG_OPTS(RTPMuxContext, flags),
35 { "payload_type", "Specify RTP payload type", offsetof(RTPMuxContext, payload_type), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, 127, AV_OPT_FLAG_ENCODING_PARAM },
36 { NULL },
37 };
38
39 static const AVClass rtp_muxer_class = {
40 .class_name = "RTP muxer",
41 .item_name = av_default_item_name,
42 .option = options,
43 .version = LIBAVUTIL_VERSION_INT,
44 };
45
46 #define RTCP_SR_SIZE 28
47
48 static int is_supported(enum CodecID id)
49 {
50 switch(id) {
51 case CODEC_ID_H263:
52 case CODEC_ID_H263P:
53 case CODEC_ID_H264:
54 case CODEC_ID_MPEG1VIDEO:
55 case CODEC_ID_MPEG2VIDEO:
56 case CODEC_ID_MPEG4:
57 case CODEC_ID_AAC:
58 case CODEC_ID_MP2:
59 case CODEC_ID_MP3:
60 case CODEC_ID_PCM_ALAW:
61 case CODEC_ID_PCM_MULAW:
62 case CODEC_ID_PCM_S8:
63 case CODEC_ID_PCM_S16BE:
64 case CODEC_ID_PCM_S16LE:
65 case CODEC_ID_PCM_U16BE:
66 case CODEC_ID_PCM_U16LE:
67 case CODEC_ID_PCM_U8:
68 case CODEC_ID_MPEG2TS:
69 case CODEC_ID_AMR_NB:
70 case CODEC_ID_AMR_WB:
71 case CODEC_ID_VORBIS:
72 case CODEC_ID_THEORA:
73 case CODEC_ID_VP8:
74 case CODEC_ID_ADPCM_G722:
75 return 1;
76 default:
77 return 0;
78 }
79 }
80
81 static int rtp_write_header(AVFormatContext *s1)
82 {
83 RTPMuxContext *s = s1->priv_data;
84 int max_packet_size, n;
85 AVStream *st;
86
87 if (s1->nb_streams != 1)
88 return -1;
89 st = s1->streams[0];
90 if (!is_supported(st->codec->codec_id)) {
91 av_log(s1, AV_LOG_ERROR, "Unsupported codec %x\n", st->codec->codec_id);
92
93 return -1;
94 }
95
96 if (s->payload_type < 0)
97 s->payload_type = ff_rtp_get_payload_type(s1, st->codec);
98 s->base_timestamp = av_get_random_seed();
99 s->timestamp = s->base_timestamp;
100 s->cur_timestamp = 0;
101 s->ssrc = av_get_random_seed();
102 s->first_packet = 1;
103 s->first_rtcp_ntp_time = ff_ntp_time();
104 if (s1->start_time_realtime)
105 /* Round the NTP time to whole milliseconds. */
106 s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
107 NTP_OFFSET_US;
108
109 max_packet_size = s1->pb->max_packet_size;
110 if (max_packet_size <= 12)
111 return AVERROR(EIO);
112 s->buf = av_malloc(max_packet_size);
113 if (s->buf == NULL) {
114 return AVERROR(ENOMEM);
115 }
116 s->max_payload_size = max_packet_size - 12;
117
118 s->max_frames_per_packet = 0;
119 if (s1->max_delay) {
120 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
121 if (st->codec->frame_size == 0) {
122 av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
123 } else {
124 s->max_frames_per_packet = av_rescale_rnd(s1->max_delay, st->codec->sample_rate, AV_TIME_BASE * (int64_t)st->codec->frame_size, AV_ROUND_DOWN);
125 }
126 }
127 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
128 /* FIXME: We should round down here... */
129 s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
130 }
131 }
132
133 avpriv_set_pts_info(st, 32, 1, 90000);
134 switch(st->codec->codec_id) {
135 case CODEC_ID_MP2:
136 case CODEC_ID_MP3:
137 s->buf_ptr = s->buf + 4;
138 break;
139 case CODEC_ID_MPEG1VIDEO:
140 case CODEC_ID_MPEG2VIDEO:
141 break;
142 case CODEC_ID_MPEG2TS:
143 n = s->max_payload_size / TS_PACKET_SIZE;
144 if (n < 1)
145 n = 1;
146 s->max_payload_size = n * TS_PACKET_SIZE;
147 s->buf_ptr = s->buf;
148 break;
149 case CODEC_ID_H264:
150 /* check for H.264 MP4 syntax */
151 if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
152 s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
153 }
154 break;
155 case CODEC_ID_VORBIS:
156 case CODEC_ID_THEORA:
157 if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
158 s->max_frames_per_packet = av_clip(s->max_frames_per_packet, 1, 15);
159 s->max_payload_size -= 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
160 s->num_frames = 0;
161 goto defaultcase;
162 case CODEC_ID_VP8:
163 av_log(s1, AV_LOG_ERROR, "RTP VP8 payload implementation is "
164 "incompatible with the latest spec drafts.\n");
165 break;
166 case CODEC_ID_ADPCM_G722:
167 /* Due to a historical error, the clock rate for G722 in RTP is
168 * 8000, even if the sample rate is 16000. See RFC 3551. */
169 avpriv_set_pts_info(st, 32, 1, 8000);
170 break;
171 case CODEC_ID_AMR_NB:
172 case CODEC_ID_AMR_WB:
173 if (!s->max_frames_per_packet)
174 s->max_frames_per_packet = 12;
175 if (st->codec->codec_id == CODEC_ID_AMR_NB)
176 n = 31;
177 else
178 n = 61;
179 /* max_header_toc_size + the largest AMR payload must fit */
180 if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
181 av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
182 return -1;
183 }
184 if (st->codec->channels != 1) {
185 av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
186 return -1;
187 }
188 case CODEC_ID_AAC:
189 s->num_frames = 0;
190 default:
191 defaultcase:
192 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
193 avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
194 }
195 s->buf_ptr = s->buf;
196 break;
197 }
198
199 return 0;
200 }
201
202 /* send an rtcp sender report packet */
203 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
204 {
205 RTPMuxContext *s = s1->priv_data;
206 uint32_t rtp_ts;
207
208 av_dlog(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
209
210 s->last_rtcp_ntp_time = ntp_time;
211 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
212 s1->streams[0]->time_base) + s->base_timestamp;
213 avio_w8(s1->pb, (RTP_VERSION << 6));
214 avio_w8(s1->pb, RTCP_SR);
215 avio_wb16(s1->pb, 6); /* length in words - 1 */
216 avio_wb32(s1->pb, s->ssrc);
217 avio_wb32(s1->pb, ntp_time / 1000000);
218 avio_wb32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
219 avio_wb32(s1->pb, rtp_ts);
220 avio_wb32(s1->pb, s->packet_count);
221 avio_wb32(s1->pb, s->octet_count);
222 avio_flush(s1->pb);
223 }
224
225 /* send an rtp packet. sequence number is incremented, but the caller
226 must update the timestamp itself */
227 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
228 {
229 RTPMuxContext *s = s1->priv_data;
230
231 av_dlog(s1, "rtp_send_data size=%d\n", len);
232
233 /* build the RTP header */
234 avio_w8(s1->pb, (RTP_VERSION << 6));
235 avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
236 avio_wb16(s1->pb, s->seq);
237 avio_wb32(s1->pb, s->timestamp);
238 avio_wb32(s1->pb, s->ssrc);
239
240 avio_write(s1->pb, buf1, len);
241 avio_flush(s1->pb);
242
243 s->seq++;
244 s->octet_count += len;
245 s->packet_count++;
246 }
247
248 /* send an integer number of samples and compute time stamp and fill
249 the rtp send buffer before sending. */
250 static void rtp_send_samples(AVFormatContext *s1,
251 const uint8_t *buf1, int size, int sample_size)
252 {
253 RTPMuxContext *s = s1->priv_data;
254 int len, max_packet_size, n;
255
256 max_packet_size = (s->max_payload_size / sample_size) * sample_size;
257 /* not needed, but who nows */
258 if ((size % sample_size) != 0)
259 av_abort();
260 n = 0;
261 while (size > 0) {
262 s->buf_ptr = s->buf;
263 len = FFMIN(max_packet_size, size);
264
265 /* copy data */
266 memcpy(s->buf_ptr, buf1, len);
267 s->buf_ptr += len;
268 buf1 += len;
269 size -= len;
270 s->timestamp = s->cur_timestamp + n / sample_size;
271 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
272 n += (s->buf_ptr - s->buf);
273 }
274 }
275
276 static void rtp_send_mpegaudio(AVFormatContext *s1,
277 const uint8_t *buf1, int size)
278 {
279 RTPMuxContext *s = s1->priv_data;
280 int len, count, max_packet_size;
281
282 max_packet_size = s->max_payload_size;
283
284 /* test if we must flush because not enough space */
285 len = (s->buf_ptr - s->buf);
286 if ((len + size) > max_packet_size) {
287 if (len > 4) {
288 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
289 s->buf_ptr = s->buf + 4;
290 }
291 }
292 if (s->buf_ptr == s->buf + 4) {
293 s->timestamp = s->cur_timestamp;
294 }
295
296 /* add the packet */
297 if (size > max_packet_size) {
298 /* big packet: fragment */
299 count = 0;
300 while (size > 0) {
301 len = max_packet_size - 4;
302 if (len > size)
303 len = size;
304 /* build fragmented packet */
305 s->buf[0] = 0;
306 s->buf[1] = 0;
307 s->buf[2] = count >> 8;
308 s->buf[3] = count;
309 memcpy(s->buf + 4, buf1, len);
310 ff_rtp_send_data(s1, s->buf, len + 4, 0);
311 size -= len;
312 buf1 += len;
313 count += len;
314 }
315 } else {
316 if (s->buf_ptr == s->buf + 4) {
317 /* no fragmentation possible */
318 s->buf[0] = 0;
319 s->buf[1] = 0;
320 s->buf[2] = 0;
321 s->buf[3] = 0;
322 }
323 memcpy(s->buf_ptr, buf1, size);
324 s->buf_ptr += size;
325 }
326 }
327
328 static void rtp_send_raw(AVFormatContext *s1,
329 const uint8_t *buf1, int size)
330 {
331 RTPMuxContext *s = s1->priv_data;
332 int len, max_packet_size;
333
334 max_packet_size = s->max_payload_size;
335
336 while (size > 0) {
337 len = max_packet_size;
338 if (len > size)
339 len = size;
340
341 s->timestamp = s->cur_timestamp;
342 ff_rtp_send_data(s1, buf1, len, (len == size));
343
344 buf1 += len;
345 size -= len;
346 }
347 }
348
349 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
350 static void rtp_send_mpegts_raw(AVFormatContext *s1,
351 const uint8_t *buf1, int size)
352 {
353 RTPMuxContext *s = s1->priv_data;
354 int len, out_len;
355
356 while (size >= TS_PACKET_SIZE) {
357 len = s->max_payload_size - (s->buf_ptr - s->buf);
358 if (len > size)
359 len = size;
360 memcpy(s->buf_ptr, buf1, len);
361 buf1 += len;
362 size -= len;
363 s->buf_ptr += len;
364
365 out_len = s->buf_ptr - s->buf;
366 if (out_len >= s->max_payload_size) {
367 ff_rtp_send_data(s1, s->buf, out_len, 0);
368 s->buf_ptr = s->buf;
369 }
370 }
371 }
372
373 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
374 {
375 RTPMuxContext *s = s1->priv_data;
376 AVStream *st = s1->streams[0];
377 int rtcp_bytes;
378 int size= pkt->size;
379
380 av_dlog(s1, "%d: write len=%d\n", pkt->stream_index, size);
381
382 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
383 RTCP_TX_RATIO_DEN;
384 if (s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
385 (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
386 rtcp_send_sr(s1, ff_ntp_time());
387 s->last_octet_count = s->octet_count;
388 s->first_packet = 0;
389 }
390 s->cur_timestamp = s->base_timestamp + pkt->pts;
391
392 switch(st->codec->codec_id) {
393 case CODEC_ID_PCM_MULAW:
394 case CODEC_ID_PCM_ALAW:
395 case CODEC_ID_PCM_U8:
396 case CODEC_ID_PCM_S8:
397 rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
398 break;
399 case CODEC_ID_PCM_U16BE:
400 case CODEC_ID_PCM_U16LE:
401 case CODEC_ID_PCM_S16BE:
402 case CODEC_ID_PCM_S16LE:
403 rtp_send_samples(s1, pkt->data, size, 2 * st->codec->channels);
404 break;
405 case CODEC_ID_ADPCM_G722:
406 /* The actual sample size is half a byte per sample, but since the
407 * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
408 * the correct parameter for send_samples is 1 byte per stream clock. */
409 rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
410 break;
411 case CODEC_ID_MP2:
412 case CODEC_ID_MP3:
413 rtp_send_mpegaudio(s1, pkt->data, size);
414 break;
415 case CODEC_ID_MPEG1VIDEO:
416 case CODEC_ID_MPEG2VIDEO:
417 ff_rtp_send_mpegvideo(s1, pkt->data, size);
418 break;
419 case CODEC_ID_AAC:
420 if (s->flags & FF_RTP_FLAG_MP4A_LATM)
421 ff_rtp_send_latm(s1, pkt->data, size);
422 else
423 ff_rtp_send_aac(s1, pkt->data, size);
424 break;
425 case CODEC_ID_AMR_NB:
426 case CODEC_ID_AMR_WB:
427 ff_rtp_send_amr(s1, pkt->data, size);
428 break;
429 case CODEC_ID_MPEG2TS:
430 rtp_send_mpegts_raw(s1, pkt->data, size);
431 break;
432 case CODEC_ID_H264:
433 ff_rtp_send_h264(s1, pkt->data, size);
434 break;
435 case CODEC_ID_H263:
436 case CODEC_ID_H263P:
437 ff_rtp_send_h263(s1, pkt->data, size);
438 break;
439 case CODEC_ID_VORBIS:
440 case CODEC_ID_THEORA:
441 ff_rtp_send_xiph(s1, pkt->data, size);
442 break;
443 case CODEC_ID_VP8:
444 ff_rtp_send_vp8(s1, pkt->data, size);
445 break;
446 default:
447 /* better than nothing : send the codec raw data */
448 rtp_send_raw(s1, pkt->data, size);
449 break;
450 }
451 return 0;
452 }
453
454 static int rtp_write_trailer(AVFormatContext *s1)
455 {
456 RTPMuxContext *s = s1->priv_data;
457
458 av_freep(&s->buf);
459
460 return 0;
461 }
462
463 AVOutputFormat ff_rtp_muxer = {
464 .name = "rtp",
465 .long_name = NULL_IF_CONFIG_SMALL("RTP output format"),
466 .priv_data_size = sizeof(RTPMuxContext),
467 .audio_codec = CODEC_ID_PCM_MULAW,
468 .video_codec = CODEC_ID_MPEG4,
469 .write_header = rtp_write_header,
470 .write_packet = rtp_write_packet,
471 .write_trailer = rtp_write_trailer,
472 .priv_class = &rtp_muxer_class,
473 };