rtpenc: Cast a rescaling parameter to int64_t
[libav.git] / libavformat / rtpenc.c
CommitLineData
83a0d387
LA
1/*
2 * RTP output format
406792e7 3 * Copyright (c) 2002 Fabrice Bellard
83a0d387 4 *
2912e87a 5 * This file is part of Libav.
83a0d387 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
83a0d387
LA
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
83a0d387
LA
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
83a0d387
LA
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
245976da 21
83a0d387
LA
22#include "avformat.h"
23#include "mpegts.h"
594a9aeb 24#include "internal.h"
0ebcdf5c 25#include "libavutil/mathematics.h"
4c1202f7 26#include "libavutil/random_seed.h"
08321228 27#include "libavutil/opt.h"
83a0d387 28
302879cb 29#include "rtpenc.h"
83a0d387
LA
30
31//#define DEBUG
32
08321228 33static const AVOption options[] = {
635fac9a 34 FF_RTP_FLAG_OPTS(RTPMuxContext, flags),
145f741e 35 { "payload_type", "Specify RTP payload type", offsetof(RTPMuxContext, payload_type), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, 127, AV_OPT_FLAG_ENCODING_PARAM },
08321228
JCR
36 { NULL },
37};
38
39static 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
83a0d387
LA
46#define RTCP_SR_SIZE 28
47
0766c3ee
LA
48static int is_supported(enum CodecID id)
49{
50 switch(id) {
9edfaf3c
MS
51 case CODEC_ID_H263:
52 case CODEC_ID_H263P:
0766c3ee
LA
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:
08e696c0
MS
69 case CODEC_ID_AMR_NB:
70 case CODEC_ID_AMR_WB:
91af5601
JA
71 case CODEC_ID_VORBIS:
72 case CODEC_ID_THEORA:
7b18d94c 73 case CODEC_ID_VP8:
0048a2a8 74 case CODEC_ID_ADPCM_G722:
0766c3ee
LA
75 return 1;
76 default:
77 return 0;
78 }
79}
80
83a0d387
LA
81static int rtp_write_header(AVFormatContext *s1)
82{
302879cb 83 RTPMuxContext *s = s1->priv_data;
b3fbe02c 84 int max_packet_size, n;
83a0d387
LA
85 AVStream *st;
86
87 if (s1->nb_streams != 1)
88 return -1;
89 st = s1->streams[0];
0766c3ee
LA
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 }
83a0d387 95
9152880e
RC
96 if (s->payload_type < 0)
97 s->payload_type = ff_rtp_get_payload_type(s1, st->codec);
576fb48e 98 s->base_timestamp = av_get_random_seed();
83a0d387
LA
99 s->timestamp = s->base_timestamp;
100 s->cur_timestamp = 0;
576fb48e 101 s->ssrc = av_get_random_seed();
83a0d387 102 s->first_packet = 1;
594a9aeb 103 s->first_rtcp_ntp_time = ff_ntp_time();
b1d55e5e
MS
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;
83a0d387 108
e8bb2e24 109 max_packet_size = s1->pb->max_packet_size;
83a0d387
LA
110 if (max_packet_size <= 12)
111 return AVERROR(EIO);
d3536678
LA
112 s->buf = av_malloc(max_packet_size);
113 if (s->buf == NULL) {
114 return AVERROR(ENOMEM);
115 }
83a0d387
LA
116 s->max_payload_size = max_packet_size - 12;
117
118 s->max_frames_per_packet = 0;
119 if (s1->max_delay) {
72415b2a 120 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
83a0d387
LA
121 if (st->codec->frame_size == 0) {
122 av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
123 } else {
2d31d890 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);
83a0d387
LA
125 }
126 }
72415b2a 127 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
83a0d387 128 /* FIXME: We should round down here... */
a4696aa2 129 s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
83a0d387
LA
130 }
131 }
132
c3f9ebf7 133 avpriv_set_pts_info(st, 32, 1, 90000);
83a0d387
LA
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;
8b889b34
LA
149 case CODEC_ID_H264:
150 /* check for H.264 MP4 syntax */
8a2679ad 151 if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
8b889b34
LA
152 s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
153 }
154 break;
91af5601
JA
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;
7b18d94c 162 case CODEC_ID_VP8:
35b01694
MS
163 av_log(s1, AV_LOG_ERROR, "RTP VP8 payload implementation is "
164 "incompatible with the latest spec drafts.\n");
7b18d94c 165 break;
0048a2a8
MS
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. */
c3f9ebf7 169 avpriv_set_pts_info(st, 32, 1, 8000);
0048a2a8 170 break;
08e696c0
MS
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 }
83a0d387 188 case CODEC_ID_AAC:
21da81d7 189 s->num_frames = 0;
83a0d387 190 default:
91af5601 191defaultcase:
72415b2a 192 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
c3f9ebf7 193 avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
83a0d387
LA
194 }
195 s->buf_ptr = s->buf;
196 break;
197 }
198
199 return 0;
200}
201
202/* send an rtcp sender report packet */
203static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
204{
302879cb 205 RTPMuxContext *s = s1->priv_data;
83a0d387
LA
206 uint32_t rtp_ts;
207
dfd2a005 208 av_dlog(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
83a0d387 209
83a0d387 210 s->last_rtcp_ntp_time = ntp_time;
a4696aa2 211 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
83a0d387 212 s1->streams[0]->time_base) + s->base_timestamp;
77eb5504
AK
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);
b7f2fdde 222 avio_flush(s1->pb);
83a0d387
LA
223}
224
225/* send an rtp packet. sequence number is incremented, but the caller
226 must update the timestamp itself */
227void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
228{
302879cb 229 RTPMuxContext *s = s1->priv_data;
83a0d387 230
dfd2a005 231 av_dlog(s1, "rtp_send_data size=%d\n", len);
83a0d387
LA
232
233 /* build the RTP header */
77eb5504
AK
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);
83a0d387 239
77eb5504 240 avio_write(s1->pb, buf1, len);
b7f2fdde 241 avio_flush(s1->pb);
83a0d387
LA
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. */
250static void rtp_send_samples(AVFormatContext *s1,
251 const uint8_t *buf1, int size, int sample_size)
252{
302879cb 253 RTPMuxContext *s = s1->priv_data;
83a0d387
LA
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
83a0d387
LA
276static void rtp_send_mpegaudio(AVFormatContext *s1,
277 const uint8_t *buf1, int size)
278{
302879cb 279 RTPMuxContext *s = s1->priv_data;
83a0d387
LA
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
328static void rtp_send_raw(AVFormatContext *s1,
329 const uint8_t *buf1, int size)
330{
302879cb 331 RTPMuxContext *s = s1->priv_data;
83a0d387
LA
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 */
350static void rtp_send_mpegts_raw(AVFormatContext *s1,
351 const uint8_t *buf1, int size)
352{
302879cb 353 RTPMuxContext *s = s1->priv_data;
83a0d387
LA
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
83a0d387
LA
373static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
374{
302879cb 375 RTPMuxContext *s = s1->priv_data;
83a0d387
LA
376 AVStream *st = s1->streams[0];
377 int rtcp_bytes;
378 int size= pkt->size;
83a0d387 379
dfd2a005 380 av_dlog(s1, "%d: write len=%d\n", pkt->stream_index, size);
83a0d387 381
83a0d387
LA
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) &&
594a9aeb
MS
385 (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
386 rtcp_send_sr(s1, ff_ntp_time());
83a0d387
LA
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:
d3d1eae6 397 rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
83a0d387
LA
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:
d3d1eae6 403 rtp_send_samples(s1, pkt->data, size, 2 * st->codec->channels);
83a0d387 404 break;
0048a2a8
MS
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;
83a0d387
LA
411 case CODEC_ID_MP2:
412 case CODEC_ID_MP3:
d3d1eae6 413 rtp_send_mpegaudio(s1, pkt->data, size);
83a0d387
LA
414 break;
415 case CODEC_ID_MPEG1VIDEO:
416 case CODEC_ID_MPEG2VIDEO:
d3d1eae6 417 ff_rtp_send_mpegvideo(s1, pkt->data, size);
83a0d387
LA
418 break;
419 case CODEC_ID_AAC:
08321228
JCR
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);
83a0d387 424 break;
08e696c0
MS
425 case CODEC_ID_AMR_NB:
426 case CODEC_ID_AMR_WB:
d3d1eae6 427 ff_rtp_send_amr(s1, pkt->data, size);
08e696c0 428 break;
83a0d387 429 case CODEC_ID_MPEG2TS:
d3d1eae6 430 rtp_send_mpegts_raw(s1, pkt->data, size);
83a0d387 431 break;
f79bfe48 432 case CODEC_ID_H264:
d3d1eae6 433 ff_rtp_send_h264(s1, pkt->data, size);
f79bfe48 434 break;
9edfaf3c
MS
435 case CODEC_ID_H263:
436 case CODEC_ID_H263P:
d3d1eae6 437 ff_rtp_send_h263(s1, pkt->data, size);
9edfaf3c 438 break;
91af5601
JA
439 case CODEC_ID_VORBIS:
440 case CODEC_ID_THEORA:
441 ff_rtp_send_xiph(s1, pkt->data, size);
442 break;
7b18d94c
JA
443 case CODEC_ID_VP8:
444 ff_rtp_send_vp8(s1, pkt->data, size);
445 break;
83a0d387
LA
446 default:
447 /* better than nothing : send the codec raw data */
d3d1eae6 448 rtp_send_raw(s1, pkt->data, size);
83a0d387
LA
449 break;
450 }
451 return 0;
452}
453
d3536678
LA
454static 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
c6610a21 463AVOutputFormat ff_rtp_muxer = {
dfc2c4d9
AK
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,
9f938ca5 468 .video_codec = CODEC_ID_MPEG4,
dfc2c4d9
AK
469 .write_header = rtp_write_header,
470 .write_packet = rtp_write_packet,
471 .write_trailer = rtp_write_trailer,
08321228 472 .priv_class = &rtp_muxer_class,
83a0d387 473};