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