LCOV - code coverage report
Current view: top level - libavformat - avc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 57 145 39.3 %
Date: 2017-12-15 18:13:28 Functions: 5 7 71.4 %

          Line data    Source code
       1             : /*
       2             :  * AVC helper functions for muxers
       3             :  * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
       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 "libavutil/intreadwrite.h"
      23             : #include "libavcodec/h264.h"
      24             : #include "avformat.h"
      25             : #include "avio.h"
      26             : #include "avc.h"
      27             : 
      28         434 : static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
      29             : {
      30         434 :     const uint8_t *a = p + 4 - ((intptr_t)p & 3);
      31             : 
      32        1315 :     for (end -= 3; p < a && p < end; p++) {
      33         930 :         if (p[0] == 0 && p[1] == 0 && p[2] == 1)
      34          49 :             return p;
      35             :     }
      36             : 
      37       16488 :     for (end -= 3; p < end; p += 4) {
      38       16439 :         uint32_t x = *(const uint32_t*)p;
      39             : //      if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
      40             : //      if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
      41       16439 :         if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
      42         676 :             if (p[1] == 0) {
      43         260 :                 if (p[0] == 0 && p[2] == 1)
      44          89 :                     return p;
      45         171 :                 if (p[2] == 0 && p[3] == 1)
      46          78 :                     return p+1;
      47             :             }
      48         509 :             if (p[3] == 0) {
      49         275 :                 if (p[2] == 0 && p[4] == 1)
      50          89 :                     return p+2;
      51         186 :                 if (p[4] == 0 && p[5] == 1)
      52          80 :                     return p+3;
      53             :             }
      54             :         }
      55             :     }
      56             : 
      57         123 :     for (end += 3; p < end; p++) {
      58          74 :         if (p[0] == 0 && p[1] == 0 && p[2] == 1)
      59           0 :             return p;
      60             :     }
      61             : 
      62          49 :     return end + 3;
      63             : }
      64             : 
      65         434 : const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){
      66         434 :     const uint8_t *out= ff_avc_find_startcode_internal(p, end);
      67         434 :     if(p<out && out<end && !out[-1]) out--;
      68         434 :     return out;
      69             : }
      70             : 
      71          49 : int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
      72             : {
      73          49 :     const uint8_t *p = buf_in;
      74          49 :     const uint8_t *end = p + size;
      75             :     const uint8_t *nal_start, *nal_end;
      76             : 
      77          49 :     size = 0;
      78          49 :     nal_start = ff_avc_find_startcode(p, end);
      79             :     for (;;) {
      80         819 :         while (nal_start < end && !*(nal_start++));
      81         434 :         if (nal_start == end)
      82          49 :             break;
      83             : 
      84         385 :         nal_end = ff_avc_find_startcode(nal_start, end);
      85         385 :         avio_wb32(pb, nal_end - nal_start);
      86         385 :         avio_write(pb, nal_start, nal_end - nal_start);
      87         385 :         size += 4 + nal_end - nal_start;
      88         385 :         nal_start = nal_end;
      89             :     }
      90          49 :     return size;
      91             : }
      92             : 
      93           1 : int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
      94             : {
      95             :     AVIOContext *pb;
      96           1 :     int ret = avio_open_dyn_buf(&pb);
      97           1 :     if(ret < 0)
      98           0 :         return ret;
      99             : 
     100           1 :     ff_avc_parse_nal_units(pb, buf_in, *size);
     101             : 
     102           1 :     av_freep(buf);
     103           1 :     *size = avio_close_dyn_buf(pb, buf);
     104           1 :     return 0;
     105             : }
     106             : 
     107          49 : int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
     108             : {
     109          49 :     AVIOContext *sps_pb = NULL, *pps_pb = NULL;
     110          49 :     uint8_t *buf = NULL, *end, *start = NULL;
     111          49 :     uint8_t *sps = NULL, *pps = NULL;
     112          49 :     uint32_t sps_size = 0, pps_size = 0;
     113          49 :     int ret, nb_sps = 0, nb_pps = 0;
     114             : 
     115          49 :     if (len <= 6)
     116           0 :         return AVERROR_INVALIDDATA;
     117             : 
     118             :     /* check for H.264 start code */
     119          98 :     if (AV_RB32(data) != 0x00000001 &&
     120          49 :         AV_RB24(data) != 0x000001) {
     121          49 :         avio_write(pb, data, len);
     122          49 :         return 0;
     123             :     }
     124             : 
     125           0 :     ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
     126           0 :     if (ret < 0)
     127           0 :         return ret;
     128           0 :     start = buf;
     129           0 :     end = buf + len;
     130             : 
     131           0 :     ret = avio_open_dyn_buf(&sps_pb);
     132           0 :     if (ret < 0)
     133           0 :         goto fail;
     134           0 :     ret = avio_open_dyn_buf(&pps_pb);
     135           0 :     if (ret < 0)
     136           0 :         goto fail;
     137             : 
     138             :     /* look for sps and pps */
     139           0 :     while (end - buf > 4) {
     140             :         uint32_t size;
     141             :         uint8_t nal_type;
     142           0 :         size = FFMIN(AV_RB32(buf), end - buf - 4);
     143           0 :         buf += 4;
     144           0 :         nal_type = buf[0] & 0x1f;
     145             : 
     146           0 :         if (nal_type == 7) { /* SPS */
     147           0 :             nb_sps++;
     148           0 :             if (size > UINT16_MAX || nb_sps >= H264_MAX_SPS_COUNT) {
     149           0 :                 ret = AVERROR_INVALIDDATA;
     150           0 :                 goto fail;
     151             :             }
     152           0 :             avio_wb16(sps_pb, size);
     153           0 :             avio_write(sps_pb, buf, size);
     154           0 :         } else if (nal_type == 8) { /* PPS */
     155           0 :             nb_pps++;
     156           0 :             if (size > UINT16_MAX || nb_pps >= H264_MAX_PPS_COUNT) {
     157           0 :                 ret = AVERROR_INVALIDDATA;
     158           0 :                 goto fail;
     159             :             }
     160           0 :             avio_wb16(pps_pb, size);
     161           0 :             avio_write(pps_pb, buf, size);
     162             :         }
     163             : 
     164           0 :         buf += size;
     165             :     }
     166           0 :     sps_size = avio_close_dyn_buf(sps_pb, &sps);
     167           0 :     pps_size = avio_close_dyn_buf(pps_pb, &pps);
     168             : 
     169           0 :     if (sps_size < 6 || !pps_size) {
     170           0 :         ret = AVERROR_INVALIDDATA;
     171           0 :         goto fail;
     172             :     }
     173             : 
     174           0 :     avio_w8(pb, 1); /* version */
     175           0 :     avio_w8(pb, sps[3]); /* profile */
     176           0 :     avio_w8(pb, sps[4]); /* profile compat */
     177           0 :     avio_w8(pb, sps[5]); /* level */
     178           0 :     avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
     179           0 :     avio_w8(pb, 0xe0 | nb_sps); /* 3 bits reserved (111) + 5 bits number of sps */
     180             : 
     181           0 :     avio_write(pb, sps, sps_size);
     182           0 :     avio_w8(pb, nb_pps); /* number of pps */
     183           0 :     avio_write(pb, pps, pps_size);
     184             : 
     185           0 : fail:
     186           0 :     if (!sps)
     187           0 :         avio_close_dyn_buf(sps_pb, &sps);
     188           0 :     if (!pps)
     189           0 :         avio_close_dyn_buf(pps_pb, &pps);
     190           0 :     av_free(sps);
     191           0 :     av_free(pps);
     192           0 :     av_free(start);
     193             : 
     194           0 :     return ret;
     195             : }
     196             : 
     197           0 : int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
     198             : {
     199             :     uint16_t sps_size, pps_size;
     200             :     uint8_t *out;
     201             :     int out_size;
     202             : 
     203           0 :     *buf = NULL;
     204           0 :     if (*size >= 4 && (AV_RB32(in) == 0x00000001 || AV_RB24(in) == 0x000001))
     205           0 :         return 0;
     206           0 :     if (*size < 11 || in[0] != 1)
     207           0 :         return AVERROR_INVALIDDATA;
     208             : 
     209           0 :     sps_size = AV_RB16(&in[6]);
     210           0 :     if (11 + sps_size > *size)
     211           0 :         return AVERROR_INVALIDDATA;
     212           0 :     pps_size = AV_RB16(&in[9 + sps_size]);
     213           0 :     if (11 + sps_size + pps_size > *size)
     214           0 :         return AVERROR_INVALIDDATA;
     215           0 :     out_size = 8 + sps_size + pps_size;
     216           0 :     out = av_mallocz(out_size + AV_INPUT_BUFFER_PADDING_SIZE);
     217           0 :     if (!out)
     218           0 :         return AVERROR(ENOMEM);
     219           0 :     AV_WB32(&out[0], 0x00000001);
     220           0 :     memcpy(out + 4, &in[8], sps_size);
     221           0 :     AV_WB32(&out[4 + sps_size], 0x00000001);
     222           0 :     memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size);
     223           0 :     *buf = out;
     224           0 :     *size = out_size;
     225           0 :     return 0;
     226             : }
     227             : 
     228           0 : const uint8_t *ff_avc_mp4_find_startcode(const uint8_t *start,
     229             :                                          const uint8_t *end,
     230             :                                          int nal_length_size)
     231             : {
     232           0 :     unsigned int res = 0;
     233             : 
     234           0 :     if (end - start < nal_length_size)
     235           0 :         return NULL;
     236           0 :     while (nal_length_size--)
     237           0 :         res = (res << 8) | *start++;
     238             : 
     239           0 :     if (res > end - start)
     240           0 :         return NULL;
     241             : 
     242           0 :     return start + res;
     243             : }

Generated by: LCOV version 1.13