OSDN Git Service

Merge commit '5d8cae45737bed6239bd6b6e0698802dbe1463c8'
[android-x86/external-ffmpeg.git] / libavformat / rtpdec_ac3.c
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 "rtpdec_formats.h"
24
25 #define RTP_AC3_PAYLOAD_HEADER_SIZE 2
26
27 struct PayloadContext {
28     unsigned nr_frames;
29     unsigned last_frame;
30     uint32_t timestamp;
31     AVIOContext *fragment;
32 };
33
34 static void free_fragment(PayloadContext *data)
35 {
36     if (data->fragment) {
37         uint8_t *p;
38         avio_close_dyn_buf(data->fragment, &p);
39         av_free(p);
40         data->fragment = NULL;
41     }
42 }
43
44 static void ac3_free_context(PayloadContext *data)
45 {
46     free_fragment(data);
47     av_free(data);
48 }
49
50 static int ac3_handle_packet(AVFormatContext *ctx, PayloadContext *data,
51                              AVStream *st, AVPacket *pkt, uint32_t *timestamp,
52                              const uint8_t *buf, int len, uint16_t seq,
53                              int flags)
54 {
55     unsigned frame_type;
56     unsigned nr_frames;
57     int err;
58
59     if (len < RTP_AC3_PAYLOAD_HEADER_SIZE + 1) {
60         av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
61         return AVERROR_INVALIDDATA;
62     }
63
64     frame_type = buf[0] & 0x3;
65     nr_frames = buf[1];
66     buf += RTP_AC3_PAYLOAD_HEADER_SIZE;
67     len -= RTP_AC3_PAYLOAD_HEADER_SIZE;
68
69     switch (frame_type) {
70     case 0: /* One or more complete frames */
71         if (!nr_frames) {
72             av_log(ctx, AV_LOG_ERROR, "Invalid AC3 packet data\n");
73             return AVERROR_INVALIDDATA;
74         }
75         if (av_new_packet(pkt, len)) {
76             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
77             return AVERROR(ENOMEM);
78         }
79
80         pkt->stream_index = st->index;
81         memcpy(pkt->data, buf, len);
82         return 0;
83
84     case 1:
85     case 2: /* First fragment */
86         free_fragment(data);
87
88         data->last_frame = 1;
89         data->nr_frames = nr_frames;
90         err = avio_open_dyn_buf(&data->fragment);
91         if (err < 0)
92             return err;
93
94         avio_write(data->fragment, buf, len);
95         data->timestamp = *timestamp;
96         return AVERROR(EAGAIN);
97
98     case 3: /* Fragment other than first */
99         if (!data->fragment) {
100             av_log(ctx, AV_LOG_WARNING,
101                    "Received packet without a start fragment; dropping.\n");
102             return AVERROR(EAGAIN);
103         }
104         if (nr_frames != data->nr_frames ||
105             data->timestamp != *timestamp) {
106             free_fragment(data);
107             av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
108             return AVERROR_INVALIDDATA;
109         }
110
111         avio_write(data->fragment, buf, len);
112         data->last_frame++;
113     }
114
115     if (!(flags & RTP_FLAG_MARKER))
116         return AVERROR(EAGAIN);
117
118     if (data->last_frame != data->nr_frames) {
119         free_fragment(data);
120         av_log(ctx, AV_LOG_ERROR, "Missed %d packets\n",
121                data->nr_frames - data->last_frame);
122         return AVERROR_INVALIDDATA;
123     }
124
125     err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
126     if (err < 0) {
127         av_log(ctx, AV_LOG_ERROR,
128                "Error occurred when getting fragment buffer.\n");
129         return err;
130     }
131
132     return 0;
133 }
134
135 RTPDynamicProtocolHandler ff_ac3_dynamic_handler = {
136     .enc_name           = "ac3",
137     .codec_type         = AVMEDIA_TYPE_AUDIO,
138     .codec_id           = AV_CODEC_ID_AC3,
139     .need_parsing       = AVSTREAM_PARSE_FULL,
140     .priv_data_size     = sizeof(PayloadContext),
141     .free               = ac3_free_context,
142     .parse_packet       = ac3_handle_packet,
143 };