FFmpeg  4.0
h264dec.c
Go to the documentation of this file.
1 /*
2  * RAW H.264 video demuxer
3  * Copyright (c) 2008 Michael Niedermayer <michaelni@gmx.at>
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 "libavcodec/get_bits.h"
23 #include "libavcodec/golomb.h"
24 #include "avformat.h"
25 #include "rawdec.h"
26 #include "libavcodec/internal.h"
27 
28 #define MAX_SPS_COUNT 32
29 #define MAX_PPS_COUNT 256
30 
31 static int h264_probe(AVProbeData *p)
32 {
33  uint32_t code = -1;
34  int sps = 0, pps = 0, idr = 0, res = 0, sli = 0;
35  int i, ret;
36  int pps_ids[MAX_PPS_COUNT+1] = {0};
37  int sps_ids[MAX_SPS_COUNT+1] = {0};
38  unsigned pps_id, sps_id;
39  GetBitContext gb;
40 
41  for (i = 0; i + 2 < p->buf_size; i++) {
42  code = (code << 8) + p->buf[i];
43  if ((code & 0xffffff00) == 0x100) {
44  int ref_idc = (code >> 5) & 3;
45  int type = code & 0x1F;
46  static const int8_t ref_zero[] = {
47  2, 0, 0, 0, 0, -1, 1, -1,
48  -1, 1, 1, 1, 1, -1, 2, 2,
49  2, 2, 2, 0, 2, 2, 2, 2,
50  2, 2, 2, 2, 2, 2, 2, 2
51  };
52 
53  if (code & 0x80) // forbidden_bit
54  return 0;
55 
56  if (ref_zero[type] == 1 && ref_idc)
57  return 0;
58  if (ref_zero[type] == -1 && !ref_idc)
59  return 0;
60  if (ref_zero[type] == 2) {
61  if (!(code == 0x100 && !p->buf[i + 1] && !p->buf[i + 2]))
62  res++;
63  }
64 
65  ret = init_get_bits8(&gb, p->buf + i + 1, p->buf_size - i - 1);
66  if (ret < 0)
67  return 0;
68 
69  switch (type) {
70  case 1:
71  case 5:
72  get_ue_golomb_long(&gb);
73  if (get_ue_golomb_long(&gb) > 9U)
74  return 0;
75  pps_id = get_ue_golomb_long(&gb);
76  if (pps_id > MAX_PPS_COUNT)
77  return 0;
78  if (!pps_ids[pps_id])
79  break;
80 
81  if (type == 1)
82  sli++;
83  else
84  idr++;
85  break;
86  case 7:
87  skip_bits(&gb, 14);
88  if (get_bits(&gb, 2))
89  return 0;
90  skip_bits(&gb, 8);
91  sps_id = get_ue_golomb_long(&gb);
92  if (sps_id > MAX_SPS_COUNT)
93  return 0;
94  sps_ids[sps_id] = 1;
95  sps++;
96  break;
97  case 8:
98  pps_id = get_ue_golomb_long(&gb);
99  if (pps_id > MAX_PPS_COUNT)
100  return 0;
101  sps_id = get_ue_golomb_long(&gb);
102  if (sps_id > MAX_SPS_COUNT)
103  return 0;
104  if (!sps_ids[sps_id])
105  break;
106  pps_ids[pps_id] = 1;
107  pps++;
108  break;
109  }
110  }
111  }
112  ff_tlog(NULL, "sps:%d pps:%d idr:%d sli:%d res:%d\n", sps, pps, idr, sli, res);
113 
114  if (sps && pps && (idr || sli > 3) && res < (sps + pps + idr))
115  return AVPROBE_SCORE_EXTENSION + 1; // 1 more than .mpg
116 
117  return 0;
118 }
119 
120 FF_DEF_RAWVIDEO_DEMUXER(h264, "raw H.264 video", h264_probe, "h26l,h264,264,avc", AV_CODEC_ID_H264)
#define ff_tlog(ctx,...)
Definition: internal.h:75
#define NULL
Definition: coverity.c:32
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:269
#define FF_DEF_RAWVIDEO_DEMUXER(shortname, longname, probe, ext, id)
Definition: rawdec.h:69
static void idr(H264Context *h)
instantaneous decoder refresh.
Definition: h264dec.c:470
bitstream reader API header.
static int h264_probe(AVProbeData *p)
Definition: h264dec.c:31
#define U(x)
Definition: vp56_arith.h:37
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:451
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
#define MAX_SPS_COUNT
Definition: h264dec.c:28
if(ret< 0)
Definition: vf_mcdeint.c:279
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:85
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:464
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:314
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
cl_device_type type
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:458
#define MAX_PPS_COUNT
Definition: h264dec.c:29
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
Main libavformat public API header.
common internal api header.
exp golomb vlc stuff