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