FFmpeg
rtpenc_aac.c
Go to the documentation of this file.
1 /*
2  * copyright (c) 2007 Luca Abeni
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/intreadwrite.h"
22 
23 #include "avformat.h"
24 #include "rtpenc.h"
25 
26 
27 void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size)
28 {
29  RTPMuxContext *s = s1->priv_data;
30  AVStream *st = s1->streams[0];
31  const int max_au_headers_size = 2 + 2 * s->max_frames_per_packet;
32  int len, max_packet_size = s->max_payload_size - max_au_headers_size;
33  uint8_t *p;
34 
35  /* skip ADTS header, if present */
36  if ((s1->streams[0]->codecpar->extradata_size) == 0) {
37  if (size < 7) {
38  av_log(s1, AV_LOG_ERROR, "AAC packet too small for ADTS header\n");
39  return;
40  }
41  size -= 7;
42  buff += 7;
43  }
44 
45  /* test if the packet must be sent */
46  len = (s->buf_ptr - s->buf);
47  if (s->num_frames &&
48  (s->num_frames == s->max_frames_per_packet ||
49  (len + size) > s->max_payload_size ||
50  av_compare_ts(s->cur_timestamp - s->timestamp, st->time_base,
51  s1->max_delay, AV_TIME_BASE_Q) >= 0)) {
52  int au_size = s->num_frames * 2;
53 
54  p = s->buf + max_au_headers_size - au_size - 2;
55  if (p != s->buf) {
56  memmove(p + 2, s->buf + 2, au_size);
57  }
58  /* Write the AU header size */
59  AV_WB16(p, au_size * 8);
60 
61  ff_rtp_send_data(s1, p, s->buf_ptr - p, 1);
62 
63  s->num_frames = 0;
64  }
65  if (s->num_frames == 0) {
66  s->buf_ptr = s->buf + max_au_headers_size;
67  s->timestamp = s->cur_timestamp;
68  }
69 
70  if (size <= max_packet_size) {
71  p = s->buf + s->num_frames++ * 2 + 2;
72  AV_WB16(p, size * 8);
73  memcpy(s->buf_ptr, buff, size);
74  s->buf_ptr += size;
75  } else {
76  int au_size = size;
77 
78  max_packet_size = s->max_payload_size - 4;
79  p = s->buf;
80  AV_WB16(p, 2 * 8);
81  while (size > 0) {
82  len = FFMIN(size, max_packet_size);
83  AV_WB16(&p[2], au_size * 8);
84  memcpy(p + 4, buff, len);
85  ff_rtp_send_data(s1, p, len + 4, len == size);
86  size -= len;
87  buff += len;
88  }
89  }
90 }
av_compare_ts
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
Definition: mathematics.c:147
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:263
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1352
ff_rtp_send_data
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
Definition: rtpenc.c:364
RTPMuxContext::st
AVStream * st
Definition: rtpenc.h:30
ff_rtp_send_aac
void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size)
Definition: rtpenc_aac.c:27
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVFormatContext
Format I/O context.
Definition: avformat.h:1284
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:770
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:786
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:401
RTPMuxContext
Definition: rtpenc.h:27
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:75
size
int size
Definition: twinvq_data.h:10344
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFormatContext::max_delay
int max_delay
Definition: avformat.h:1429
len
int len
Definition: vorbis_enc_data.h:426
rtpenc.h
AVStream
Stream structure.
Definition: avformat.h:747
avformat.h
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1312