You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1170 lines
35KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "config.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/mem_internal.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/pixfmt.h"
  34. #include "avcodec.h"
  35. #include "codec.h"
  36. #include "hwconfig.h"
  37. #include "thread.h"
  38. #include "internal.h"
  39. #include "put_bits.h"
  40. #include "raw.h"
  41. #include "version.h"
  42. #include <stdlib.h>
  43. #include <stdarg.h>
  44. #include <stdatomic.h>
  45. #include <limits.h>
  46. #include <float.h>
  47. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  48. {
  49. uint8_t **p = ptr;
  50. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  51. av_freep(p);
  52. *size = 0;
  53. return;
  54. }
  55. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  56. memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  57. }
  58. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  59. {
  60. uint8_t **p = ptr;
  61. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  62. av_freep(p);
  63. *size = 0;
  64. return;
  65. }
  66. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  67. memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  68. }
  69. int av_codec_is_encoder(const AVCodec *codec)
  70. {
  71. return codec && (codec->encode_sub || codec->encode2 || codec->receive_packet);
  72. }
  73. int av_codec_is_decoder(const AVCodec *codec)
  74. {
  75. return codec && (codec->decode || codec->receive_frame);
  76. }
  77. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  78. {
  79. int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
  80. if (ret < 0)
  81. width = height = 0;
  82. s->coded_width = width;
  83. s->coded_height = height;
  84. s->width = AV_CEIL_RSHIFT(width, s->lowres);
  85. s->height = AV_CEIL_RSHIFT(height, s->lowres);
  86. return ret;
  87. }
  88. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  89. {
  90. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  91. if (ret < 0) {
  92. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  93. sar.num, sar.den);
  94. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  95. return ret;
  96. } else {
  97. avctx->sample_aspect_ratio = sar;
  98. }
  99. return 0;
  100. }
  101. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  102. enum AVMatrixEncoding matrix_encoding)
  103. {
  104. AVFrameSideData *side_data;
  105. enum AVMatrixEncoding *data;
  106. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  107. if (!side_data)
  108. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  109. sizeof(enum AVMatrixEncoding));
  110. if (!side_data)
  111. return AVERROR(ENOMEM);
  112. data = (enum AVMatrixEncoding*)side_data->data;
  113. *data = matrix_encoding;
  114. return 0;
  115. }
  116. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  117. int linesize_align[AV_NUM_DATA_POINTERS])
  118. {
  119. int i;
  120. int w_align = 1;
  121. int h_align = 1;
  122. AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
  123. if (desc) {
  124. w_align = 1 << desc->log2_chroma_w;
  125. h_align = 1 << desc->log2_chroma_h;
  126. }
  127. switch (s->pix_fmt) {
  128. case AV_PIX_FMT_YUV420P:
  129. case AV_PIX_FMT_YUYV422:
  130. case AV_PIX_FMT_YVYU422:
  131. case AV_PIX_FMT_UYVY422:
  132. case AV_PIX_FMT_YUV422P:
  133. case AV_PIX_FMT_YUV440P:
  134. case AV_PIX_FMT_YUV444P:
  135. case AV_PIX_FMT_GBRP:
  136. case AV_PIX_FMT_GBRAP:
  137. case AV_PIX_FMT_GRAY8:
  138. case AV_PIX_FMT_GRAY16BE:
  139. case AV_PIX_FMT_GRAY16LE:
  140. case AV_PIX_FMT_YUVJ420P:
  141. case AV_PIX_FMT_YUVJ422P:
  142. case AV_PIX_FMT_YUVJ440P:
  143. case AV_PIX_FMT_YUVJ444P:
  144. case AV_PIX_FMT_YUVA420P:
  145. case AV_PIX_FMT_YUVA422P:
  146. case AV_PIX_FMT_YUVA444P:
  147. case AV_PIX_FMT_YUV420P9LE:
  148. case AV_PIX_FMT_YUV420P9BE:
  149. case AV_PIX_FMT_YUV420P10LE:
  150. case AV_PIX_FMT_YUV420P10BE:
  151. case AV_PIX_FMT_YUV420P12LE:
  152. case AV_PIX_FMT_YUV420P12BE:
  153. case AV_PIX_FMT_YUV420P14LE:
  154. case AV_PIX_FMT_YUV420P14BE:
  155. case AV_PIX_FMT_YUV420P16LE:
  156. case AV_PIX_FMT_YUV420P16BE:
  157. case AV_PIX_FMT_YUVA420P9LE:
  158. case AV_PIX_FMT_YUVA420P9BE:
  159. case AV_PIX_FMT_YUVA420P10LE:
  160. case AV_PIX_FMT_YUVA420P10BE:
  161. case AV_PIX_FMT_YUVA420P16LE:
  162. case AV_PIX_FMT_YUVA420P16BE:
  163. case AV_PIX_FMT_YUV422P9LE:
  164. case AV_PIX_FMT_YUV422P9BE:
  165. case AV_PIX_FMT_YUV422P10LE:
  166. case AV_PIX_FMT_YUV422P10BE:
  167. case AV_PIX_FMT_YUV422P12LE:
  168. case AV_PIX_FMT_YUV422P12BE:
  169. case AV_PIX_FMT_YUV422P14LE:
  170. case AV_PIX_FMT_YUV422P14BE:
  171. case AV_PIX_FMT_YUV422P16LE:
  172. case AV_PIX_FMT_YUV422P16BE:
  173. case AV_PIX_FMT_YUVA422P9LE:
  174. case AV_PIX_FMT_YUVA422P9BE:
  175. case AV_PIX_FMT_YUVA422P10LE:
  176. case AV_PIX_FMT_YUVA422P10BE:
  177. case AV_PIX_FMT_YUVA422P12LE:
  178. case AV_PIX_FMT_YUVA422P12BE:
  179. case AV_PIX_FMT_YUVA422P16LE:
  180. case AV_PIX_FMT_YUVA422P16BE:
  181. case AV_PIX_FMT_YUV440P10LE:
  182. case AV_PIX_FMT_YUV440P10BE:
  183. case AV_PIX_FMT_YUV440P12LE:
  184. case AV_PIX_FMT_YUV440P12BE:
  185. case AV_PIX_FMT_YUV444P9LE:
  186. case AV_PIX_FMT_YUV444P9BE:
  187. case AV_PIX_FMT_YUV444P10LE:
  188. case AV_PIX_FMT_YUV444P10BE:
  189. case AV_PIX_FMT_YUV444P12LE:
  190. case AV_PIX_FMT_YUV444P12BE:
  191. case AV_PIX_FMT_YUV444P14LE:
  192. case AV_PIX_FMT_YUV444P14BE:
  193. case AV_PIX_FMT_YUV444P16LE:
  194. case AV_PIX_FMT_YUV444P16BE:
  195. case AV_PIX_FMT_YUVA444P9LE:
  196. case AV_PIX_FMT_YUVA444P9BE:
  197. case AV_PIX_FMT_YUVA444P10LE:
  198. case AV_PIX_FMT_YUVA444P10BE:
  199. case AV_PIX_FMT_YUVA444P12LE:
  200. case AV_PIX_FMT_YUVA444P12BE:
  201. case AV_PIX_FMT_YUVA444P16LE:
  202. case AV_PIX_FMT_YUVA444P16BE:
  203. case AV_PIX_FMT_GBRP9LE:
  204. case AV_PIX_FMT_GBRP9BE:
  205. case AV_PIX_FMT_GBRP10LE:
  206. case AV_PIX_FMT_GBRP10BE:
  207. case AV_PIX_FMT_GBRP12LE:
  208. case AV_PIX_FMT_GBRP12BE:
  209. case AV_PIX_FMT_GBRP14LE:
  210. case AV_PIX_FMT_GBRP14BE:
  211. case AV_PIX_FMT_GBRP16LE:
  212. case AV_PIX_FMT_GBRP16BE:
  213. case AV_PIX_FMT_GBRAP12LE:
  214. case AV_PIX_FMT_GBRAP12BE:
  215. case AV_PIX_FMT_GBRAP16LE:
  216. case AV_PIX_FMT_GBRAP16BE:
  217. w_align = 16; //FIXME assume 16 pixel per macroblock
  218. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  219. break;
  220. case AV_PIX_FMT_YUV411P:
  221. case AV_PIX_FMT_YUVJ411P:
  222. case AV_PIX_FMT_UYYVYY411:
  223. w_align = 32;
  224. h_align = 16 * 2;
  225. break;
  226. case AV_PIX_FMT_YUV410P:
  227. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  228. w_align = 64;
  229. h_align = 64;
  230. }
  231. break;
  232. case AV_PIX_FMT_RGB555:
  233. if (s->codec_id == AV_CODEC_ID_RPZA) {
  234. w_align = 4;
  235. h_align = 4;
  236. }
  237. if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  238. w_align = 8;
  239. h_align = 8;
  240. }
  241. break;
  242. case AV_PIX_FMT_PAL8:
  243. case AV_PIX_FMT_BGR8:
  244. case AV_PIX_FMT_RGB8:
  245. if (s->codec_id == AV_CODEC_ID_SMC ||
  246. s->codec_id == AV_CODEC_ID_CINEPAK) {
  247. w_align = 4;
  248. h_align = 4;
  249. }
  250. if (s->codec_id == AV_CODEC_ID_JV ||
  251. s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  252. w_align = 8;
  253. h_align = 8;
  254. }
  255. break;
  256. case AV_PIX_FMT_BGR24:
  257. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  258. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  259. w_align = 4;
  260. h_align = 4;
  261. }
  262. break;
  263. case AV_PIX_FMT_RGB24:
  264. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  265. w_align = 4;
  266. h_align = 4;
  267. }
  268. break;
  269. default:
  270. break;
  271. }
  272. if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
  273. w_align = FFMAX(w_align, 8);
  274. }
  275. *width = FFALIGN(*width, w_align);
  276. *height = FFALIGN(*height, h_align);
  277. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres ||
  278. s->codec_id == AV_CODEC_ID_VP5 || s->codec_id == AV_CODEC_ID_VP6 ||
  279. s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A
  280. ) {
  281. // some of the optimized chroma MC reads one line too much
  282. // which is also done in mpeg decoders with lowres > 0
  283. *height += 2;
  284. // H.264 uses edge emulation for out of frame motion vectors, for this
  285. // it requires a temporary area large enough to hold a 21x21 block,
  286. // increasing witdth ensure that the temporary area is large enough,
  287. // the next rounded up width is 32
  288. *width = FFMAX(*width, 32);
  289. }
  290. for (i = 0; i < 4; i++)
  291. linesize_align[i] = STRIDE_ALIGN;
  292. }
  293. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  294. {
  295. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  296. int chroma_shift = desc->log2_chroma_w;
  297. int linesize_align[AV_NUM_DATA_POINTERS];
  298. int align;
  299. avcodec_align_dimensions2(s, width, height, linesize_align);
  300. align = FFMAX(linesize_align[0], linesize_align[3]);
  301. linesize_align[1] <<= chroma_shift;
  302. linesize_align[2] <<= chroma_shift;
  303. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  304. *width = FFALIGN(*width, align);
  305. }
  306. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  307. {
  308. if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  309. return AVERROR(EINVAL);
  310. pos--;
  311. *xpos = (pos&1) * 128;
  312. *ypos = ((pos>>1)^(pos<4)) * 128;
  313. return 0;
  314. }
  315. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  316. {
  317. int pos, xout, yout;
  318. for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  319. if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  320. return pos;
  321. }
  322. return AVCHROMA_LOC_UNSPECIFIED;
  323. }
  324. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  325. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  326. int buf_size, int align)
  327. {
  328. int ch, planar, needed_size, ret = 0;
  329. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  330. frame->nb_samples, sample_fmt,
  331. align);
  332. if (buf_size < needed_size)
  333. return AVERROR(EINVAL);
  334. planar = av_sample_fmt_is_planar(sample_fmt);
  335. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  336. if (!(frame->extended_data = av_mallocz_array(nb_channels,
  337. sizeof(*frame->extended_data))))
  338. return AVERROR(ENOMEM);
  339. } else {
  340. frame->extended_data = frame->data;
  341. }
  342. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  343. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  344. sample_fmt, align)) < 0) {
  345. if (frame->extended_data != frame->data)
  346. av_freep(&frame->extended_data);
  347. return ret;
  348. }
  349. if (frame->extended_data != frame->data) {
  350. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  351. frame->data[ch] = frame->extended_data[ch];
  352. }
  353. return ret;
  354. }
  355. void ff_color_frame(AVFrame *frame, const int c[4])
  356. {
  357. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  358. int p, y;
  359. av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  360. for (p = 0; p<desc->nb_components; p++) {
  361. uint8_t *dst = frame->data[p];
  362. int is_chroma = p == 1 || p == 2;
  363. int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  364. int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  365. if (desc->comp[0].depth >= 9) {
  366. ((uint16_t*)dst)[0] = c[p];
  367. av_memcpy_backptr(dst + 2, 2, bytes - 2);
  368. dst += frame->linesize[p];
  369. for (y = 1; y < height; y++) {
  370. memcpy(dst, frame->data[p], 2*bytes);
  371. dst += frame->linesize[p];
  372. }
  373. } else {
  374. for (y = 0; y < height; y++) {
  375. memset(dst, c[p], bytes);
  376. dst += frame->linesize[p];
  377. }
  378. }
  379. }
  380. }
  381. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  382. unsigned int fourcc)
  383. {
  384. while (tags->pix_fmt >= 0) {
  385. if (tags->fourcc == fourcc)
  386. return tags->pix_fmt;
  387. tags++;
  388. }
  389. return AV_PIX_FMT_NONE;
  390. }
  391. #if FF_API_CODEC_GET_SET
  392. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  393. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  394. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  395. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  396. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  397. unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
  398. {
  399. return codec->properties;
  400. }
  401. int av_codec_get_max_lowres(const AVCodec *codec)
  402. {
  403. return codec->max_lowres;
  404. }
  405. #endif
  406. int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
  407. return !!(codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
  408. }
  409. const char *avcodec_get_name(enum AVCodecID id)
  410. {
  411. const AVCodecDescriptor *cd;
  412. const AVCodec *codec;
  413. if (id == AV_CODEC_ID_NONE)
  414. return "none";
  415. cd = avcodec_descriptor_get(id);
  416. if (cd)
  417. return cd->name;
  418. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  419. codec = avcodec_find_decoder(id);
  420. if (codec)
  421. return codec->name;
  422. codec = avcodec_find_encoder(id);
  423. if (codec)
  424. return codec->name;
  425. return "unknown_codec";
  426. }
  427. #if FF_API_TAG_STRING
  428. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  429. {
  430. int i, len, ret = 0;
  431. #define TAG_PRINT(x) \
  432. (((x) >= '0' && (x) <= '9') || \
  433. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  434. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  435. for (i = 0; i < 4; i++) {
  436. len = snprintf(buf, buf_size,
  437. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  438. buf += len;
  439. buf_size = buf_size > len ? buf_size - len : 0;
  440. ret += len;
  441. codec_tag >>= 8;
  442. }
  443. return ret;
  444. }
  445. #endif
  446. const char *av_get_profile_name(const AVCodec *codec, int profile)
  447. {
  448. const AVProfile *p;
  449. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  450. return NULL;
  451. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  452. if (p->profile == profile)
  453. return p->name;
  454. return NULL;
  455. }
  456. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  457. {
  458. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  459. const AVProfile *p;
  460. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  461. return NULL;
  462. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  463. if (p->profile == profile)
  464. return p->name;
  465. return NULL;
  466. }
  467. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  468. {
  469. switch (codec_id) {
  470. case AV_CODEC_ID_8SVX_EXP:
  471. case AV_CODEC_ID_8SVX_FIB:
  472. case AV_CODEC_ID_ADPCM_ARGO:
  473. case AV_CODEC_ID_ADPCM_CT:
  474. case AV_CODEC_ID_ADPCM_IMA_ALP:
  475. case AV_CODEC_ID_ADPCM_IMA_AMV:
  476. case AV_CODEC_ID_ADPCM_IMA_APC:
  477. case AV_CODEC_ID_ADPCM_IMA_APM:
  478. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  479. case AV_CODEC_ID_ADPCM_IMA_OKI:
  480. case AV_CODEC_ID_ADPCM_IMA_WS:
  481. case AV_CODEC_ID_ADPCM_IMA_SSI:
  482. case AV_CODEC_ID_ADPCM_G722:
  483. case AV_CODEC_ID_ADPCM_YAMAHA:
  484. case AV_CODEC_ID_ADPCM_AICA:
  485. return 4;
  486. case AV_CODEC_ID_DSD_LSBF:
  487. case AV_CODEC_ID_DSD_MSBF:
  488. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  489. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  490. case AV_CODEC_ID_PCM_ALAW:
  491. case AV_CODEC_ID_PCM_MULAW:
  492. case AV_CODEC_ID_PCM_VIDC:
  493. case AV_CODEC_ID_PCM_S8:
  494. case AV_CODEC_ID_PCM_S8_PLANAR:
  495. case AV_CODEC_ID_PCM_SGA:
  496. case AV_CODEC_ID_PCM_U8:
  497. case AV_CODEC_ID_SDX2_DPCM:
  498. case AV_CODEC_ID_DERF_DPCM:
  499. return 8;
  500. case AV_CODEC_ID_PCM_S16BE:
  501. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  502. case AV_CODEC_ID_PCM_S16LE:
  503. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  504. case AV_CODEC_ID_PCM_U16BE:
  505. case AV_CODEC_ID_PCM_U16LE:
  506. return 16;
  507. case AV_CODEC_ID_PCM_S24DAUD:
  508. case AV_CODEC_ID_PCM_S24BE:
  509. case AV_CODEC_ID_PCM_S24LE:
  510. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  511. case AV_CODEC_ID_PCM_U24BE:
  512. case AV_CODEC_ID_PCM_U24LE:
  513. return 24;
  514. case AV_CODEC_ID_PCM_S32BE:
  515. case AV_CODEC_ID_PCM_S32LE:
  516. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  517. case AV_CODEC_ID_PCM_U32BE:
  518. case AV_CODEC_ID_PCM_U32LE:
  519. case AV_CODEC_ID_PCM_F32BE:
  520. case AV_CODEC_ID_PCM_F32LE:
  521. case AV_CODEC_ID_PCM_F24LE:
  522. case AV_CODEC_ID_PCM_F16LE:
  523. return 32;
  524. case AV_CODEC_ID_PCM_F64BE:
  525. case AV_CODEC_ID_PCM_F64LE:
  526. case AV_CODEC_ID_PCM_S64BE:
  527. case AV_CODEC_ID_PCM_S64LE:
  528. return 64;
  529. default:
  530. return 0;
  531. }
  532. }
  533. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  534. {
  535. static const enum AVCodecID map[][2] = {
  536. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  537. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  538. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  539. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  540. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  541. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  542. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  543. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  544. [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
  545. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  546. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  547. };
  548. if (fmt < 0 || fmt >= FF_ARRAY_ELEMS(map))
  549. return AV_CODEC_ID_NONE;
  550. if (be < 0 || be > 1)
  551. be = AV_NE(1, 0);
  552. return map[fmt][be];
  553. }
  554. int av_get_bits_per_sample(enum AVCodecID codec_id)
  555. {
  556. switch (codec_id) {
  557. case AV_CODEC_ID_ADPCM_SBPRO_2:
  558. return 2;
  559. case AV_CODEC_ID_ADPCM_SBPRO_3:
  560. return 3;
  561. case AV_CODEC_ID_ADPCM_SBPRO_4:
  562. case AV_CODEC_ID_ADPCM_IMA_WAV:
  563. case AV_CODEC_ID_ADPCM_IMA_QT:
  564. case AV_CODEC_ID_ADPCM_SWF:
  565. case AV_CODEC_ID_ADPCM_MS:
  566. return 4;
  567. default:
  568. return av_get_exact_bits_per_sample(codec_id);
  569. }
  570. }
  571. static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
  572. uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
  573. uint8_t * extradata, int frame_size, int frame_bytes)
  574. {
  575. int bps = av_get_exact_bits_per_sample(id);
  576. int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
  577. /* codecs with an exact constant bits per sample */
  578. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  579. return (frame_bytes * 8LL) / (bps * ch);
  580. bps = bits_per_coded_sample;
  581. /* codecs with a fixed packet duration */
  582. switch (id) {
  583. case AV_CODEC_ID_ADPCM_ADX: return 32;
  584. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  585. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  586. case AV_CODEC_ID_AMR_NB:
  587. case AV_CODEC_ID_EVRC:
  588. case AV_CODEC_ID_GSM:
  589. case AV_CODEC_ID_QCELP:
  590. case AV_CODEC_ID_RA_288: return 160;
  591. case AV_CODEC_ID_AMR_WB:
  592. case AV_CODEC_ID_GSM_MS: return 320;
  593. case AV_CODEC_ID_MP1: return 384;
  594. case AV_CODEC_ID_ATRAC1: return 512;
  595. case AV_CODEC_ID_ATRAC9:
  596. case AV_CODEC_ID_ATRAC3:
  597. if (framecount > INT_MAX/1024)
  598. return 0;
  599. return 1024 * framecount;
  600. case AV_CODEC_ID_ATRAC3P: return 2048;
  601. case AV_CODEC_ID_MP2:
  602. case AV_CODEC_ID_MUSEPACK7: return 1152;
  603. case AV_CODEC_ID_AC3: return 1536;
  604. }
  605. if (sr > 0) {
  606. /* calc from sample rate */
  607. if (id == AV_CODEC_ID_TTA)
  608. return 256 * sr / 245;
  609. else if (id == AV_CODEC_ID_DST)
  610. return 588 * sr / 44100;
  611. else if (id == AV_CODEC_ID_BINKAUDIO_DCT) {
  612. if (sr / 22050 > 22)
  613. return 0;
  614. return (480 << (sr / 22050));
  615. }
  616. if (id == AV_CODEC_ID_MP3)
  617. return sr <= 24000 ? 576 : 1152;
  618. }
  619. if (ba > 0) {
  620. /* calc from block_align */
  621. if (id == AV_CODEC_ID_SIPR) {
  622. switch (ba) {
  623. case 20: return 160;
  624. case 19: return 144;
  625. case 29: return 288;
  626. case 37: return 480;
  627. }
  628. } else if (id == AV_CODEC_ID_ILBC) {
  629. switch (ba) {
  630. case 38: return 160;
  631. case 50: return 240;
  632. }
  633. }
  634. }
  635. if (frame_bytes > 0) {
  636. /* calc from frame_bytes only */
  637. if (id == AV_CODEC_ID_TRUESPEECH)
  638. return 240 * (frame_bytes / 32);
  639. if (id == AV_CODEC_ID_NELLYMOSER)
  640. return 256 * (frame_bytes / 64);
  641. if (id == AV_CODEC_ID_RA_144)
  642. return 160 * (frame_bytes / 20);
  643. if (bps > 0) {
  644. /* calc from frame_bytes and bits_per_coded_sample */
  645. if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
  646. return frame_bytes * 8 / bps;
  647. }
  648. if (ch > 0 && ch < INT_MAX/16) {
  649. /* calc from frame_bytes and channels */
  650. switch (id) {
  651. case AV_CODEC_ID_FASTAUDIO:
  652. return frame_bytes / (40 * ch) * 256;
  653. case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
  654. return (frame_bytes - 4 * ch) / (128 * ch) * 256;
  655. case AV_CODEC_ID_ADPCM_AFC:
  656. return frame_bytes / (9 * ch) * 16;
  657. case AV_CODEC_ID_ADPCM_PSX:
  658. case AV_CODEC_ID_ADPCM_DTK:
  659. frame_bytes /= 16 * ch;
  660. if (frame_bytes > INT_MAX / 28)
  661. return 0;
  662. return frame_bytes * 28;
  663. case AV_CODEC_ID_ADPCM_4XM:
  664. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  665. case AV_CODEC_ID_ADPCM_IMA_ISS:
  666. return (frame_bytes - 4 * ch) * 2 / ch;
  667. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  668. return (frame_bytes - 4) * 2 / ch;
  669. case AV_CODEC_ID_ADPCM_IMA_AMV:
  670. return (frame_bytes - 8) * 2;
  671. case AV_CODEC_ID_ADPCM_THP:
  672. case AV_CODEC_ID_ADPCM_THP_LE:
  673. if (extradata)
  674. return frame_bytes * 14 / (8 * ch);
  675. break;
  676. case AV_CODEC_ID_ADPCM_XA:
  677. return (frame_bytes / 128) * 224 / ch;
  678. case AV_CODEC_ID_INTERPLAY_DPCM:
  679. return (frame_bytes - 6 - ch) / ch;
  680. case AV_CODEC_ID_ROQ_DPCM:
  681. return (frame_bytes - 8) / ch;
  682. case AV_CODEC_ID_XAN_DPCM:
  683. return (frame_bytes - 2 * ch) / ch;
  684. case AV_CODEC_ID_MACE3:
  685. return 3 * frame_bytes / ch;
  686. case AV_CODEC_ID_MACE6:
  687. return 6 * frame_bytes / ch;
  688. case AV_CODEC_ID_PCM_LXF:
  689. return 2 * (frame_bytes / (5 * ch));
  690. case AV_CODEC_ID_IAC:
  691. case AV_CODEC_ID_IMC:
  692. return 4 * frame_bytes / ch;
  693. }
  694. if (tag) {
  695. /* calc from frame_bytes, channels, and codec_tag */
  696. if (id == AV_CODEC_ID_SOL_DPCM) {
  697. if (tag == 3)
  698. return frame_bytes / ch;
  699. else
  700. return frame_bytes * 2 / ch;
  701. }
  702. }
  703. if (ba > 0) {
  704. /* calc from frame_bytes, channels, and block_align */
  705. int blocks = frame_bytes / ba;
  706. switch (id) {
  707. case AV_CODEC_ID_ADPCM_IMA_WAV:
  708. if (bps < 2 || bps > 5)
  709. return 0;
  710. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  711. case AV_CODEC_ID_ADPCM_IMA_DK3:
  712. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  713. case AV_CODEC_ID_ADPCM_IMA_DK4:
  714. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  715. case AV_CODEC_ID_ADPCM_IMA_RAD:
  716. return blocks * ((ba - 4 * ch) * 2 / ch);
  717. case AV_CODEC_ID_ADPCM_MS:
  718. return blocks * (2 + (ba - 7 * ch) * 2LL / ch);
  719. case AV_CODEC_ID_ADPCM_MTAF:
  720. return blocks * (ba - 16) * 2 / ch;
  721. }
  722. }
  723. if (bps > 0) {
  724. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  725. switch (id) {
  726. case AV_CODEC_ID_PCM_DVD:
  727. if(bps<4 || frame_bytes<3)
  728. return 0;
  729. return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
  730. case AV_CODEC_ID_PCM_BLURAY:
  731. if(bps<4 || frame_bytes<4)
  732. return 0;
  733. return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
  734. case AV_CODEC_ID_S302M:
  735. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  736. }
  737. }
  738. }
  739. }
  740. /* Fall back on using frame_size */
  741. if (frame_size > 1 && frame_bytes)
  742. return frame_size;
  743. //For WMA we currently have no other means to calculate duration thus we
  744. //do it here by assuming CBR, which is true for all known cases.
  745. if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
  746. if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
  747. return (frame_bytes * 8LL * sr) / bitrate;
  748. }
  749. return 0;
  750. }
  751. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  752. {
  753. return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
  754. avctx->channels, avctx->block_align,
  755. avctx->codec_tag, avctx->bits_per_coded_sample,
  756. avctx->bit_rate, avctx->extradata, avctx->frame_size,
  757. frame_bytes);
  758. }
  759. int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
  760. {
  761. return get_audio_frame_duration(par->codec_id, par->sample_rate,
  762. par->channels, par->block_align,
  763. par->codec_tag, par->bits_per_coded_sample,
  764. par->bit_rate, par->extradata, par->frame_size,
  765. frame_bytes);
  766. }
  767. #if !HAVE_THREADS
  768. int ff_thread_init(AVCodecContext *s)
  769. {
  770. return -1;
  771. }
  772. #endif
  773. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  774. {
  775. unsigned int n = 0;
  776. while (v >= 0xff) {
  777. *s++ = 0xff;
  778. v -= 0xff;
  779. n++;
  780. }
  781. *s = v;
  782. n++;
  783. return n;
  784. }
  785. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  786. {
  787. int i;
  788. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  789. return i;
  790. }
  791. const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index)
  792. {
  793. int i;
  794. if (!codec->hw_configs || index < 0)
  795. return NULL;
  796. for (i = 0; i <= index; i++)
  797. if (!codec->hw_configs[i])
  798. return NULL;
  799. return &codec->hw_configs[index]->public;
  800. }
  801. #if FF_API_USER_VISIBLE_AVHWACCEL
  802. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  803. {
  804. return NULL;
  805. }
  806. void av_register_hwaccel(AVHWAccel *hwaccel)
  807. {
  808. }
  809. #endif
  810. unsigned int avpriv_toupper4(unsigned int x)
  811. {
  812. return av_toupper(x & 0xFF) +
  813. (av_toupper((x >> 8) & 0xFF) << 8) +
  814. (av_toupper((x >> 16) & 0xFF) << 16) +
  815. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  816. }
  817. int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
  818. {
  819. int ret;
  820. dst->owner[0] = src->owner[0];
  821. dst->owner[1] = src->owner[1];
  822. ret = av_frame_ref(dst->f, src->f);
  823. if (ret < 0)
  824. return ret;
  825. av_assert0(!dst->progress);
  826. if (src->progress &&
  827. !(dst->progress = av_buffer_ref(src->progress))) {
  828. ff_thread_release_buffer(dst->owner[0], dst);
  829. return AVERROR(ENOMEM);
  830. }
  831. return 0;
  832. }
  833. #if !HAVE_THREADS
  834. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  835. {
  836. return ff_get_format(avctx, fmt);
  837. }
  838. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  839. {
  840. f->owner[0] = f->owner[1] = avctx;
  841. return ff_get_buffer(avctx, f->f, flags);
  842. }
  843. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  844. {
  845. if (f->f)
  846. av_frame_unref(f->f);
  847. }
  848. void ff_thread_finish_setup(AVCodecContext *avctx)
  849. {
  850. }
  851. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  852. {
  853. }
  854. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  855. {
  856. }
  857. int ff_thread_can_start_frame(AVCodecContext *avctx)
  858. {
  859. return 1;
  860. }
  861. int ff_alloc_entries(AVCodecContext *avctx, int count)
  862. {
  863. return 0;
  864. }
  865. void ff_reset_entries(AVCodecContext *avctx)
  866. {
  867. }
  868. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  869. {
  870. }
  871. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  872. {
  873. }
  874. #endif
  875. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  876. const uint8_t *end,
  877. uint32_t *av_restrict state)
  878. {
  879. int i;
  880. av_assert0(p <= end);
  881. if (p >= end)
  882. return end;
  883. for (i = 0; i < 3; i++) {
  884. uint32_t tmp = *state << 8;
  885. *state = tmp + *(p++);
  886. if (tmp == 0x100 || p == end)
  887. return p;
  888. }
  889. while (p < end) {
  890. if (p[-1] > 1 ) p += 3;
  891. else if (p[-2] ) p += 2;
  892. else if (p[-3]|(p[-1]-1)) p++;
  893. else {
  894. p++;
  895. break;
  896. }
  897. }
  898. p = FFMIN(p, end) - 4;
  899. *state = AV_RB32(p);
  900. return p + 4;
  901. }
  902. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  903. {
  904. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  905. if (!props)
  906. return NULL;
  907. if (size)
  908. *size = sizeof(*props);
  909. props->vbv_delay = UINT64_MAX;
  910. return props;
  911. }
  912. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  913. {
  914. AVPacketSideData *tmp;
  915. AVCPBProperties *props;
  916. size_t size;
  917. int i;
  918. for (i = 0; i < avctx->nb_coded_side_data; i++)
  919. if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
  920. return (AVCPBProperties *)avctx->coded_side_data[i].data;
  921. props = av_cpb_properties_alloc(&size);
  922. if (!props)
  923. return NULL;
  924. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  925. if (!tmp) {
  926. av_freep(&props);
  927. return NULL;
  928. }
  929. avctx->coded_side_data = tmp;
  930. avctx->nb_coded_side_data++;
  931. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  932. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  933. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  934. return props;
  935. }
  936. static unsigned bcd2uint(uint8_t bcd)
  937. {
  938. unsigned low = bcd & 0xf;
  939. unsigned high = bcd >> 4;
  940. if (low > 9 || high > 9)
  941. return 0;
  942. return low + 10*high;
  943. }
  944. int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
  945. void **data, size_t *sei_size)
  946. {
  947. AVFrameSideData *sd = NULL;
  948. uint8_t *sei_data;
  949. PutBitContext pb;
  950. uint32_t *tc;
  951. int m;
  952. if (frame)
  953. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE);
  954. if (!sd) {
  955. *data = NULL;
  956. return 0;
  957. }
  958. tc = (uint32_t*)sd->data;
  959. m = tc[0] & 3;
  960. *sei_size = sizeof(uint32_t) * 4;
  961. *data = av_mallocz(*sei_size + prefix_len);
  962. if (!*data)
  963. return AVERROR(ENOMEM);
  964. sei_data = (uint8_t*)*data + prefix_len;
  965. init_put_bits(&pb, sei_data, *sei_size);
  966. put_bits(&pb, 2, m); // num_clock_ts
  967. for (int j = 1; j <= m; j++) {
  968. uint32_t tcsmpte = tc[j];
  969. unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
  970. unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
  971. unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
  972. unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
  973. unsigned drop = tcsmpte & 1<<30 && !0; // 1-bit drop if not arbitrary bit
  974. /* Calculate frame number of HEVC by SMPTE ST 12-1:2014 Sec 12.2 if rate > 30FPS */
  975. if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
  976. unsigned pc;
  977. ff *= 2;
  978. if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
  979. pc = !!(tcsmpte & 1 << 7);
  980. else
  981. pc = !!(tcsmpte & 1 << 23);
  982. ff = (ff + pc) & 0x7f;
  983. }
  984. put_bits(&pb, 1, 1); // clock_timestamp_flag
  985. put_bits(&pb, 1, 1); // units_field_based_flag
  986. put_bits(&pb, 5, 0); // counting_type
  987. put_bits(&pb, 1, 1); // full_timestamp_flag
  988. put_bits(&pb, 1, 0); // discontinuity_flag
  989. put_bits(&pb, 1, drop);
  990. put_bits(&pb, 9, ff);
  991. put_bits(&pb, 6, ss);
  992. put_bits(&pb, 6, mm);
  993. put_bits(&pb, 5, hh);
  994. put_bits(&pb, 5, 0);
  995. }
  996. flush_put_bits(&pb);
  997. return 0;
  998. }
  999. int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
  1000. {
  1001. AVRational framerate = avctx->framerate;
  1002. int bits_per_coded_sample = avctx->bits_per_coded_sample;
  1003. int64_t bitrate;
  1004. if (!(framerate.num && framerate.den))
  1005. framerate = av_inv_q(avctx->time_base);
  1006. if (!(framerate.num && framerate.den))
  1007. return 0;
  1008. if (!bits_per_coded_sample) {
  1009. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1010. bits_per_coded_sample = av_get_bits_per_pixel(desc);
  1011. }
  1012. bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
  1013. framerate.num / framerate.den;
  1014. return bitrate;
  1015. }
  1016. int ff_int_from_list_or_default(void *ctx, const char * val_name, int val,
  1017. const int * array_valid_values, int default_value)
  1018. {
  1019. int i = 0, ref_val;
  1020. while (1) {
  1021. ref_val = array_valid_values[i];
  1022. if (ref_val == INT_MAX)
  1023. break;
  1024. if (val == ref_val)
  1025. return val;
  1026. i++;
  1027. }
  1028. /* val is not a valid value */
  1029. av_log(ctx, AV_LOG_DEBUG,
  1030. "%s %d are not supported. Set to default value : %d\n", val_name, val, default_value);
  1031. return default_value;
  1032. }