libavcodec/h264_mp4toannexb_bsf.c
bdfae2a5
 /*
ec21c215
  * H.264 MP4 to Annex B byte stream format filter
074bfa7d
  * Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
bdfae2a5
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
1d9c2dc8
 #include <string.h>
 
6a5d31ac
 #include "libavutil/intreadwrite.h"
1d9c2dc8
 #include "libavutil/mem.h"
33d18982
 
bdfae2a5
 #include "avcodec.h"
33d18982
 #include "bsf.h"
bdfae2a5
 
 typedef struct H264BSFContext {
1cf4d2e9
     int32_t  sps_offset;
     int32_t  pps_offset;
bdfae2a5
     uint8_t  length_size;
f9bd6d61
     uint8_t  new_idr;
1cf4d2e9
     uint8_t  idr_sps_seen;
     uint8_t  idr_pps_seen;
d5cc1ed7
     int      extradata_parsed;
bdfae2a5
 } H264BSFContext;
 
33d18982
 static int alloc_and_copy(AVPacket *out,
bb5cfc48
                           const uint8_t *sps_pps, uint32_t sps_pps_size,
5d21ca45
                           const uint8_t *in, uint32_t in_size)
 {
33d18982
     uint32_t offset         = out->size;
bdfae2a5
     uint8_t nal_header_size = offset ? 3 : 4;
9b8d11a7
     int err;
bdfae2a5
 
33d18982
     err = av_grow_packet(out, sps_pps_size + in_size + nal_header_size);
     if (err < 0)
9b8d11a7
         return err;
33d18982
 
bdfae2a5
     if (sps_pps)
33d18982
         memcpy(out->data + offset, sps_pps, sps_pps_size);
     memcpy(out->data + sps_pps_size + nal_header_size + offset, in, in_size);
1f7d2f54
     if (!offset) {
33d18982
         AV_WB32(out->data + sps_pps_size, 1);
1f7d2f54
     } else {
33d18982
         (out->data + offset + sps_pps_size)[0] =
         (out->data + offset + sps_pps_size)[1] = 0;
         (out->data + offset + sps_pps_size)[2] = 1;
bdfae2a5
     }
639c697c
 
     return 0;
bdfae2a5
 }
 
33d18982
 static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding)
8d929afd
 {
af9cac1b
     H264BSFContext *s = ctx->priv_data;
8d929afd
     uint16_t unit_size;
     uint64_t total_size                 = 0;
     uint8_t *out                        = NULL, unit_nb, sps_done = 0,
              sps_seen                   = 0, pps_seen = 0;
33d18982
     const uint8_t *extradata            = ctx->par_in->extradata + 4;
8d929afd
     static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
     int length_size = (*extradata++ & 0x3) + 1; // retrieve length coded size
 
af9cac1b
     s->sps_offset = s->pps_offset = -1;
1cf4d2e9
 
8d929afd
     /* retrieve sps and pps unit(s) */
     unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
     if (!unit_nb) {
0a5d22a1
         goto pps;
8d929afd
     } else {
af9cac1b
         s->sps_offset = 0;
8d929afd
         sps_seen = 1;
     }
 
     while (unit_nb--) {
9b8d11a7
         int err;
8d929afd
 
         unit_size   = AV_RB16(extradata);
         total_size += unit_size + 4;
53c853e0
         if (total_size > INT_MAX - padding) {
af9cac1b
             av_log(ctx, AV_LOG_ERROR,
53c853e0
                    "Too big extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
             av_free(out);
             return AVERROR(EINVAL);
         }
af9cac1b
         if (extradata + 2 + unit_size > ctx->par_in->extradata + ctx->par_in->extradata_size) {
             av_log(ctx, AV_LOG_ERROR, "Packet header is not contained in global extradata, "
53c853e0
                    "corrupted stream or invalid MP4/AVCC bitstream\n");
8d929afd
             av_free(out);
             return AVERROR(EINVAL);
         }
9b8d11a7
         if ((err = av_reallocp(&out, total_size + padding)) < 0)
             return err;
8d929afd
         memcpy(out + total_size - unit_size - 4, nalu_header, 4);
         memcpy(out + total_size - unit_size, extradata + 2, unit_size);
         extradata += 2 + unit_size;
0a5d22a1
 pps:
8d929afd
         if (!unit_nb && !sps_done++) {
             unit_nb = *extradata++; /* number of pps unit(s) */
1cf4d2e9
             if (unit_nb) {
af9cac1b
                 s->pps_offset = total_size;
8d929afd
                 pps_seen = 1;
1cf4d2e9
             }
8d929afd
         }
     }
 
     if (out)
d5ddcb5f
         memset(out + total_size, 0, padding);
8d929afd
 
     if (!sps_seen)
33d18982
         av_log(ctx, AV_LOG_WARNING,
8d929afd
                "Warning: SPS NALU missing or invalid. "
                "The resulting stream may not play.\n");
 
     if (!pps_seen)
33d18982
         av_log(ctx, AV_LOG_WARNING,
8d929afd
                "Warning: PPS NALU missing or invalid. "
                "The resulting stream may not play.\n");
 
33d18982
     av_freep(&ctx->par_out->extradata);
     ctx->par_out->extradata      = out;
     ctx->par_out->extradata_size = total_size;
8d929afd
 
     return length_size;
 }
 
33d18982
 static int h264_mp4toannexb_init(AVBSFContext *ctx)
5d21ca45
 {
33d18982
     H264BSFContext *s = ctx->priv_data;
f3ed4849
     int extra_size = ctx->par_in->extradata_size;
33d18982
     int ret;
 
     /* retrieve sps and pps NAL units from extradata */
f3ed4849
     if (!extra_size                                               ||
         (extra_size >= 3 && AV_RB24(ctx->par_in->extradata) == 1) ||
         (extra_size >= 4 && AV_RB32(ctx->par_in->extradata) == 1)) {
         av_log(ctx, AV_LOG_VERBOSE,
                "The input looks like it is Annex B already\n");
     } else if (extra_size >= 6) {
33d18982
         ret = h264_extradata_to_annexb(ctx, AV_INPUT_BUFFER_PADDING_SIZE);
         if (ret < 0)
             return ret;
 
         s->length_size      = ret;
af9cac1b
         s->new_idr          = 1;
         s->idr_sps_seen     = 0;
         s->idr_pps_seen     = 0;
33d18982
         s->extradata_parsed = 1;
f3ed4849
     } else {
         av_log(ctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", extra_size);
         return AVERROR_INVALIDDATA;
33d18982
     }
 
     return 0;
 }
 
 static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
 {
     H264BSFContext *s = ctx->priv_data;
 
     AVPacket *in;
bdfae2a5
     uint8_t unit_type;
52486603
     int32_t nal_size;
5d21ca45
     uint32_t cumul_size    = 0;
33d18982
     const uint8_t *buf;
     const uint8_t *buf_end;
     int            buf_size;
af9cac1b
     int ret = 0, i;
bdfae2a5
 
33d18982
     ret = ff_bsf_get_packet(ctx, &in);
     if (ret < 0)
         return ret;
bdfae2a5
 
     /* nothing to filter */
33d18982
     if (!s->extradata_parsed) {
         av_packet_move_ref(out, in);
         av_packet_free(&in);
bdfae2a5
         return 0;
     }
 
33d18982
     buf      = in->data;
     buf_size = in->size;
     buf_end  = in->data + in->size;
1defff85
 
bdfae2a5
     do {
7ae251b4
         ret= AVERROR(EINVAL);
33d18982
         if (buf + s->length_size > buf_end)
52486603
             goto fail;
 
af9cac1b
         for (nal_size = 0, i = 0; i<s->length_size; i++)
f1fdd208
             nal_size = (nal_size << 8) | buf[i];
bdfae2a5
 
33d18982
         buf += s->length_size;
bdfae2a5
         unit_type = *buf & 0x1f;
 
2bb54b82
         if (nal_size > buf_end - buf || nal_size < 0)
52486603
             goto fail;
 
1cf4d2e9
         if (unit_type == 7)
af9cac1b
             s->idr_sps_seen = s->new_idr = 1;
1cf4d2e9
         else if (unit_type == 8) {
af9cac1b
             s->idr_pps_seen = s->new_idr = 1;
1cf4d2e9
             /* if SPS has not been seen yet, prepend the AVCC one to PPS */
af9cac1b
             if (!s->idr_sps_seen) {
                 if (s->sps_offset == -1)
                     av_log(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n");
1cf4d2e9
                 else {
af9cac1b
                     if ((ret = alloc_and_copy(out,
                                          ctx->par_out->extradata + s->sps_offset,
                                          s->pps_offset != -1 ? s->pps_offset : ctx->par_out->extradata_size - s->sps_offset,
1cf4d2e9
                                          buf, nal_size)) < 0)
                         goto fail;
af9cac1b
                     s->idr_sps_seen = 1;
1cf4d2e9
                     goto next_nal;
                 }
             }
         }
ad91bf85
 
bf428bb3
         /* if this is a new IDR picture following an IDR picture, reset the idr flag.
          * Just check first_mb_in_slice to be 0 as this is the simplest solution.
          * This could be checking idr_pic_id instead, but would complexify the parsing. */
af9cac1b
         if (!s->new_idr && unit_type == 5 && (buf[1] & 0x80))
             s->new_idr = 1;
ad91bf85
 
         /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */
af9cac1b
         if (s->new_idr && unit_type == 5 && !s->idr_sps_seen && !s->idr_pps_seen) {
             if ((ret=alloc_and_copy(out,
33d18982
                                ctx->par_out->extradata, ctx->par_out->extradata_size,
7ae251b4
                                buf, nal_size)) < 0)
639c697c
                 goto fail;
af9cac1b
             s->new_idr = 0;
1cf4d2e9
         /* if only SPS has been seen, also insert PPS */
af9cac1b
         } else if (s->new_idr && unit_type == 5 && s->idr_sps_seen && !s->idr_pps_seen) {
             if (s->pps_offset == -1) {
                 av_log(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n");
                 if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
1cf4d2e9
                     goto fail;
af9cac1b
             } else if ((ret = alloc_and_copy(out,
                                         ctx->par_out->extradata + s->pps_offset, ctx->par_out->extradata_size - s->pps_offset,
1cf4d2e9
                                         buf, nal_size)) < 0)
                 goto fail;
1f7d2f54
         } else {
af9cac1b
             if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
639c697c
                 goto fail;
af9cac1b
             if (!s->new_idr && unit_type == 1) {
                 s->new_idr = 1;
                 s->idr_sps_seen = 0;
                 s->idr_pps_seen = 0;
ad91bf85
             }
bdfae2a5
         }
 
1cf4d2e9
 next_nal:
5d21ca45
         buf        += nal_size;
33d18982
         cumul_size += nal_size + s->length_size;
bdfae2a5
     } while (cumul_size < buf_size);
 
33d18982
     ret = av_packet_copy_props(out, in);
     if (ret < 0)
         goto fail;
52486603
 
 fail:
33d18982
     if (ret < 0)
         av_packet_unref(out);
     av_packet_free(&in);
 
7ae251b4
     return ret;
bdfae2a5
 }
 
33d18982
 static const enum AVCodecID codec_ids[] = {
     AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
 };
1defff85
 
33d18982
 const AVBitStreamFilter ff_h264_mp4toannexb_bsf = {
2490996f
     .name           = "h264_mp4toannexb",
     .priv_data_size = sizeof(H264BSFContext),
33d18982
     .init           = h264_mp4toannexb_init,
2490996f
     .filter         = h264_mp4toannexb_filter,
33d18982
     .codec_ids      = codec_ids,
bdfae2a5
 };