FFmpeg
rtpdec_ac3.c
Go to the documentation of this file.
1 /*
2  * RTP parser for AC3 payload format (RFC 4184)
3  * Copyright (c) 2015 Gilles Chanteperdrix <gch@xenomai.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "avio_internal.h"
24 #include "rtpdec_formats.h"
25 
26 #define RTP_AC3_PAYLOAD_HEADER_SIZE 2
27 
28 struct PayloadContext {
29  unsigned nr_frames;
30  unsigned last_frame;
31  uint32_t timestamp;
33 };
34 
36 {
37  ffio_free_dyn_buf(&data->fragment);
38 }
39 
41  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
42  const uint8_t *buf, int len, uint16_t seq,
43  int flags)
44 {
45  unsigned frame_type;
46  unsigned nr_frames;
47  int err;
48 
49  if (len < RTP_AC3_PAYLOAD_HEADER_SIZE + 1) {
50  av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
51  return AVERROR_INVALIDDATA;
52  }
53 
54  frame_type = buf[0] & 0x3;
55  nr_frames = buf[1];
58 
59  switch (frame_type) {
60  case 0: /* One or more complete frames */
61  if (!nr_frames) {
62  av_log(ctx, AV_LOG_ERROR, "Invalid AC3 packet data\n");
63  return AVERROR_INVALIDDATA;
64  }
65  if ((err = av_new_packet(pkt, len)) < 0) {
66  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
67  return err;
68  }
69 
70  pkt->stream_index = st->index;
71  memcpy(pkt->data, buf, len);
72  return 0;
73 
74  case 1:
75  case 2: /* First fragment */
76  ffio_free_dyn_buf(&data->fragment);
77 
78  data->last_frame = 1;
79  data->nr_frames = nr_frames;
80  err = avio_open_dyn_buf(&data->fragment);
81  if (err < 0)
82  return err;
83 
84  avio_write(data->fragment, buf, len);
85  data->timestamp = *timestamp;
86  return AVERROR(EAGAIN);
87 
88  case 3: /* Fragment other than first */
89  if (!data->fragment) {
91  "Received packet without a start fragment; dropping.\n");
92  return AVERROR(EAGAIN);
93  }
94  if (nr_frames != data->nr_frames ||
95  data->timestamp != *timestamp) {
96  ffio_free_dyn_buf(&data->fragment);
97  av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
98  return AVERROR_INVALIDDATA;
99  }
100 
101  avio_write(data->fragment, buf, len);
102  data->last_frame++;
103  }
104 
105  if (!(flags & RTP_FLAG_MARKER))
106  return AVERROR(EAGAIN);
107 
108  if (data->last_frame != data->nr_frames) {
109  ffio_free_dyn_buf(&data->fragment);
110  av_log(ctx, AV_LOG_ERROR, "Missed %d packets\n",
111  data->nr_frames - data->last_frame);
112  return AVERROR_INVALIDDATA;
113  }
114 
115  err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
116  if (err < 0) {
118  "Error occurred when getting fragment buffer.\n");
119  return err;
120  }
121 
122  return 0;
123 }
124 
126  .enc_name = "ac3",
127  .codec_type = AVMEDIA_TYPE_AUDIO,
128  .codec_id = AV_CODEC_ID_AC3,
129  .need_parsing = AVSTREAM_PARSE_FULL,
130  .priv_data_size = sizeof(PayloadContext),
131  .close = ac3_close_context,
133 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:443
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
rtpdec_formats.h
RTP_FLAG_MARKER
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:94
AVPacket::data
uint8_t * data
Definition: packet.h:522
data
const char data[16]
Definition: mxf.c:148
ff_rtp_finalize_packet
int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
Close the dynamic buffer and make a packet from it.
Definition: rtpdec.c:1002
PayloadContext::timestamp
uint32_t timestamp
current frame timestamp
Definition: rtpdec_ac3.c:31
RTPDynamicProtocolHandler::enc_name
const char * enc_name
Definition: rtpdec.h:117
ac3_close_context
static void ac3_close_context(PayloadContext *data)
Definition: rtpdec_ac3.c:35
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1361
PayloadContext::fragment
AVIOContext * fragment
buffer for split payloads
Definition: rtpdec_ac3.c:32
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:98
RTP_AC3_PAYLOAD_HEADER_SIZE
#define RTP_AC3_PAYLOAD_HEADER_SIZE
Definition: rtpdec_ac3.c:26
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ac3_handle_packet
static int ac3_handle_packet(AVFormatContext *ctx, PayloadContext *data, AVStream *st, AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len, uint16_t seq, int flags)
Definition: rtpdec_ac3.c:40
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
ff_ac3_dynamic_handler
const RTPDynamicProtocolHandler ff_ac3_dynamic_handler
Definition: rtpdec_ac3.c:125
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:200
frame_type
frame_type
Definition: jpeg2000_parser.c:31
avio_internal.h
len
int len
Definition: vorbis_enc_data.h:426
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1434
AVStream
Stream structure.
Definition: avformat.h:743
avformat.h
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:749
AVPacket::stream_index
int stream_index
Definition: packet.h:524
parse_packet
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index, int flush)
Parse a packet, add all split parts to parse_queue.
Definition: demux.c:1154
AVPacket
This structure stores compressed data.
Definition: packet.h:499
PayloadContext::nr_frames
unsigned nr_frames
Definition: rtpdec_ac3.c:29
AVSTREAM_PARSE_FULL
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:593
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
PayloadContext
RTP/JPEG specific private data.
Definition: rdt.c:84
PayloadContext::last_frame
unsigned last_frame
Definition: rtpdec_ac3.c:30
RTPDynamicProtocolHandler
Definition: rtpdec.h:116