]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpdec_h261.c
configure: Drop weak dependencies on external libraries for webm muxer
[ffmpeg] / libavformat / rtpdec_h261.c
1 /*
2  * RTP parser for H.261 payload format (RFC 4587)
3  * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
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 "libavcodec/bitstream.h"
23
24 #include "avformat.h"
25 #include "avio_internal.h"
26 #include "rtpdec_formats.h"
27
28 #define RTP_H261_PAYLOAD_HEADER_SIZE 4
29
30 struct PayloadContext {
31     AVIOContext *buf;
32     uint8_t      endbyte;
33     int          endbyte_bits;
34     uint32_t     timestamp;
35 };
36
37 static av_cold void h261_close_context(PayloadContext *pl_ctx)
38 {
39     /* return if context is invalid */
40     if (!pl_ctx)
41         return;
42
43     /* free buffer if it is valid */
44     ffio_free_dyn_buf(&pl_ctx->buf);
45 }
46
47 static int h261_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_h261_ctx,
48                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
49                               const uint8_t *buf, int len, uint16_t seq,
50                               int flags)
51 {
52     int sbit, ebit, gobn, mbap, quant;
53     int res;
54
55     /* drop data of previous packets in case of non-continuous (lossy) packet stream */
56     if (rtp_h261_ctx->buf && rtp_h261_ctx->timestamp != *timestamp) {
57         ffio_free_dyn_buf(&rtp_h261_ctx->buf);
58         rtp_h261_ctx->endbyte_bits = 0;
59     }
60
61     /* sanity check for size of input packet: 1 byte payload at least */
62     if (len < RTP_H261_PAYLOAD_HEADER_SIZE + 1) {
63         av_log(ctx, AV_LOG_ERROR, "Too short RTP/H.261 packet, got %d bytes\n", len);
64         return AVERROR_INVALIDDATA;
65     }
66
67     /*
68      * decode the H.261 payload header according to section 4.1 of RFC 4587:
69      * (uses 4 bytes between RTP header and H.261 stream per packet)
70      *
71      *    0                   1                   2                   3
72      *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
73      *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74      *   |SBIT |EBIT |I|V| GOBN  |   MBAP  |  QUANT  |  HMVD   |  VMVD   |
75      *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76      *
77      *      Start bit position (SBIT): 3 bits
78      *      End bit position (EBIT): 3 bits
79      *      INTRA-frame encoded data (I): 1 bit
80      *      Motion Vector flag (V): 1 bit
81      *      GOB number (GOBN): 4 bits
82      *      Macroblock address predictor (MBAP): 5 bits
83      *      Quantizer (QUANT): 5 bits
84      *      Horizontal motion vector data (HMVD): 5 bits
85      *      Vertical motion vector data (VMVD): 5 bits
86      */
87     sbit  =  (buf[0] >> 5) & 0x07;
88     ebit  =  (buf[0] >> 2) & 0x07;
89     gobn  =  (buf[1] >> 4) & 0x0f;
90     mbap  = ((buf[1] << 1) & 0x1e) | ((buf[2] >> 7) & 0x01);
91     quant =  (buf[2] >> 2) & 0x1f;
92
93     /* pass the H.261 payload header and continue with the actual payload */
94     buf += RTP_H261_PAYLOAD_HEADER_SIZE;
95     len -= RTP_H261_PAYLOAD_HEADER_SIZE;
96
97     /* start frame buffering with new dynamic buffer */
98     if (!rtp_h261_ctx->buf) {
99         /* sanity check: a new frame starts with gobn=0, sbit=0, mbap=0, quant=0 */
100         if (!gobn && !sbit && !mbap && !quant) {
101             res = avio_open_dyn_buf(&rtp_h261_ctx->buf);
102             if (res < 0)
103                 return res;
104             /* update the timestamp in the frame packet with the one from the RTP packet */
105             rtp_h261_ctx->timestamp = *timestamp;
106         } else {
107             /* frame not started yet, need more packets */
108             return AVERROR(EAGAIN);
109         }
110     }
111
112     /* do the "byte merging" at the boundaries of two consecutive frame fragments */
113     if (rtp_h261_ctx->endbyte_bits || sbit) {
114         if (rtp_h261_ctx->endbyte_bits == sbit) {
115             rtp_h261_ctx->endbyte     |= buf[0] & (0xff >> sbit);
116             rtp_h261_ctx->endbyte_bits = 0;
117             buf++;
118             len--;
119             avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
120         } else {
121             /* ebit/sbit values inconsistent, assuming packet loss */
122             BitstreamContext bc;
123             bitstream_init(&bc, buf, len * 8 - ebit);
124             bitstream_skip(&bc, sbit);
125             if (rtp_h261_ctx->endbyte_bits) {
126                 rtp_h261_ctx->endbyte |= bitstream_read(&bc, 8 - rtp_h261_ctx->endbyte_bits);
127                 avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
128             }
129             while (bitstream_bits_left(&bc) >= 8)
130                 avio_w8(rtp_h261_ctx->buf, bitstream_read(&bc, 8));
131             rtp_h261_ctx->endbyte_bits = bitstream_bits_left(&bc);
132             if (rtp_h261_ctx->endbyte_bits)
133                 rtp_h261_ctx->endbyte = bitstream_read(&bc, rtp_h261_ctx->endbyte_bits) <<
134                                         (8 - rtp_h261_ctx->endbyte_bits);
135             ebit = 0;
136             len  = 0;
137         }
138     }
139     if (ebit) {
140         if (len > 0)
141             avio_write(rtp_h261_ctx->buf, buf, len - 1);
142         rtp_h261_ctx->endbyte_bits = 8 - ebit;
143         rtp_h261_ctx->endbyte      = buf[len - 1] & (0xff << ebit);
144     } else {
145         avio_write(rtp_h261_ctx->buf, buf, len);
146     }
147
148     /* RTP marker bit means: last fragment of current frame was received;
149        otherwise, an additional fragment is needed for the current frame */
150     if (!(flags & RTP_FLAG_MARKER))
151         return AVERROR(EAGAIN);
152
153     /* write the completed last byte from the "byte merging" */
154     if (rtp_h261_ctx->endbyte_bits)
155         avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
156     rtp_h261_ctx->endbyte_bits = 0;
157
158     /* close frame buffering and create resulting A/V packet */
159     res = ff_rtp_finalize_packet(pkt, &rtp_h261_ctx->buf, st->index);
160     if (res < 0)
161         return res;
162
163     return 0;
164 }
165
166 RTPDynamicProtocolHandler ff_h261_dynamic_handler = {
167     .enc_name          = "H261",
168     .codec_type        = AVMEDIA_TYPE_VIDEO,
169     .codec_id          = AV_CODEC_ID_H261,
170     .need_parsing      = AVSTREAM_PARSE_FULL,
171     .priv_data_size    = sizeof(PayloadContext),
172     .close             = h261_close_context,
173     .parse_packet      = h261_handle_packet,
174     .static_payload_id = 31,
175 };