00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "avformat.h"
00029 #include "avc.h"
00030 #include "rtpenc.h"
00031
00032 static const uint8_t *avc_mp4_find_startcode(const uint8_t *start, const uint8_t *end, int nal_length_size)
00033 {
00034 int res = 0;
00035
00036 if (end - start < nal_length_size) {
00037 return NULL;
00038 }
00039 while (nal_length_size--) {
00040 res = (res << 8) | *start++;
00041 }
00042
00043 if (end - start < res) {
00044 return NULL;
00045 }
00046
00047 return res + start;
00048 }
00049
00050 static void nal_send(AVFormatContext *s1, const uint8_t *buf, int size, int last)
00051 {
00052 RTPMuxContext *s = s1->priv_data;
00053
00054 av_log(s1, AV_LOG_DEBUG, "Sending NAL %x of len %d M=%d\n", buf[0] & 0x1F, size, last);
00055 if (size <= s->max_payload_size) {
00056 ff_rtp_send_data(s1, buf, size, last);
00057 } else {
00058 uint8_t type = buf[0] & 0x1F;
00059 uint8_t nri = buf[0] & 0x60;
00060
00061 av_log(s1, AV_LOG_DEBUG, "NAL size %d > %d\n", size, s->max_payload_size);
00062 s->buf[0] = 28;
00063 s->buf[0] |= nri;
00064 s->buf[1] = type;
00065 s->buf[1] |= 1 << 7;
00066 buf += 1;
00067 size -= 1;
00068 while (size + 2 > s->max_payload_size) {
00069 memcpy(&s->buf[2], buf, s->max_payload_size - 2);
00070 ff_rtp_send_data(s1, s->buf, s->max_payload_size, 0);
00071 buf += s->max_payload_size - 2;
00072 size -= s->max_payload_size - 2;
00073 s->buf[1] &= ~(1 << 7);
00074 }
00075 s->buf[1] |= 1 << 6;
00076 memcpy(&s->buf[2], buf, size);
00077 ff_rtp_send_data(s1, s->buf, size + 2, last);
00078 }
00079 }
00080
00081 void ff_rtp_send_h264(AVFormatContext *s1, const uint8_t *buf1, int size)
00082 {
00083 const uint8_t *r;
00084 RTPMuxContext *s = s1->priv_data;
00085
00086 s->timestamp = s->cur_timestamp;
00087 r = s->nal_length_size ? (avc_mp4_find_startcode(buf1, buf1 + size, s->nal_length_size) ? buf1 : buf1 + size) : ff_avc_find_startcode(buf1, buf1 + size);
00088 while (r < buf1 + size) {
00089 const uint8_t *r1;
00090
00091 if (s->nal_length_size) {
00092 r1 = avc_mp4_find_startcode(r, buf1 + size, s->nal_length_size);
00093 if (!r1) {
00094 r1 = buf1 + size;
00095 }
00096 r += s->nal_length_size;
00097 } else {
00098 while(!*(r++));
00099 r1 = ff_avc_find_startcode(r, buf1 + size);
00100 }
00101 nal_send(s1, r, r1 - r, (r1 == buf1 + size));
00102 r = r1;
00103 }
00104 }