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