avc.c
Go to the documentation of this file.
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 "avformat.h"
24 #include "avio.h"
25 #include "avc.h"
26 
27 static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
28 {
29  const uint8_t *a = p + 4 - ((intptr_t)p & 3);
30 
31  for (end -= 3; p < a && p < end; p++) {
32  if (p[0] == 0 && p[1] == 0 && p[2] == 1)
33  return p;
34  }
35 
36  for (end -= 3; p < end; p += 4) {
37  uint32_t x = *(const uint32_t*)p;
38 // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
39 // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
40  if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
41  if (p[1] == 0) {
42  if (p[0] == 0 && p[2] == 1)
43  return p;
44  if (p[2] == 0 && p[3] == 1)
45  return p+1;
46  }
47  if (p[3] == 0) {
48  if (p[2] == 0 && p[4] == 1)
49  return p+2;
50  if (p[4] == 0 && p[5] == 1)
51  return p+3;
52  }
53  }
54  }
55 
56  for (end += 3; p < end; p++) {
57  if (p[0] == 0 && p[1] == 0 && p[2] == 1)
58  return p;
59  }
60 
61  return end + 3;
62 }
63 
64 const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){
66  if(p<out && out<end && !out[-1]) out--;
67  return out;
68 }
69 
70 int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
71 {
72  const uint8_t *p = buf_in;
73  const uint8_t *end = p + size;
74  const uint8_t *nal_start, *nal_end;
75 
76  size = 0;
77  nal_start = ff_avc_find_startcode(p, end);
78  for (;;) {
79  while (nal_start < end && !*(nal_start++));
80  if (nal_start == end)
81  break;
82 
83  nal_end = ff_avc_find_startcode(nal_start, end);
84  avio_wb32(pb, nal_end - nal_start);
85  avio_write(pb, nal_start, nal_end - nal_start);
86  size += 4 + nal_end - nal_start;
87  nal_start = nal_end;
88  }
89  return size;
90 }
91 
92 int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
93 {
94  AVIOContext *pb;
95  int ret = avio_open_dyn_buf(&pb);
96  if(ret < 0)
97  return ret;
98 
99  ff_avc_parse_nal_units(pb, buf_in, *size);
100 
101  av_freep(buf);
102  *size = avio_close_dyn_buf(pb, buf);
103  return 0;
104 }
105 
107 {
108  if (len > 6) {
109  /* check for h264 start code */
110  if (AV_RB32(data) == 0x00000001 ||
111  AV_RB24(data) == 0x000001) {
112  uint8_t *buf=NULL, *end, *start;
113  uint32_t sps_size=0, pps_size=0;
114  uint8_t *sps=0, *pps=0;
115 
116  int ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
117  if (ret < 0)
118  return ret;
119  start = buf;
120  end = buf + len;
121 
122  /* look for sps and pps */
123  while (end - buf > 4) {
124  uint32_t size;
125  uint8_t nal_type;
126  size = FFMIN(AV_RB32(buf), end - buf - 4);
127  buf += 4;
128  nal_type = buf[0] & 0x1f;
129 
130  if (nal_type == 7) { /* SPS */
131  sps = buf;
132  sps_size = size;
133  } else if (nal_type == 8) { /* PPS */
134  pps = buf;
135  pps_size = size;
136  }
137 
138  buf += size;
139  }
140 
141  if (!sps || !pps || sps_size < 4 || sps_size > UINT16_MAX || pps_size > UINT16_MAX)
142  return AVERROR_INVALIDDATA;
143 
144  avio_w8(pb, 1); /* version */
145  avio_w8(pb, sps[1]); /* profile */
146  avio_w8(pb, sps[2]); /* profile compat */
147  avio_w8(pb, sps[3]); /* level */
148  avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
149  avio_w8(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
150 
151  avio_wb16(pb, sps_size);
152  avio_write(pb, sps, sps_size);
153  avio_w8(pb, 1); /* number of pps */
154  avio_wb16(pb, pps_size);
155  avio_write(pb, pps, pps_size);
156  av_free(start);
157  } else {
158  avio_write(pb, data, len);
159  }
160  }
161  return 0;
162 }
163 
165 {
166  uint16_t sps_size, pps_size;
167  uint8_t *out;
168  int out_size;
169 
170  *buf = NULL;
171  if (*size >= 4 && (AV_RB32(in) == 0x00000001 || AV_RB24(in) == 0x000001))
172  return 0;
173  if (*size < 11 || in[0] != 1)
174  return AVERROR_INVALIDDATA;
175 
176  sps_size = AV_RB16(&in[6]);
177  if (11 + sps_size > *size)
178  return AVERROR_INVALIDDATA;
179  pps_size = AV_RB16(&in[9 + sps_size]);
180  if (11 + sps_size + pps_size > *size)
181  return AVERROR_INVALIDDATA;
182  out_size = 8 + sps_size + pps_size;
183  out = av_mallocz(out_size);
184  if (!out)
185  return AVERROR(ENOMEM);
186  AV_WB32(&out[0], 0x00000001);
187  memcpy(out + 4, &in[8], sps_size);
188  AV_WB32(&out[4 + sps_size], 0x00000001);
189  memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size);
190  *buf = out;
191  *size = out_size;
192  return 0;
193 }
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Buffered I/O operations.
const uint8_t * ff_avc_find_startcode(const uint8_t *p, const uint8_t *end)
Definition: avc.c:64
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:988
#define AV_RB24
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:976
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
#define AV_WB32(p, darg)
Definition: intreadwrite.h:265
uint8_t
#define AV_RB32
end end
int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
Definition: avc.c:164
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
Discrete Time axis x
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
#define AV_RB16
Spectrum Plot time data
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: avc.c:92
int size
int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
Definition: avc.c:70
#define FFMIN(a, b)
Definition: common.h:58
static const uint8_t * ff_avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
Definition: avc.c:27
ret
Definition: avfilter.c:821
NULL
Definition: eval.c:55
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:151
void * buf
Definition: avisynth_c.h:594
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:373
Main libavformat public API header.
int len
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:299
void INT64 start
Definition: avisynth_c.h:594
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:106