2 * RTP packetization for H.263 video
3 * Copyright (c) 2012 Martin Storsjo
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavcodec/put_bits.h"
25 #include "libavcodec/get_bits.h"
40 int hmv1, vmv1, hmv2, vmv2;
44 static void send_mode_a(AVFormatContext *s1, const struct H263Info *info,
45 const uint8_t *buf, int len, int ebits, int m)
47 RTPMuxContext *s = s1->priv_data;
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 */
65 memcpy(s->buf + 4, buf, len);
67 ff_rtp_send_data(s1, s->buf, len + 4, m);
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)
74 RTPMuxContext *s = s1->priv_data;
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 */
96 memcpy(s->buf + 8, buf, len);
98 ff_rtp_send_data(s1, s->buf, len + 8, m);
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)
104 RTPMuxContext *s = s1->priv_data;
105 int len, sbits = 0, ebits = 0;
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;
112 s->timestamp = s->cur_timestamp;
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);
128 struct H263State packet_start_state = state;
129 len = FFMIN(s->max_payload_size - 8, size);
131 /* Look for a better place to split the frame into packets. */
133 const uint8_t *end = ff_h263_find_resync_marker_reverse(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)
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)
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];
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);
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);
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);
179 if (size > 2 && !buf[0] && !buf[1])
180 send_mode_a(s1, &info, buf, len, ebits, len == size);
182 send_mode_b(s1, &info, &packet_start_state, buf, len, sbits,