FFmpeg
avsscanf.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005-2014 Rich Felker, et al.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <stdarg.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <float.h>
29 
30 #include "config.h"
31 #include "common.h"
32 #include "avstring.h"
33 #include "bprint.h"
34 
35 typedef struct FFFILE {
36  size_t buf_size;
37  unsigned char *buf;
38  unsigned char *rpos, *rend;
39  unsigned char *shend;
40  ptrdiff_t shlim, shcnt;
41  void *cookie;
42  size_t (*read)(struct FFFILE *, unsigned char *, size_t);
43 } FFFILE;
44 
45 #define SIZE_hh -2
46 #define SIZE_h -1
47 #define SIZE_def 0
48 #define SIZE_l 1
49 #define SIZE_L 2
50 #define SIZE_ll 3
51 
52 #define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf))
53 
54 static int fftoread(FFFILE *f)
55 {
56  f->rpos = f->rend = f->buf + f->buf_size;
57  return 0;
58 }
59 
60 static size_t ffstring_read(FFFILE *f, unsigned char *buf, size_t len)
61 {
62  char *src = f->cookie;
63  size_t k = len+256;
64  char *end = memchr(src, 0, k);
65 
66  if (end) k = end-src;
67  if (k < len) len = k;
68  memcpy(buf, src, len);
69  f->rpos = (void *)(src+len);
70  f->rend = (void *)(src+k);
71  f->cookie = src+k;
72 
73  return len;
74 }
75 
76 static int ffuflow(FFFILE *f)
77 {
78  unsigned char c;
79  if (!fftoread(f) && f->read(f, &c, 1)==1) return c;
80  return EOF;
81 }
82 
83 static void ffshlim(FFFILE *f, ptrdiff_t lim)
84 {
85  f->shlim = lim;
86  f->shcnt = f->buf - f->rpos;
87  /* If lim is nonzero, rend must be a valid pointer. */
88  if (lim && f->rend - f->rpos > lim)
89  f->shend = f->rpos + lim;
90  else
91  f->shend = f->rend;
92 }
93 
94 static int ffshgetc(FFFILE *f)
95 {
96  int c;
97  ptrdiff_t cnt = shcnt(f);
98  if (f->shlim && cnt >= f->shlim || (c=ffuflow(f)) < 0) {
99  f->shcnt = f->buf - f->rpos + cnt;
100  f->shend = 0;
101  return EOF;
102  }
103  cnt++;
104  if (f->shlim && f->rend - f->rpos > f->shlim - cnt)
105  f->shend = f->rpos + (f->shlim - cnt);
106  else
107  f->shend = f->rend;
108  f->shcnt = f->buf - f->rpos + cnt;
109  if (f->rpos[-1] != c) f->rpos[-1] = c;
110  return c;
111 }
112 
113 #define shlim(f, lim) ffshlim((f), (lim))
114 #define shgetc(f) (((f)->rpos < (f)->shend) ? *(f)->rpos++ : ffshgetc(f))
115 #define shunget(f) ((f)->shend ? (void)(f)->rpos-- : (void)0)
116 
117 static const unsigned char table[] = { -1,
118  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
119  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
120  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
121  0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
122  -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
123  25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
124  -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
125  25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
126  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
127  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
128  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
129  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
130  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
131  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
132  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
133  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
134 };
135 
136 static unsigned long long ffintscan(FFFILE *f, unsigned base, int pok, unsigned long long lim)
137 {
138  const unsigned char *val = table+1;
139  int c, neg=0;
140  unsigned x;
141  unsigned long long y;
142  if (base > 36 || base == 1) {
143  errno = EINVAL;
144  return 0;
145  }
146  while (av_isspace((c=shgetc(f))));
147  if (c=='+' || c=='-') {
148  neg = -(c=='-');
149  c = shgetc(f);
150  }
151  if ((base == 0 || base == 16) && c=='0') {
152  c = shgetc(f);
153  if ((c|32)=='x') {
154  c = shgetc(f);
155  if (val[c]>=16) {
156  shunget(f);
157  if (pok) shunget(f);
158  else shlim(f, 0);
159  return 0;
160  }
161  base = 16;
162  } else if (base == 0) {
163  base = 8;
164  }
165  } else {
166  if (base == 0) base = 10;
167  if (val[c] >= base) {
168  shunget(f);
169  shlim(f, 0);
170  errno = EINVAL;
171  return 0;
172  }
173  }
174  if (base == 10) {
175  for (x=0; c-'0'<10U && x<=UINT_MAX/10-1; c=shgetc(f))
176  x = x*10 + (c-'0');
177  for (y=x; c-'0'<10U && y<=ULLONG_MAX/10 && 10*y<=ULLONG_MAX-(c-'0'); c=shgetc(f))
178  y = y*10 + (c-'0');
179  if (c-'0'>=10U) goto done;
180  } else if (!(base & base-1)) {
181  int bs = "\0\1\2\4\7\3\6\5"[(0x17*base)>>5&7];
182  for (x=0; val[c]<base && x<=UINT_MAX/32; c=shgetc(f))
183  x = x<<bs | val[c];
184  for (y=x; val[c]<base && y<=ULLONG_MAX>>bs; c=shgetc(f))
185  y = y<<bs | val[c];
186  } else {
187  for (x=0; val[c]<base && x<=UINT_MAX/36-1; c=shgetc(f))
188  x = x*base + val[c];
189  for (y=x; val[c]<base && y<=ULLONG_MAX/base && base*y<=ULLONG_MAX-val[c]; c=shgetc(f))
190  y = y*base + val[c];
191  }
192  if (val[c]<base) {
193  for (; val[c]<base; c=shgetc(f));
194  errno = ERANGE;
195  y = lim;
196  if (lim&1) neg = 0;
197  }
198 done:
199  shunget(f);
200  if (y>=lim) {
201  if (!(lim&1) && !neg) {
202  errno = ERANGE;
203  return lim-1;
204  } else if (y>lim) {
205  errno = ERANGE;
206  return lim;
207  }
208  }
209  return (y^neg)-neg;
210 }
211 
212 static long long scanexp(FFFILE *f, int pok)
213 {
214  int c;
215  int x;
216  long long y;
217  int neg = 0;
218 
219  c = shgetc(f);
220  if (c=='+' || c=='-') {
221  neg = (c=='-');
222  c = shgetc(f);
223  if (c-'0'>=10U && pok) shunget(f);
224  }
225  if (c-'0'>=10U) {
226  shunget(f);
227  return LLONG_MIN;
228  }
229  for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f))
230  x = 10*x + (c-'0');
231  for (y=x; c-'0'<10U && y<LLONG_MAX/100; c = shgetc(f))
232  y = 10*y + (c-'0');
233  for (; c-'0'<10U; c = shgetc(f));
234  shunget(f);
235  return neg ? -y : y;
236 }
237 
238 #define LD_B1B_DIG 2
239 #define LD_B1B_MAX 9007199, 254740991
240 #define KMAX 128
241 #define MASK (KMAX-1)
242 
243 static double decfloat(FFFILE *f, int c, int bits, int emin, int sign, int pok)
244 {
245  uint32_t x[KMAX];
246  static const uint32_t th[] = { LD_B1B_MAX };
247  int i, j, k, a, z;
248  long long lrp=0, dc=0;
249  long long e10=0;
250  int lnz = 0;
251  int gotdig = 0, gotrad = 0;
252  int rp;
253  int e2;
254  int emax = -emin-bits+3;
255  int denormal = 0;
256  double y;
257  double frac=0;
258  double bias=0;
259  static const int p10s[] = { 10, 100, 1000, 10000,
260  100000, 1000000, 10000000, 100000000 };
261 
262  j=0;
263  k=0;
264 
265  /* Don't let leading zeros consume buffer space */
266  for (; c=='0'; c = shgetc(f)) gotdig=1;
267  if (c=='.') {
268  gotrad = 1;
269  for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--;
270  }
271 
272  x[0] = 0;
273  for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
274  if (c == '.') {
275  if (gotrad) break;
276  gotrad = 1;
277  lrp = dc;
278  } else if (k < KMAX-3) {
279  dc++;
280  if (c!='0') lnz = dc;
281  if (j) x[k] = x[k]*10 + c-'0';
282  else x[k] = c-'0';
283  if (++j==9) {
284  k++;
285  j=0;
286  }
287  gotdig=1;
288  } else {
289  dc++;
290  if (c!='0') {
291  lnz = (KMAX-4)*9;
292  x[KMAX-4] |= 1;
293  }
294  }
295  }
296  if (!gotrad) lrp=dc;
297 
298  if (gotdig && (c|32)=='e') {
299  e10 = scanexp(f, pok);
300  if (e10 == LLONG_MIN) {
301  if (pok) {
302  shunget(f);
303  } else {
304  shlim(f, 0);
305  return 0;
306  }
307  e10 = 0;
308  }
309  lrp += e10;
310  } else if (c>=0) {
311  shunget(f);
312  }
313  if (!gotdig) {
314  errno = EINVAL;
315  shlim(f, 0);
316  return 0;
317  }
318 
319  /* Handle zero specially to avoid nasty special cases later */
320  if (!x[0]) return sign * 0.0;
321 
322  /* Optimize small integers (w/no exponent) and over/under-flow */
323  if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
324  return sign * (double)x[0];
325  if (lrp > -emin/2) {
326  errno = ERANGE;
327  return sign * DBL_MAX * DBL_MAX;
328  }
329  if (lrp < emin-2*DBL_MANT_DIG) {
330  errno = ERANGE;
331  return sign * DBL_MIN * DBL_MIN;
332  }
333 
334  /* Align incomplete final B1B digit */
335  if (j) {
336  for (; j<9; j++) x[k]*=10;
337  k++;
338  j=0;
339  }
340 
341  a = 0;
342  z = k;
343  e2 = 0;
344  rp = lrp;
345 
346  /* Optimize small to mid-size integers (even in exp. notation) */
347  if (lnz<9 && lnz<=rp && rp < 18) {
348  int bitlim;
349  if (rp == 9) return sign * (double)x[0];
350  if (rp < 9) return sign * (double)x[0] / p10s[8-rp];
351  bitlim = bits-3*(int)(rp-9);
352  if (bitlim>30 || x[0]>>bitlim==0)
353  return sign * (double)x[0] * p10s[rp-10];
354  }
355 
356  /* Drop trailing zeros */
357  for (; !x[z-1]; z--);
358 
359  /* Align radix point to B1B digit boundary */
360  if (rp % 9) {
361  int rpm9 = rp>=0 ? rp%9 : rp%9+9;
362  int p10 = p10s[8-rpm9];
363  uint32_t carry = 0;
364  for (k=a; k!=z; k++) {
365  uint32_t tmp = x[k] % p10;
366  x[k] = x[k]/p10 + carry;
367  carry = 1000000000/p10 * tmp;
368  if (k==a && !x[k]) {
369  a = (a+1 & MASK);
370  rp -= 9;
371  }
372  }
373  if (carry) x[z++] = carry;
374  rp += 9-rpm9;
375  }
376 
377  /* Upscale until desired number of bits are left of radix point */
378  while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
379  uint32_t carry = 0;
380  e2 -= 29;
381  for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
382  uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
383  if (tmp > 1000000000) {
384  carry = tmp / 1000000000;
385  x[k] = tmp % 1000000000;
386  } else {
387  carry = 0;
388  x[k] = tmp;
389  }
390  if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
391  if (k==a) break;
392  }
393  if (carry) {
394  rp += 9;
395  a = (a-1 & MASK);
396  if (a == z) {
397  z = (z-1 & MASK);
398  x[z-1 & MASK] |= x[z];
399  }
400  x[a] = carry;
401  }
402  }
403 
404  /* Downscale until exactly number of bits are left of radix point */
405  for (;;) {
406  uint32_t carry = 0;
407  int sh = 1;
408  for (i=0; i<LD_B1B_DIG; i++) {
409  k = (a+i & MASK);
410  if (k == z || x[k] < th[i]) {
411  i=LD_B1B_DIG;
412  break;
413  }
414  if (x[a+i & MASK] > th[i]) break;
415  }
416  if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
417  /* FIXME: find a way to compute optimal sh */
418  if (rp > 9+9*LD_B1B_DIG) sh = 9;
419  e2 += sh;
420  for (k=a; k!=z; k=(k+1 & MASK)) {
421  uint32_t tmp = x[k] & (1<<sh)-1;
422  x[k] = (x[k]>>sh) + carry;
423  carry = (1000000000>>sh) * tmp;
424  if (k==a && !x[k]) {
425  a = (a+1 & MASK);
426  i--;
427  rp -= 9;
428  }
429  }
430  if (carry) {
431  if ((z+1 & MASK) != a) {
432  x[z] = carry;
433  z = (z+1 & MASK);
434  } else x[z-1 & MASK] |= 1;
435  }
436  }
437 
438  /* Assemble desired bits into floating point variable */
439  for (y=i=0; i<LD_B1B_DIG; i++) {
440  if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
441  y = 1000000000.0L * y + x[a+i & MASK];
442  }
443 
444  y *= sign;
445 
446  /* Limit precision for denormal results */
447  if (bits > DBL_MANT_DIG+e2-emin) {
448  bits = DBL_MANT_DIG+e2-emin;
449  if (bits<0) bits=0;
450  denormal = 1;
451  }
452 
453  /* Calculate bias term to force rounding, move out lower bits */
454  if (bits < DBL_MANT_DIG) {
455  bias = copysign(scalbn(1, 2*DBL_MANT_DIG-bits-1), y);
456  frac = fmod(y, scalbn(1, DBL_MANT_DIG-bits));
457  y -= frac;
458  y += bias;
459  }
460 
461  /* Process tail of decimal input so it can affect rounding */
462  if ((a+i & MASK) != z) {
463  uint32_t t = x[a+i & MASK];
464  if (t < 500000000 && (t || (a+i+1 & MASK) != z))
465  frac += 0.25*sign;
466  else if (t > 500000000)
467  frac += 0.75*sign;
468  else if (t == 500000000) {
469  if ((a+i+1 & MASK) == z)
470  frac += 0.5*sign;
471  else
472  frac += 0.75*sign;
473  }
474  if (DBL_MANT_DIG-bits >= 2 && !fmod(frac, 1))
475  frac++;
476  }
477 
478  y += frac;
479  y -= bias;
480 
481  if ((e2+DBL_MANT_DIG & INT_MAX) > emax-5) {
482  if (fabs(y) >= pow(2, DBL_MANT_DIG)) {
483  if (denormal && bits==DBL_MANT_DIG+e2-emin)
484  denormal = 0;
485  y *= 0.5;
486  e2++;
487  }
488  if (e2+DBL_MANT_DIG>emax || (denormal && frac))
489  errno = ERANGE;
490  }
491 
492  return scalbn(y, e2);
493 }
494 
495 static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok)
496 {
497  uint32_t x = 0;
498  double y = 0;
499  double scale = 1;
500  double bias = 0;
501  int gottail = 0, gotrad = 0, gotdig = 0;
502  long long rp = 0;
503  long long dc = 0;
504  long long e2 = 0;
505  int d;
506  int c;
507 
508  c = shgetc(f);
509 
510  /* Skip leading zeros */
511  for (; c=='0'; c = shgetc(f))
512  gotdig = 1;
513 
514  if (c=='.') {
515  gotrad = 1;
516  c = shgetc(f);
517  /* Count zeros after the radix point before significand */
518  for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
519  }
520 
521  for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
522  if (c=='.') {
523  if (gotrad) break;
524  rp = dc;
525  gotrad = 1;
526  } else {
527  gotdig = 1;
528  if (c > '9') d = (c|32)+10-'a';
529  else d = c-'0';
530  if (dc<8) {
531  x = x*16 + d;
532  } else if (dc < DBL_MANT_DIG/4+1) {
533  y += d*(scale/=16);
534  } else if (d && !gottail) {
535  y += 0.5*scale;
536  gottail = 1;
537  }
538  dc++;
539  }
540  }
541  if (!gotdig) {
542  shunget(f);
543  if (pok) {
544  shunget(f);
545  if (gotrad) shunget(f);
546  } else {
547  shlim(f, 0);
548  }
549  return sign * 0.0;
550  }
551  if (!gotrad) rp = dc;
552  while (dc<8) x *= 16, dc++;
553  if ((c|32)=='p') {
554  e2 = scanexp(f, pok);
555  if (e2 == LLONG_MIN) {
556  if (pok) {
557  shunget(f);
558  } else {
559  shlim(f, 0);
560  return 0;
561  }
562  e2 = 0;
563  }
564  } else {
565  shunget(f);
566  }
567  e2 += 4*rp - 32;
568 
569  if (!x) return sign * 0.0;
570  if (e2 > -emin) {
571  errno = ERANGE;
572  return sign * DBL_MAX * DBL_MAX;
573  }
574  if (e2 < emin-2*DBL_MANT_DIG) {
575  errno = ERANGE;
576  return sign * DBL_MIN * DBL_MIN;
577  }
578 
579  while (x < 0x80000000) {
580  if (y>=0.5) {
581  x += x + 1;
582  y += y - 1;
583  } else {
584  x += x;
585  y += y;
586  }
587  e2--;
588  }
589 
590  if (bits > 32+e2-emin) {
591  bits = 32+e2-emin;
592  if (bits<0) bits=0;
593  }
594 
595  if (bits < DBL_MANT_DIG)
596  bias = copysign(scalbn(1, 32+DBL_MANT_DIG-bits-1), sign);
597 
598  if (bits<32 && y && !(x&1)) x++, y=0;
599 
600  y = bias + sign*(double)x + sign*y;
601  y -= bias;
602 
603  if (!y) errno = ERANGE;
604 
605  return scalbn(y, e2);
606 }
607 
608 static double fffloatscan(FFFILE *f, int prec, int pok)
609 {
610  int sign = 1;
611  size_t i;
612  int bits;
613  int emin;
614  int c;
615 
616  switch (prec) {
617  case 0:
618  bits = FLT_MANT_DIG;
619  emin = FLT_MIN_EXP-bits;
620  break;
621  case 1:
622  bits = DBL_MANT_DIG;
623  emin = DBL_MIN_EXP-bits;
624  break;
625  case 2:
626  bits = DBL_MANT_DIG;
627  emin = DBL_MIN_EXP-bits;
628  break;
629  default:
630  return 0;
631  }
632 
633  while (av_isspace((c = shgetc(f))));
634 
635  if (c=='+' || c=='-') {
636  sign -= 2*(c=='-');
637  c = shgetc(f);
638  }
639 
640  for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
641  if (i<7) c = shgetc(f);
642  if (i==3 || i==8 || (i>3 && pok)) {
643  if (i!=8) {
644  shunget(f);
645  if (pok) for (; i>3; i--) shunget(f);
646  }
647  return sign * INFINITY;
648  }
649  if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
650  if (i<2) c = shgetc(f);
651  if (i==3) {
652  if (shgetc(f) != '(') {
653  shunget(f);
654  return NAN;
655  }
656  for (i=1; ; i++) {
657  c = shgetc(f);
658  if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_')
659  continue;
660  if (c==')') return NAN;
661  shunget(f);
662  if (!pok) {
663  errno = EINVAL;
664  shlim(f, 0);
665  return 0;
666  }
667  while (i--) shunget(f);
668  return NAN;
669  }
670  return NAN;
671  }
672 
673  if (i) {
674  shunget(f);
675  errno = EINVAL;
676  shlim(f, 0);
677  return 0;
678  }
679 
680  if (c=='0') {
681  c = shgetc(f);
682  if ((c|32) == 'x')
683  return hexfloat(f, bits, emin, sign, pok);
684  shunget(f);
685  c = '0';
686  }
687 
688  return decfloat(f, c, bits, emin, sign, pok);
689 }
690 
691 static void *arg_n(va_list ap, unsigned int n)
692 {
693  void *p;
694  unsigned int i;
695  va_list ap2;
696  va_copy(ap2, ap);
697  for (i=n; i>1; i--) va_arg(ap2, void *);
698  p = va_arg(ap2, void *);
699  va_end(ap2);
700  return p;
701 }
702 
703 static void store_int(void *dest, int size, unsigned long long i)
704 {
705  if (!dest) return;
706  switch (size) {
707  case SIZE_hh:
708  *(char *)dest = i;
709  break;
710  case SIZE_h:
711  *(short *)dest = i;
712  break;
713  case SIZE_def:
714  *(int *)dest = i;
715  break;
716  case SIZE_l:
717  *(long *)dest = i;
718  break;
719  case SIZE_ll:
720  *(long long *)dest = i;
721  break;
722  }
723 }
724 
725 static int ff_vfscanf(FFFILE *f, const char *fmt, va_list ap)
726 {
727  int width;
728  int size;
729  int base;
730  const unsigned char *p;
731  int c, t;
732  char *s;
733  void *dest=NULL;
734  int invert;
735  int matches=0;
736  unsigned long long x;
737  double y;
738  ptrdiff_t pos = 0;
739  unsigned char scanset[257];
740  size_t i;
741 
742  for (p=(const unsigned char *)fmt; *p; p++) {
743 
744  if (av_isspace(*p)) {
745  while (av_isspace(p[1])) p++;
746  shlim(f, 0);
747  while (av_isspace(shgetc(f)));
748  shunget(f);
749  pos += shcnt(f);
750  continue;
751  }
752  if (*p != '%' || p[1] == '%') {
753  shlim(f, 0);
754  if (*p == '%') {
755  p++;
756  while (av_isspace((c=shgetc(f))));
757  } else {
758  c = shgetc(f);
759  }
760  if (c!=*p) {
761  shunget(f);
762  if (c<0) goto input_fail;
763  goto match_fail;
764  }
765  pos += shcnt(f);
766  continue;
767  }
768 
769  p++;
770  if (*p=='*') {
771  dest = 0; p++;
772  } else if (av_isdigit(*p) && p[1]=='$') {
773  dest = arg_n(ap, *p-'0'); p+=2;
774  } else {
775  dest = va_arg(ap, void *);
776  }
777 
778  for (width=0; av_isdigit(*p); p++) {
779  width = 10*width + *p - '0';
780  }
781 
782  if (*p=='m') {
783  s = 0;
784  p++;
785  }
786 
787  size = SIZE_def;
788  switch (*p++) {
789  case 'h':
790  if (*p == 'h') p++, size = SIZE_hh;
791  else size = SIZE_h;
792  break;
793  case 'l':
794  if (*p == 'l') p++, size = SIZE_ll;
795  else size = SIZE_l;
796  break;
797  case 'j':
798  size = SIZE_ll;
799  break;
800  case 'z':
801  case 't':
802  size = SIZE_l;
803  break;
804  case 'L':
805  size = SIZE_L;
806  break;
807  case 'd': case 'i': case 'o': case 'u': case 'x':
808  case 'a': case 'e': case 'f': case 'g':
809  case 'A': case 'E': case 'F': case 'G': case 'X':
810  case 's': case 'c': case '[':
811  case 'S': case 'C':
812  case 'p': case 'n':
813  p--;
814  break;
815  default:
816  goto fmt_fail;
817  }
818 
819  t = *p;
820 
821  /* C or S */
822  if ((t&0x2f) == 3) {
823  t |= 32;
824  size = SIZE_l;
825  }
826 
827  switch (t) {
828  case 'c':
829  if (width < 1) width = 1;
830  case '[':
831  break;
832  case 'n':
833  store_int(dest, size, pos);
834  /* do not increment match count, etc! */
835  continue;
836  default:
837  shlim(f, 0);
838  while (av_isspace(shgetc(f)));
839  shunget(f);
840  pos += shcnt(f);
841  }
842 
843  shlim(f, width);
844  if (shgetc(f) < 0) goto input_fail;
845  shunget(f);
846 
847  switch (t) {
848  case 's':
849  case 'c':
850  case '[':
851  if (t == 'c' || t == 's') {
852  memset(scanset, -1, sizeof scanset);
853  scanset[0] = 0;
854  if (t == 's') {
855  scanset[1 + '\t'] = 0;
856  scanset[1 + '\n'] = 0;
857  scanset[1 + '\v'] = 0;
858  scanset[1 + '\f'] = 0;
859  scanset[1 + '\r'] = 0;
860  scanset[1 + ' ' ] = 0;
861  }
862  } else {
863  if (*++p == '^') p++, invert = 1;
864  else invert = 0;
865  memset(scanset, invert, sizeof scanset);
866  scanset[0] = 0;
867  if (*p == '-') p++, scanset[1+'-'] = 1-invert;
868  else if (*p == ']') p++, scanset[1+']'] = 1-invert;
869  for (; *p != ']'; p++) {
870  if (!*p) goto fmt_fail;
871  if (*p=='-' && p[1] && p[1] != ']')
872  for (c=p++[-1]; c<*p; c++)
873  scanset[1+c] = 1-invert;
874  scanset[1+*p] = 1-invert;
875  }
876  }
877  s = 0;
878  i = 0;
879  if ((s = dest)) {
880  while (scanset[(c=shgetc(f))+1])
881  s[i++] = c;
882  } else {
883  while (scanset[(c=shgetc(f))+1]);
884  }
885  shunget(f);
886  if (!shcnt(f)) goto match_fail;
887  if (t == 'c' && shcnt(f) != width) goto match_fail;
888  if (t != 'c') {
889  if (s) s[i] = 0;
890  }
891  break;
892  case 'p':
893  case 'X':
894  case 'x':
895  base = 16;
896  goto int_common;
897  case 'o':
898  base = 8;
899  goto int_common;
900  case 'd':
901  case 'u':
902  base = 10;
903  goto int_common;
904  case 'i':
905  base = 0;
906 int_common:
907  x = ffintscan(f, base, 0, ULLONG_MAX);
908  if (!shcnt(f))
909  goto match_fail;
910  if (t=='p' && dest)
911  *(void **)dest = (void *)(uintptr_t)x;
912  else
913  store_int(dest, size, x);
914  break;
915  case 'a': case 'A':
916  case 'e': case 'E':
917  case 'f': case 'F':
918  case 'g': case 'G':
919  y = fffloatscan(f, size, 0);
920  if (!shcnt(f))
921  goto match_fail;
922  if (dest) {
923  switch (size) {
924  case SIZE_def:
925  *(float *)dest = y;
926  break;
927  case SIZE_l:
928  *(double *)dest = y;
929  break;
930  case SIZE_L:
931  *(double *)dest = y;
932  break;
933  }
934  }
935  break;
936  }
937 
938  pos += shcnt(f);
939  if (dest) matches++;
940  }
941  if (0) {
942 fmt_fail:
943 input_fail:
944  if (!matches) matches--;
945  }
946 match_fail:
947  return matches;
948 }
949 
950 static int ff_vsscanf(const char *s, const char *fmt, va_list ap)
951 {
952  FFFILE f = {
953  .buf = (void *)s, .cookie = (void *)s,
954  .read = ffstring_read,
955  };
956 
957  return ff_vfscanf(&f, fmt, ap);
958 }
959 
960 int av_sscanf(const char *string, const char *format, ...)
961 {
962  int ret;
963  va_list ap;
964  va_start(ap, format);
965  ret = ff_vsscanf(string, format, ap);
966  va_end(ap);
967  return ret;
968 }
shlim
#define shlim(f, lim)
Definition: avsscanf.c:113
INFINITY
#define INFINITY
Definition: mathematics.h:67
store_int
static void store_int(void *dest, int size, unsigned long long i)
Definition: avsscanf.c:703
table
static const unsigned char table[]
Definition: avsscanf.c:117
FFFILE::buf_size
size_t buf_size
Definition: avsscanf.c:36
FFFILE::shcnt
ptrdiff_t shcnt
Definition: avsscanf.c:40
av_isspace
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:227
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
FFFILE::read
size_t(* read)(struct FFFILE *, unsigned char *, size_t)
Definition: avsscanf.c:42
decfloat
static double decfloat(FFFILE *f, int c, int bits, int emin, int sign, int pok)
Definition: avsscanf.c:243
base
uint8_t base
Definition: vp3data.h:141
float.h
LD_B1B_DIG
#define LD_B1B_DIG
Definition: avsscanf.c:238
FFFILE::shlim
ptrdiff_t shlim
Definition: avsscanf.c:40
SIZE_L
#define SIZE_L
Definition: avsscanf.c:49
MASK
#define MASK
Definition: avsscanf.c:241
U
#define U(x)
Definition: vp56_arith.h:37
scanexp
static long long scanexp(FFFILE *f, int pok)
Definition: avsscanf.c:212
val
static double val(void *priv, double ch)
Definition: aeval.c:76
fffloatscan
static double fffloatscan(FFFILE *f, int prec, int pok)
Definition: avsscanf.c:608
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1388
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
FFFILE::rpos
unsigned char * rpos
Definition: avsscanf.c:38
bits
uint8_t bits
Definition: vp3data.h:141
copysign
static av_always_inline double copysign(double x, double y)
Definition: libm.h:68
SIZE_hh
#define SIZE_hh
Definition: avsscanf.c:45
NAN
#define NAN
Definition: mathematics.h:64
f
#define f(width, name)
Definition: cbs_vp9.c:255
ffintscan
static unsigned long long ffintscan(FFFILE *f, unsigned base, int pok, unsigned long long lim)
Definition: avsscanf.c:136
av_sscanf
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:960
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
FFFILE::shend
unsigned char * shend
Definition: avsscanf.c:39
NULL
#define NULL
Definition: coverity.c:32
src
#define src
Definition: vp8dsp.c:255
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
FFFILE::buf
unsigned char * buf
Definition: avsscanf.c:37
ffuflow
static int ffuflow(FFFILE *f)
Definition: avsscanf.c:76
dc
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff) *mv_scale Intra DC Prediction block[y][x] dc[1]
Definition: snow.txt:400
SIZE_l
#define SIZE_l
Definition: avsscanf.c:48
hexfloat
static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok)
Definition: avsscanf.c:495
size
int size
Definition: twinvq_data.h:10344
format
ofilter format
Definition: ffmpeg_filter.c:172
shunget
#define shunget(f)
Definition: avsscanf.c:115
av_isdigit
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.h:211
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
va_copy
#define va_copy(dst, src)
Definition: va_copy.h:31
ff_vsscanf
static int ff_vsscanf(const char *s, const char *fmt, va_list ap)
Definition: avsscanf.c:950
th
#define th
Definition: regdef.h:75
bprint.h
KMAX
#define KMAX
Definition: avsscanf.c:240
arg_n
static void * arg_n(va_list ap, unsigned int n)
Definition: avsscanf.c:691
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
invert
static void invert(float *h, int n)
Definition: asrc_sinc.c:195
common.h
len
int len
Definition: vorbis_enc_data.h:426
SIZE_def
#define SIZE_def
Definition: avsscanf.c:47
ret
ret
Definition: filter_design.txt:187
pos
unsigned int pos
Definition: spdifenc.c:412
SIZE_h
#define SIZE_h
Definition: avsscanf.c:46
shcnt
#define shcnt(f)
Definition: avsscanf.c:52
LD_B1B_MAX
#define LD_B1B_MAX
Definition: avsscanf.c:239
ffstring_read
static size_t ffstring_read(FFFILE *f, unsigned char *buf, size_t len)
Definition: avsscanf.c:60
d
d
Definition: ffmpeg_filter.c:153
ff_vfscanf
static int ff_vfscanf(FFFILE *f, const char *fmt, va_list ap)
Definition: avsscanf.c:725
shgetc
#define shgetc(f)
Definition: avsscanf.c:114
FFFILE::rend
unsigned char * rend
Definition: avsscanf.c:38
FFFILE
Definition: avsscanf.c:35
avstring.h
int
int
Definition: ffmpeg_filter.c:153
FFFILE::cookie
void * cookie
Definition: avsscanf.c:41
ffshgetc
static int ffshgetc(FFFILE *f)
Definition: avsscanf.c:94
ffshlim
static void ffshlim(FFFILE *f, ptrdiff_t lim)
Definition: avsscanf.c:83
SIZE_ll
#define SIZE_ll
Definition: avsscanf.c:50
fftoread
static int fftoread(FFFILE *f)
Definition: avsscanf.c:54