]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpenc_h263_rfc2190.c
mov: move stsd finalization to an appropriate place
[ffmpeg] / libavformat / rtpenc_h263_rfc2190.c
1 /*
2  * RTP packetization for H.263 video
3  * Copyright (c) 2012 Martin Storsjo
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 #include "libavcodec/put_bits.h"
24
25 #include "avformat.h"
26 #include "rtpenc.h"
27
28 struct H263Info {
29     int src;
30     int i;
31     int u;
32     int s;
33     int a;
34     int pb;
35     int tr;
36 };
37
38 struct H263State {
39     int gobn;
40     int mba;
41     int hmv1, vmv1, hmv2, vmv2;
42     int quant;
43 };
44
45 static void send_mode_a(AVFormatContext *s1, const struct H263Info *info,
46                         const uint8_t *buf, int len, int ebits, int m)
47 {
48     RTPMuxContext *s = s1->priv_data;
49     PutBitContext pb;
50
51     init_put_bits(&pb, s->buf, 32);
52     put_bits(&pb, 1, 0); /* F - 0, mode A */
53     put_bits(&pb, 1, 0); /* P - 0, normal I/P */
54     put_bits(&pb, 3, 0); /* SBIT - 0 bits */
55     put_bits(&pb, 3, ebits); /* EBIT */
56     put_bits(&pb, 3, info->src); /* SRC - source format */
57     put_bits(&pb, 1, info->i); /* I - inter/intra */
58     put_bits(&pb, 1, info->u); /* U - unrestricted motion vector */
59     put_bits(&pb, 1, info->s); /* S - syntax-baesd arithmetic coding */
60     put_bits(&pb, 1, info->a); /* A - advanced prediction */
61     put_bits(&pb, 4, 0); /* R - reserved */
62     put_bits(&pb, 2, 0); /* DBQ - 0 */
63     put_bits(&pb, 3, 0); /* TRB - 0 */
64     put_bits(&pb, 8, info->tr); /* TR */
65     flush_put_bits(&pb);
66     memcpy(s->buf + 4, buf, len);
67
68     ff_rtp_send_data(s1, s->buf, len + 4, m);
69 }
70
71 static void send_mode_b(AVFormatContext *s1, const struct H263Info *info,
72                         const struct H263State *state, const uint8_t *buf,
73                         int len, int sbits, int ebits, int m)
74 {
75     RTPMuxContext *s = s1->priv_data;
76     PutBitContext pb;
77
78     init_put_bits(&pb, s->buf, 64);
79     put_bits(&pb, 1, 1); /* F - 1, mode B */
80     put_bits(&pb, 1, 0); /* P - 0, mode B */
81     put_bits(&pb, 3, sbits); /* SBIT - 0 bits */
82     put_bits(&pb, 3, ebits); /* EBIT - 0 bits */
83     put_bits(&pb, 3, info->src); /* SRC - source format */
84     put_bits(&pb, 5, state->quant); /* QUANT - quantizer for the first MB */
85     put_bits(&pb, 5, state->gobn); /* GOBN - GOB number */
86     put_bits(&pb, 9, state->mba); /* MBA - MB address */
87     put_bits(&pb, 2, 0); /* R - reserved */
88     put_bits(&pb, 1, info->i); /* I - inter/intra */
89     put_bits(&pb, 1, info->u); /* U - unrestricted motion vector */
90     put_bits(&pb, 1, info->s); /* S - syntax-baesd arithmetic coding */
91     put_bits(&pb, 1, info->a); /* A - advanced prediction */
92     put_bits(&pb, 7, state->hmv1); /* HVM1 - horizontal motion vector 1 */
93     put_bits(&pb, 7, state->vmv1); /* VMV1 - vertical motion vector 1 */
94     put_bits(&pb, 7, state->hmv2); /* HVM2 - horizontal motion vector 2 */
95     put_bits(&pb, 7, state->vmv2); /* VMV2 - vertical motion vector 2 */
96     flush_put_bits(&pb);
97     memcpy(s->buf + 8, buf, len);
98
99     ff_rtp_send_data(s1, s->buf, len + 8, m);
100 }
101
102 void ff_rtp_send_h263_rfc2190(AVFormatContext *s1, const uint8_t *buf, int size,
103                               const uint8_t *mb_info, int mb_info_size)
104 {
105     RTPMuxContext *s = s1->priv_data;
106     int len, sbits = 0, ebits = 0;
107     BitstreamContext bc;
108     struct H263Info info = { 0 };
109     struct H263State state = { 0 };
110     int mb_info_pos = 0, mb_info_count = mb_info_size / 12;
111     const uint8_t *buf_base = buf;
112
113     s->timestamp = s->cur_timestamp;
114
115     bitstream_init8(&bc, buf, size);
116     if (bitstream_read(&bc, 22) == 0x20) { /* Picture Start Code */
117         info.tr  = bitstream_read(&bc, 8);
118         bitstream_skip(&bc, 2); /* PTYPE start, H.261 disambiguation */
119         bitstream_skip(&bc, 3); /* Split screen, document camera, freeze picture release */
120         info.src = bitstream_read(&bc, 3);
121         info.i   = bitstream_read(&bc, 1);
122         info.u   = bitstream_read(&bc, 1);
123         info.s   = bitstream_read(&bc, 1);
124         info.a   = bitstream_read(&bc, 1);
125         info.pb  = bitstream_read(&bc, 1);
126     }
127
128     while (size > 0) {
129         struct H263State packet_start_state = state;
130         len = FFMIN(s->max_payload_size - 8, size);
131
132         /* Look for a better place to split the frame into packets. */
133         if (len < size) {
134             const uint8_t *end = ff_h263_find_resync_marker_reverse(buf,
135                                                                     buf + len);
136             len = end - buf;
137             if (len == s->max_payload_size - 8) {
138                 /* Skip mb info prior to the start of the current ptr */
139                 while (mb_info_pos < mb_info_count) {
140                     uint32_t pos = AV_RL32(&mb_info[12*mb_info_pos])/8;
141                     if (pos >= buf - buf_base)
142                         break;
143                     mb_info_pos++;
144                 }
145                 /* Find the first mb info past the end pointer */
146                 while (mb_info_pos + 1 < mb_info_count) {
147                     uint32_t pos = AV_RL32(&mb_info[12*(mb_info_pos + 1)])/8;
148                     if (pos >= end - buf_base)
149                         break;
150                     mb_info_pos++;
151                 }
152                 if (mb_info_pos < mb_info_count) {
153                     const uint8_t *ptr = &mb_info[12*mb_info_pos];
154                     uint32_t bit_pos = AV_RL32(ptr);
155                     uint32_t pos = (bit_pos + 7)/8;
156                     if (pos <= end - buf_base) {
157                         state.quant = ptr[4];
158                         state.gobn  = ptr[5];
159                         state.mba   = AV_RL16(&ptr[6]);
160                         state.hmv1  = (int8_t) ptr[8];
161                         state.vmv1  = (int8_t) ptr[9];
162                         state.hmv2  = (int8_t) ptr[10];
163                         state.vmv2  = (int8_t) ptr[11];
164                         ebits = 8 * pos - bit_pos;
165                         len   = pos - (buf - buf_base);
166                         mb_info_pos++;
167                     } else {
168                         av_log(s1, AV_LOG_ERROR,
169                                "Unable to split H.263 packet, use -mb_info %d "
170                                "or lower.\n", s->max_payload_size - 8);
171                     }
172                 } else {
173                     av_log(s1, AV_LOG_ERROR, "Unable to split H.263 packet, "
174                            "use -mb_info %d or -ps 1.\n",
175                            s->max_payload_size - 8);
176                 }
177             }
178         }
179
180         if (size > 2 && !buf[0] && !buf[1])
181             send_mode_a(s1, &info, buf, len, ebits, len == size);
182         else
183             send_mode_b(s1, &info, &packet_start_state, buf, len, sbits,
184                         ebits, len == size);
185
186         if (ebits) {
187             sbits = 8 - ebits;
188             len--;
189         } else {
190             sbits = 0;
191         }
192         buf  += len;
193         size -= len;
194         ebits = 0;
195     }
196 }