00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "rtpenc.h"
00024 #include "libavcodec/put_bits.h"
00025 #include "libavcodec/get_bits.h"
00026
00027 struct H263Info {
00028 int src;
00029 int i;
00030 int u;
00031 int s;
00032 int a;
00033 int pb;
00034 int tr;
00035 };
00036
00037 struct H263State {
00038 int gobn;
00039 int mba;
00040 int hmv1, vmv1, hmv2, vmv2;
00041 int quant;
00042 };
00043
00044 static void send_mode_a(AVFormatContext *s1, const struct H263Info *info,
00045 const uint8_t *buf, int len, int ebits, int m)
00046 {
00047 RTPMuxContext *s = s1->priv_data;
00048 PutBitContext pb;
00049
00050 init_put_bits(&pb, s->buf, 32);
00051 put_bits(&pb, 1, 0);
00052 put_bits(&pb, 1, 0);
00053 put_bits(&pb, 3, 0);
00054 put_bits(&pb, 3, ebits);
00055 put_bits(&pb, 3, info->src);
00056 put_bits(&pb, 1, info->i);
00057 put_bits(&pb, 1, info->u);
00058 put_bits(&pb, 1, info->s);
00059 put_bits(&pb, 1, info->a);
00060 put_bits(&pb, 4, 0);
00061 put_bits(&pb, 2, 0);
00062 put_bits(&pb, 3, 0);
00063 put_bits(&pb, 8, info->tr);
00064 flush_put_bits(&pb);
00065 memcpy(s->buf + 4, buf, len);
00066
00067 ff_rtp_send_data(s1, s->buf, len + 4, m);
00068 }
00069
00070 static void send_mode_b(AVFormatContext *s1, const struct H263Info *info,
00071 const struct H263State *state, const uint8_t *buf,
00072 int len, int sbits, int ebits, int m)
00073 {
00074 RTPMuxContext *s = s1->priv_data;
00075 PutBitContext pb;
00076
00077 init_put_bits(&pb, s->buf, 64);
00078 put_bits(&pb, 1, 1);
00079 put_bits(&pb, 1, 0);
00080 put_bits(&pb, 3, sbits);
00081 put_bits(&pb, 3, ebits);
00082 put_bits(&pb, 3, info->src);
00083 put_bits(&pb, 5, state->quant);
00084 put_bits(&pb, 5, state->gobn);
00085 put_bits(&pb, 9, state->mba);
00086 put_bits(&pb, 2, 0);
00087 put_bits(&pb, 1, info->i);
00088 put_bits(&pb, 1, info->u);
00089 put_bits(&pb, 1, info->s);
00090 put_bits(&pb, 1, info->a);
00091 put_bits(&pb, 7, state->hmv1);
00092 put_bits(&pb, 7, state->vmv1);
00093 put_bits(&pb, 7, state->hmv2);
00094 put_bits(&pb, 7, state->vmv2);
00095 flush_put_bits(&pb);
00096 memcpy(s->buf + 8, buf, len);
00097
00098 ff_rtp_send_data(s1, s->buf, len + 8, m);
00099 }
00100
00101 void ff_rtp_send_h263_rfc2190(AVFormatContext *s1, const uint8_t *buf, int size,
00102 const uint8_t *mb_info, int mb_info_size)
00103 {
00104 RTPMuxContext *s = s1->priv_data;
00105 int len, sbits = 0, ebits = 0;
00106 GetBitContext gb;
00107 struct H263Info info = { 0 };
00108 struct H263State state = { 0 };
00109 int mb_info_pos = 0, mb_info_count = mb_info_size / 12;
00110 const uint8_t *buf_base = buf;
00111
00112 s->timestamp = s->cur_timestamp;
00113
00114 init_get_bits(&gb, buf, size*8);
00115 if (get_bits(&gb, 22) == 0x20) {
00116 info.tr = get_bits(&gb, 8);
00117 skip_bits(&gb, 2);
00118 skip_bits(&gb, 3);
00119 info.src = get_bits(&gb, 3);
00120 info.i = get_bits(&gb, 1);
00121 info.u = get_bits(&gb, 1);
00122 info.s = get_bits(&gb, 1);
00123 info.a = get_bits(&gb, 1);
00124 info.pb = get_bits(&gb, 1);
00125 }
00126
00127 while (size > 0) {
00128 struct H263State packet_start_state = state;
00129 len = FFMIN(s->max_payload_size - 8, size);
00130
00131
00132 if (len < size) {
00133 const uint8_t *end = ff_h263_find_resync_marker_reverse(buf,
00134 buf + len);
00135 len = end - buf;
00136 if (len == s->max_payload_size - 8) {
00137
00138 while (mb_info_pos < mb_info_count) {
00139 uint32_t pos = AV_RL32(&mb_info[12*mb_info_pos])/8;
00140 if (pos >= buf - buf_base)
00141 break;
00142 mb_info_pos++;
00143 }
00144
00145 while (mb_info_pos + 1 < mb_info_count) {
00146 uint32_t pos = AV_RL32(&mb_info[12*(mb_info_pos + 1)])/8;
00147 if (pos >= end - buf_base)
00148 break;
00149 mb_info_pos++;
00150 }
00151 if (mb_info_pos < mb_info_count) {
00152 const uint8_t *ptr = &mb_info[12*mb_info_pos];
00153 uint32_t bit_pos = AV_RL32(ptr);
00154 uint32_t pos = (bit_pos + 7)/8;
00155 if (pos <= end - buf_base) {
00156 state.quant = ptr[4];
00157 state.gobn = ptr[5];
00158 state.mba = AV_RL16(&ptr[6]);
00159 state.hmv1 = (int8_t) ptr[8];
00160 state.vmv1 = (int8_t) ptr[9];
00161 state.hmv2 = (int8_t) ptr[10];
00162 state.vmv2 = (int8_t) ptr[11];
00163 ebits = 8 * pos - bit_pos;
00164 len = pos - (buf - buf_base);
00165 mb_info_pos++;
00166 } else {
00167 av_log(s1, AV_LOG_ERROR,
00168 "Unable to split H263 packet, use -mb_info %d "
00169 "or lower.\n", s->max_payload_size - 8);
00170 }
00171 } else {
00172 av_log(s1, AV_LOG_ERROR, "Unable to split H263 packet, "
00173 "use -mb_info %d or -ps 1.\n",
00174 s->max_payload_size - 8);
00175 }
00176 }
00177 }
00178
00179 if (size > 2 && !buf[0] && !buf[1])
00180 send_mode_a(s1, &info, buf, len, ebits, len == size);
00181 else
00182 send_mode_b(s1, &info, &packet_start_state, buf, len, sbits,
00183 ebits, len == size);
00184
00185 if (ebits) {
00186 sbits = 8 - ebits;
00187 len--;
00188 } else {
00189 sbits = 0;
00190 }
00191 buf += len;
00192 size -= len;
00193 ebits = 0;
00194 }
00195 }