• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/rtpenc_h264.c

Go to the documentation of this file.
00001 /*
00002  * RTP packetization for H.264 (RFC3984)
00003  * Copyright (c) 2008 Luca Abeni
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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;        /* FU Indicator; Type = 28 ---> FU-A */
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 }

Generated on Fri Feb 22 2013 07:24:32 for FFmpeg by  doxygen 1.7.1