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.

3686 lines
120KB

  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/atomic.h"
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/bprint.h"
  32. #include "libavutil/channel_layout.h"
  33. #include "libavutil/crc.h"
  34. #include "libavutil/frame.h"
  35. #include "libavutil/internal.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/pixdesc.h"
  38. #include "libavutil/imgutils.h"
  39. #include "libavutil/samplefmt.h"
  40. #include "libavutil/dict.h"
  41. #include "avcodec.h"
  42. #include "libavutil/opt.h"
  43. #include "me_cmp.h"
  44. #include "mpegvideo.h"
  45. #include "thread.h"
  46. #include "frame_thread_encoder.h"
  47. #include "internal.h"
  48. #include "raw.h"
  49. #include "bytestream.h"
  50. #include "version.h"
  51. #include <stdlib.h>
  52. #include <stdarg.h>
  53. #include <limits.h>
  54. #include <float.h>
  55. #if CONFIG_ICONV
  56. # include <iconv.h>
  57. #endif
  58. #if HAVE_PTHREADS
  59. #include <pthread.h>
  60. #elif HAVE_W32THREADS
  61. #include "compat/w32pthreads.h"
  62. #elif HAVE_OS2THREADS
  63. #include "compat/os2threads.h"
  64. #endif
  65. #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
  66. static int default_lockmgr_cb(void **arg, enum AVLockOp op)
  67. {
  68. void * volatile * mutex = arg;
  69. int err;
  70. switch (op) {
  71. case AV_LOCK_CREATE:
  72. return 0;
  73. case AV_LOCK_OBTAIN:
  74. if (!*mutex) {
  75. pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
  76. if (!tmp)
  77. return AVERROR(ENOMEM);
  78. if ((err = pthread_mutex_init(tmp, NULL))) {
  79. av_free(tmp);
  80. return AVERROR(err);
  81. }
  82. if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
  83. pthread_mutex_destroy(tmp);
  84. av_free(tmp);
  85. }
  86. }
  87. if ((err = pthread_mutex_lock(*mutex)))
  88. return AVERROR(err);
  89. return 0;
  90. case AV_LOCK_RELEASE:
  91. if ((err = pthread_mutex_unlock(*mutex)))
  92. return AVERROR(err);
  93. return 0;
  94. case AV_LOCK_DESTROY:
  95. if (*mutex)
  96. pthread_mutex_destroy(*mutex);
  97. av_free(*mutex);
  98. avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
  104. #else
  105. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
  106. #endif
  107. volatile int ff_avcodec_locked;
  108. static int volatile entangled_thread_counter = 0;
  109. static void *codec_mutex;
  110. static void *avformat_mutex;
  111. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  112. {
  113. void **p = ptr;
  114. if (min_size <= *size && *p)
  115. return 0;
  116. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  117. av_free(*p);
  118. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  119. if (!*p)
  120. min_size = 0;
  121. *size = min_size;
  122. return 1;
  123. }
  124. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  125. {
  126. uint8_t **p = ptr;
  127. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  128. av_freep(p);
  129. *size = 0;
  130. return;
  131. }
  132. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  133. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  134. }
  135. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  136. {
  137. uint8_t **p = ptr;
  138. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  139. av_freep(p);
  140. *size = 0;
  141. return;
  142. }
  143. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  144. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  145. }
  146. /* encoder management */
  147. static AVCodec *first_avcodec = NULL;
  148. static AVCodec **last_avcodec = &first_avcodec;
  149. AVCodec *av_codec_next(const AVCodec *c)
  150. {
  151. if (c)
  152. return c->next;
  153. else
  154. return first_avcodec;
  155. }
  156. static av_cold void avcodec_init(void)
  157. {
  158. static int initialized = 0;
  159. if (initialized != 0)
  160. return;
  161. initialized = 1;
  162. if (CONFIG_ME_CMP)
  163. ff_me_cmp_init_static();
  164. }
  165. int av_codec_is_encoder(const AVCodec *codec)
  166. {
  167. return codec && (codec->encode_sub || codec->encode2);
  168. }
  169. int av_codec_is_decoder(const AVCodec *codec)
  170. {
  171. return codec && codec->decode;
  172. }
  173. av_cold void avcodec_register(AVCodec *codec)
  174. {
  175. AVCodec **p;
  176. avcodec_init();
  177. p = last_avcodec;
  178. codec->next = NULL;
  179. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
  180. p = &(*p)->next;
  181. last_avcodec = &codec->next;
  182. if (codec->init_static_data)
  183. codec->init_static_data(codec);
  184. }
  185. #if FF_API_EMU_EDGE
  186. unsigned avcodec_get_edge_width(void)
  187. {
  188. return EDGE_WIDTH;
  189. }
  190. #endif
  191. #if FF_API_SET_DIMENSIONS
  192. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  193. {
  194. int ret = ff_set_dimensions(s, width, height);
  195. if (ret < 0) {
  196. av_log(s, AV_LOG_WARNING, "Failed to set dimensions %d %d\n", width, height);
  197. }
  198. }
  199. #endif
  200. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  201. {
  202. int ret = av_image_check_size(width, height, 0, s);
  203. if (ret < 0)
  204. width = height = 0;
  205. s->coded_width = width;
  206. s->coded_height = height;
  207. s->width = FF_CEIL_RSHIFT(width, s->lowres);
  208. s->height = FF_CEIL_RSHIFT(height, s->lowres);
  209. return ret;
  210. }
  211. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  212. {
  213. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  214. if (ret < 0) {
  215. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  216. sar.num, sar.den);
  217. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  218. return ret;
  219. } else {
  220. avctx->sample_aspect_ratio = sar;
  221. }
  222. return 0;
  223. }
  224. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  225. enum AVMatrixEncoding matrix_encoding)
  226. {
  227. AVFrameSideData *side_data;
  228. enum AVMatrixEncoding *data;
  229. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  230. if (!side_data)
  231. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  232. sizeof(enum AVMatrixEncoding));
  233. if (!side_data)
  234. return AVERROR(ENOMEM);
  235. data = (enum AVMatrixEncoding*)side_data->data;
  236. *data = matrix_encoding;
  237. return 0;
  238. }
  239. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  240. int linesize_align[AV_NUM_DATA_POINTERS])
  241. {
  242. int i;
  243. int w_align = 1;
  244. int h_align = 1;
  245. switch (s->pix_fmt) {
  246. case AV_PIX_FMT_YUV420P:
  247. case AV_PIX_FMT_YUYV422:
  248. case AV_PIX_FMT_YVYU422:
  249. case AV_PIX_FMT_UYVY422:
  250. case AV_PIX_FMT_YUV422P:
  251. case AV_PIX_FMT_YUV440P:
  252. case AV_PIX_FMT_YUV444P:
  253. case AV_PIX_FMT_GBRAP:
  254. case AV_PIX_FMT_GBRP:
  255. case AV_PIX_FMT_GRAY8:
  256. case AV_PIX_FMT_GRAY16BE:
  257. case AV_PIX_FMT_GRAY16LE:
  258. case AV_PIX_FMT_YUVJ420P:
  259. case AV_PIX_FMT_YUVJ422P:
  260. case AV_PIX_FMT_YUVJ440P:
  261. case AV_PIX_FMT_YUVJ444P:
  262. case AV_PIX_FMT_YUVA420P:
  263. case AV_PIX_FMT_YUVA422P:
  264. case AV_PIX_FMT_YUVA444P:
  265. case AV_PIX_FMT_YUV420P9LE:
  266. case AV_PIX_FMT_YUV420P9BE:
  267. case AV_PIX_FMT_YUV420P10LE:
  268. case AV_PIX_FMT_YUV420P10BE:
  269. case AV_PIX_FMT_YUV420P12LE:
  270. case AV_PIX_FMT_YUV420P12BE:
  271. case AV_PIX_FMT_YUV420P14LE:
  272. case AV_PIX_FMT_YUV420P14BE:
  273. case AV_PIX_FMT_YUV420P16LE:
  274. case AV_PIX_FMT_YUV420P16BE:
  275. case AV_PIX_FMT_YUVA420P9LE:
  276. case AV_PIX_FMT_YUVA420P9BE:
  277. case AV_PIX_FMT_YUVA420P10LE:
  278. case AV_PIX_FMT_YUVA420P10BE:
  279. case AV_PIX_FMT_YUVA420P16LE:
  280. case AV_PIX_FMT_YUVA420P16BE:
  281. case AV_PIX_FMT_YUV422P9LE:
  282. case AV_PIX_FMT_YUV422P9BE:
  283. case AV_PIX_FMT_YUV422P10LE:
  284. case AV_PIX_FMT_YUV422P10BE:
  285. case AV_PIX_FMT_YUV422P12LE:
  286. case AV_PIX_FMT_YUV422P12BE:
  287. case AV_PIX_FMT_YUV422P14LE:
  288. case AV_PIX_FMT_YUV422P14BE:
  289. case AV_PIX_FMT_YUV422P16LE:
  290. case AV_PIX_FMT_YUV422P16BE:
  291. case AV_PIX_FMT_YUVA422P9LE:
  292. case AV_PIX_FMT_YUVA422P9BE:
  293. case AV_PIX_FMT_YUVA422P10LE:
  294. case AV_PIX_FMT_YUVA422P10BE:
  295. case AV_PIX_FMT_YUVA422P16LE:
  296. case AV_PIX_FMT_YUVA422P16BE:
  297. case AV_PIX_FMT_YUV444P9LE:
  298. case AV_PIX_FMT_YUV444P9BE:
  299. case AV_PIX_FMT_YUV444P10LE:
  300. case AV_PIX_FMT_YUV444P10BE:
  301. case AV_PIX_FMT_YUV444P12LE:
  302. case AV_PIX_FMT_YUV444P12BE:
  303. case AV_PIX_FMT_YUV444P14LE:
  304. case AV_PIX_FMT_YUV444P14BE:
  305. case AV_PIX_FMT_YUV444P16LE:
  306. case AV_PIX_FMT_YUV444P16BE:
  307. case AV_PIX_FMT_YUVA444P9LE:
  308. case AV_PIX_FMT_YUVA444P9BE:
  309. case AV_PIX_FMT_YUVA444P10LE:
  310. case AV_PIX_FMT_YUVA444P10BE:
  311. case AV_PIX_FMT_YUVA444P16LE:
  312. case AV_PIX_FMT_YUVA444P16BE:
  313. case AV_PIX_FMT_GBRP9LE:
  314. case AV_PIX_FMT_GBRP9BE:
  315. case AV_PIX_FMT_GBRP10LE:
  316. case AV_PIX_FMT_GBRP10BE:
  317. case AV_PIX_FMT_GBRP12LE:
  318. case AV_PIX_FMT_GBRP12BE:
  319. case AV_PIX_FMT_GBRP14LE:
  320. case AV_PIX_FMT_GBRP14BE:
  321. case AV_PIX_FMT_GBRP16LE:
  322. case AV_PIX_FMT_GBRP16BE:
  323. w_align = 16; //FIXME assume 16 pixel per macroblock
  324. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  325. break;
  326. case AV_PIX_FMT_YUV411P:
  327. case AV_PIX_FMT_YUVJ411P:
  328. case AV_PIX_FMT_UYYVYY411:
  329. w_align = 32;
  330. h_align = 8;
  331. break;
  332. case AV_PIX_FMT_YUV410P:
  333. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  334. w_align = 64;
  335. h_align = 64;
  336. }
  337. break;
  338. case AV_PIX_FMT_RGB555:
  339. if (s->codec_id == AV_CODEC_ID_RPZA) {
  340. w_align = 4;
  341. h_align = 4;
  342. }
  343. break;
  344. case AV_PIX_FMT_PAL8:
  345. case AV_PIX_FMT_BGR8:
  346. case AV_PIX_FMT_RGB8:
  347. if (s->codec_id == AV_CODEC_ID_SMC ||
  348. s->codec_id == AV_CODEC_ID_CINEPAK) {
  349. w_align = 4;
  350. h_align = 4;
  351. }
  352. break;
  353. case AV_PIX_FMT_BGR24:
  354. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  355. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  356. w_align = 4;
  357. h_align = 4;
  358. }
  359. break;
  360. case AV_PIX_FMT_RGB24:
  361. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  362. w_align = 4;
  363. h_align = 4;
  364. }
  365. break;
  366. default:
  367. w_align = 1;
  368. h_align = 1;
  369. break;
  370. }
  371. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  372. w_align = FFMAX(w_align, 8);
  373. }
  374. *width = FFALIGN(*width, w_align);
  375. *height = FFALIGN(*height, h_align);
  376. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  377. // some of the optimized chroma MC reads one line too much
  378. // which is also done in mpeg decoders with lowres > 0
  379. *height += 2;
  380. for (i = 0; i < 4; i++)
  381. linesize_align[i] = STRIDE_ALIGN;
  382. }
  383. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  384. {
  385. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  386. int chroma_shift = desc->log2_chroma_w;
  387. int linesize_align[AV_NUM_DATA_POINTERS];
  388. int align;
  389. avcodec_align_dimensions2(s, width, height, linesize_align);
  390. align = FFMAX(linesize_align[0], linesize_align[3]);
  391. linesize_align[1] <<= chroma_shift;
  392. linesize_align[2] <<= chroma_shift;
  393. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  394. *width = FFALIGN(*width, align);
  395. }
  396. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  397. {
  398. if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  399. return AVERROR(EINVAL);
  400. pos--;
  401. *xpos = (pos&1) * 128;
  402. *ypos = ((pos>>1)^(pos<4)) * 128;
  403. return 0;
  404. }
  405. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  406. {
  407. int pos, xout, yout;
  408. for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  409. if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  410. return pos;
  411. }
  412. return AVCHROMA_LOC_UNSPECIFIED;
  413. }
  414. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  415. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  416. int buf_size, int align)
  417. {
  418. int ch, planar, needed_size, ret = 0;
  419. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  420. frame->nb_samples, sample_fmt,
  421. align);
  422. if (buf_size < needed_size)
  423. return AVERROR(EINVAL);
  424. planar = av_sample_fmt_is_planar(sample_fmt);
  425. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  426. if (!(frame->extended_data = av_mallocz_array(nb_channels,
  427. sizeof(*frame->extended_data))))
  428. return AVERROR(ENOMEM);
  429. } else {
  430. frame->extended_data = frame->data;
  431. }
  432. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  433. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  434. sample_fmt, align)) < 0) {
  435. if (frame->extended_data != frame->data)
  436. av_freep(&frame->extended_data);
  437. return ret;
  438. }
  439. if (frame->extended_data != frame->data) {
  440. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  441. frame->data[ch] = frame->extended_data[ch];
  442. }
  443. return ret;
  444. }
  445. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  446. {
  447. FramePool *pool = avctx->internal->pool;
  448. int i, ret;
  449. switch (avctx->codec_type) {
  450. case AVMEDIA_TYPE_VIDEO: {
  451. AVPicture picture;
  452. int size[4] = { 0 };
  453. int w = frame->width;
  454. int h = frame->height;
  455. int tmpsize, unaligned;
  456. if (pool->format == frame->format &&
  457. pool->width == frame->width && pool->height == frame->height)
  458. return 0;
  459. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  460. do {
  461. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  462. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  463. av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
  464. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  465. w += w & ~(w - 1);
  466. unaligned = 0;
  467. for (i = 0; i < 4; i++)
  468. unaligned |= picture.linesize[i] % pool->stride_align[i];
  469. } while (unaligned);
  470. tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
  471. NULL, picture.linesize);
  472. if (tmpsize < 0)
  473. return -1;
  474. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  475. size[i] = picture.data[i + 1] - picture.data[i];
  476. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  477. for (i = 0; i < 4; i++) {
  478. av_buffer_pool_uninit(&pool->pools[i]);
  479. pool->linesize[i] = picture.linesize[i];
  480. if (size[i]) {
  481. pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
  482. CONFIG_MEMORY_POISONING ?
  483. NULL :
  484. av_buffer_allocz);
  485. if (!pool->pools[i]) {
  486. ret = AVERROR(ENOMEM);
  487. goto fail;
  488. }
  489. }
  490. }
  491. pool->format = frame->format;
  492. pool->width = frame->width;
  493. pool->height = frame->height;
  494. break;
  495. }
  496. case AVMEDIA_TYPE_AUDIO: {
  497. int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
  498. int planar = av_sample_fmt_is_planar(frame->format);
  499. int planes = planar ? ch : 1;
  500. if (pool->format == frame->format && pool->planes == planes &&
  501. pool->channels == ch && frame->nb_samples == pool->samples)
  502. return 0;
  503. av_buffer_pool_uninit(&pool->pools[0]);
  504. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  505. frame->nb_samples, frame->format, 0);
  506. if (ret < 0)
  507. goto fail;
  508. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  509. if (!pool->pools[0]) {
  510. ret = AVERROR(ENOMEM);
  511. goto fail;
  512. }
  513. pool->format = frame->format;
  514. pool->planes = planes;
  515. pool->channels = ch;
  516. pool->samples = frame->nb_samples;
  517. break;
  518. }
  519. default: av_assert0(0);
  520. }
  521. return 0;
  522. fail:
  523. for (i = 0; i < 4; i++)
  524. av_buffer_pool_uninit(&pool->pools[i]);
  525. pool->format = -1;
  526. pool->planes = pool->channels = pool->samples = 0;
  527. pool->width = pool->height = 0;
  528. return ret;
  529. }
  530. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  531. {
  532. FramePool *pool = avctx->internal->pool;
  533. int planes = pool->planes;
  534. int i;
  535. frame->linesize[0] = pool->linesize[0];
  536. if (planes > AV_NUM_DATA_POINTERS) {
  537. frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
  538. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  539. frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
  540. sizeof(*frame->extended_buf));
  541. if (!frame->extended_data || !frame->extended_buf) {
  542. av_freep(&frame->extended_data);
  543. av_freep(&frame->extended_buf);
  544. return AVERROR(ENOMEM);
  545. }
  546. } else {
  547. frame->extended_data = frame->data;
  548. av_assert0(frame->nb_extended_buf == 0);
  549. }
  550. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  551. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  552. if (!frame->buf[i])
  553. goto fail;
  554. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  555. }
  556. for (i = 0; i < frame->nb_extended_buf; i++) {
  557. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  558. if (!frame->extended_buf[i])
  559. goto fail;
  560. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  561. }
  562. if (avctx->debug & FF_DEBUG_BUFFERS)
  563. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  564. return 0;
  565. fail:
  566. av_frame_unref(frame);
  567. return AVERROR(ENOMEM);
  568. }
  569. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  570. {
  571. FramePool *pool = s->internal->pool;
  572. int i;
  573. if (pic->data[0]) {
  574. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  575. return -1;
  576. }
  577. memset(pic->data, 0, sizeof(pic->data));
  578. pic->extended_data = pic->data;
  579. for (i = 0; i < 4 && pool->pools[i]; i++) {
  580. pic->linesize[i] = pool->linesize[i];
  581. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  582. if (!pic->buf[i])
  583. goto fail;
  584. pic->data[i] = pic->buf[i]->data;
  585. }
  586. for (; i < AV_NUM_DATA_POINTERS; i++) {
  587. pic->data[i] = NULL;
  588. pic->linesize[i] = 0;
  589. }
  590. if (pic->data[1] && !pic->data[2])
  591. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  592. if (s->debug & FF_DEBUG_BUFFERS)
  593. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  594. return 0;
  595. fail:
  596. av_frame_unref(pic);
  597. return AVERROR(ENOMEM);
  598. }
  599. void avpriv_color_frame(AVFrame *frame, const int c[4])
  600. {
  601. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  602. int p, y, x;
  603. av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  604. for (p = 0; p<desc->nb_components; p++) {
  605. uint8_t *dst = frame->data[p];
  606. int is_chroma = p == 1 || p == 2;
  607. int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  608. int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  609. for (y = 0; y < height; y++) {
  610. if (desc->comp[0].depth_minus1 >= 8) {
  611. for (x = 0; x<bytes; x++)
  612. ((uint16_t*)dst)[x] = c[p];
  613. }else
  614. memset(dst, c[p], bytes);
  615. dst += frame->linesize[p];
  616. }
  617. }
  618. }
  619. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  620. {
  621. int ret;
  622. if ((ret = update_frame_pool(avctx, frame)) < 0)
  623. return ret;
  624. #if FF_API_GET_BUFFER
  625. FF_DISABLE_DEPRECATION_WARNINGS
  626. frame->type = FF_BUFFER_TYPE_INTERNAL;
  627. FF_ENABLE_DEPRECATION_WARNINGS
  628. #endif
  629. switch (avctx->codec_type) {
  630. case AVMEDIA_TYPE_VIDEO:
  631. return video_get_buffer(avctx, frame);
  632. case AVMEDIA_TYPE_AUDIO:
  633. return audio_get_buffer(avctx, frame);
  634. default:
  635. return -1;
  636. }
  637. }
  638. int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
  639. {
  640. AVPacket *pkt = avctx->internal->pkt;
  641. if (pkt) {
  642. uint8_t *packet_sd;
  643. AVFrameSideData *frame_sd;
  644. int size;
  645. frame->pkt_pts = pkt->pts;
  646. av_frame_set_pkt_pos (frame, pkt->pos);
  647. av_frame_set_pkt_duration(frame, pkt->duration);
  648. av_frame_set_pkt_size (frame, pkt->size);
  649. /* copy the replaygain data to the output frame */
  650. packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_REPLAYGAIN, &size);
  651. if (packet_sd) {
  652. frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REPLAYGAIN, size);
  653. if (!frame_sd)
  654. return AVERROR(ENOMEM);
  655. memcpy(frame_sd->data, packet_sd, size);
  656. }
  657. /* copy the displaymatrix to the output frame */
  658. packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX, &size);
  659. if (packet_sd) {
  660. frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX, size);
  661. if (!frame_sd)
  662. return AVERROR(ENOMEM);
  663. memcpy(frame_sd->data, packet_sd, size);
  664. }
  665. /* copy the stereo3d format to the output frame */
  666. packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_STEREO3D, &size);
  667. if (packet_sd) {
  668. frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_STEREO3D, size);
  669. if (!frame_sd)
  670. return AVERROR(ENOMEM);
  671. memcpy(frame_sd->data, packet_sd, size);
  672. }
  673. } else {
  674. frame->pkt_pts = AV_NOPTS_VALUE;
  675. av_frame_set_pkt_pos (frame, -1);
  676. av_frame_set_pkt_duration(frame, 0);
  677. av_frame_set_pkt_size (frame, -1);
  678. }
  679. frame->reordered_opaque = avctx->reordered_opaque;
  680. if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
  681. frame->color_primaries = avctx->color_primaries;
  682. if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
  683. frame->color_trc = avctx->color_trc;
  684. if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
  685. av_frame_set_colorspace(frame, avctx->colorspace);
  686. if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
  687. av_frame_set_color_range(frame, avctx->color_range);
  688. if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
  689. frame->chroma_location = avctx->chroma_sample_location;
  690. switch (avctx->codec->type) {
  691. case AVMEDIA_TYPE_VIDEO:
  692. frame->format = avctx->pix_fmt;
  693. if (!frame->sample_aspect_ratio.num)
  694. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  695. if (frame->width && frame->height &&
  696. av_image_check_sar(frame->width, frame->height,
  697. frame->sample_aspect_ratio) < 0) {
  698. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  699. frame->sample_aspect_ratio.num,
  700. frame->sample_aspect_ratio.den);
  701. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  702. }
  703. break;
  704. case AVMEDIA_TYPE_AUDIO:
  705. if (!frame->sample_rate)
  706. frame->sample_rate = avctx->sample_rate;
  707. if (frame->format < 0)
  708. frame->format = avctx->sample_fmt;
  709. if (!frame->channel_layout) {
  710. if (avctx->channel_layout) {
  711. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  712. avctx->channels) {
  713. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  714. "configuration.\n");
  715. return AVERROR(EINVAL);
  716. }
  717. frame->channel_layout = avctx->channel_layout;
  718. } else {
  719. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  720. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  721. avctx->channels);
  722. return AVERROR(ENOSYS);
  723. }
  724. }
  725. }
  726. av_frame_set_channels(frame, avctx->channels);
  727. break;
  728. }
  729. return 0;
  730. }
  731. #if FF_API_GET_BUFFER
  732. FF_DISABLE_DEPRECATION_WARNINGS
  733. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  734. {
  735. return avcodec_default_get_buffer2(avctx, frame, 0);
  736. }
  737. typedef struct CompatReleaseBufPriv {
  738. AVCodecContext avctx;
  739. AVFrame frame;
  740. uint8_t avframe_padding[1024]; // hack to allow linking to a avutil with larger AVFrame
  741. } CompatReleaseBufPriv;
  742. static void compat_free_buffer(void *opaque, uint8_t *data)
  743. {
  744. CompatReleaseBufPriv *priv = opaque;
  745. if (priv->avctx.release_buffer)
  746. priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  747. av_freep(&priv);
  748. }
  749. static void compat_release_buffer(void *opaque, uint8_t *data)
  750. {
  751. AVBufferRef *buf = opaque;
  752. av_buffer_unref(&buf);
  753. }
  754. FF_ENABLE_DEPRECATION_WARNINGS
  755. #endif
  756. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  757. {
  758. return ff_init_buffer_info(avctx, frame);
  759. }
  760. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  761. {
  762. const AVHWAccel *hwaccel = avctx->hwaccel;
  763. int override_dimensions = 1;
  764. int ret;
  765. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  766. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  767. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  768. return AVERROR(EINVAL);
  769. }
  770. }
  771. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  772. if (frame->width <= 0 || frame->height <= 0) {
  773. frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
  774. frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
  775. override_dimensions = 0;
  776. }
  777. }
  778. ret = ff_decode_frame_props(avctx, frame);
  779. if (ret < 0)
  780. return ret;
  781. if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
  782. return ret;
  783. if (hwaccel && hwaccel->alloc_frame) {
  784. ret = hwaccel->alloc_frame(avctx, frame);
  785. goto end;
  786. }
  787. #if FF_API_GET_BUFFER
  788. FF_DISABLE_DEPRECATION_WARNINGS
  789. /*
  790. * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
  791. * We wrap each plane in its own AVBuffer. Each of those has a reference to
  792. * a dummy AVBuffer as its private data, unreffing it on free.
  793. * When all the planes are freed, the dummy buffer's free callback calls
  794. * release_buffer().
  795. */
  796. if (avctx->get_buffer) {
  797. CompatReleaseBufPriv *priv = NULL;
  798. AVBufferRef *dummy_buf = NULL;
  799. int planes, i, ret;
  800. if (flags & AV_GET_BUFFER_FLAG_REF)
  801. frame->reference = 1;
  802. ret = avctx->get_buffer(avctx, frame);
  803. if (ret < 0)
  804. return ret;
  805. /* return if the buffers are already set up
  806. * this would happen e.g. when a custom get_buffer() calls
  807. * avcodec_default_get_buffer
  808. */
  809. if (frame->buf[0])
  810. goto end0;
  811. priv = av_mallocz(sizeof(*priv));
  812. if (!priv) {
  813. ret = AVERROR(ENOMEM);
  814. goto fail;
  815. }
  816. priv->avctx = *avctx;
  817. priv->frame = *frame;
  818. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  819. if (!dummy_buf) {
  820. ret = AVERROR(ENOMEM);
  821. goto fail;
  822. }
  823. #define WRAP_PLANE(ref_out, data, data_size) \
  824. do { \
  825. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  826. if (!dummy_ref) { \
  827. ret = AVERROR(ENOMEM); \
  828. goto fail; \
  829. } \
  830. ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
  831. dummy_ref, 0); \
  832. if (!ref_out) { \
  833. av_frame_unref(frame); \
  834. ret = AVERROR(ENOMEM); \
  835. goto fail; \
  836. } \
  837. } while (0)
  838. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  839. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  840. planes = av_pix_fmt_count_planes(frame->format);
  841. /* workaround for AVHWAccel plane count of 0, buf[0] is used as
  842. check for allocated buffers: make libavcodec happy */
  843. if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  844. planes = 1;
  845. if (!desc || planes <= 0) {
  846. ret = AVERROR(EINVAL);
  847. goto fail;
  848. }
  849. for (i = 0; i < planes; i++) {
  850. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  851. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  852. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  853. }
  854. } else {
  855. int planar = av_sample_fmt_is_planar(frame->format);
  856. planes = planar ? avctx->channels : 1;
  857. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  858. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  859. frame->extended_buf = av_malloc_array(sizeof(*frame->extended_buf),
  860. frame->nb_extended_buf);
  861. if (!frame->extended_buf) {
  862. ret = AVERROR(ENOMEM);
  863. goto fail;
  864. }
  865. }
  866. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  867. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  868. for (i = 0; i < frame->nb_extended_buf; i++)
  869. WRAP_PLANE(frame->extended_buf[i],
  870. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  871. frame->linesize[0]);
  872. }
  873. av_buffer_unref(&dummy_buf);
  874. end0:
  875. frame->width = avctx->width;
  876. frame->height = avctx->height;
  877. return 0;
  878. fail:
  879. avctx->release_buffer(avctx, frame);
  880. av_freep(&priv);
  881. av_buffer_unref(&dummy_buf);
  882. return ret;
  883. }
  884. FF_ENABLE_DEPRECATION_WARNINGS
  885. #endif
  886. ret = avctx->get_buffer2(avctx, frame, flags);
  887. end:
  888. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
  889. frame->width = avctx->width;
  890. frame->height = avctx->height;
  891. }
  892. return ret;
  893. }
  894. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  895. {
  896. int ret = get_buffer_internal(avctx, frame, flags);
  897. if (ret < 0)
  898. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  899. return ret;
  900. }
  901. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  902. {
  903. AVFrame *tmp;
  904. int ret;
  905. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  906. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  907. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  908. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  909. av_frame_unref(frame);
  910. }
  911. ff_init_buffer_info(avctx, frame);
  912. if (!frame->data[0])
  913. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  914. if (av_frame_is_writable(frame))
  915. return ff_decode_frame_props(avctx, frame);
  916. tmp = av_frame_alloc();
  917. if (!tmp)
  918. return AVERROR(ENOMEM);
  919. av_frame_move_ref(tmp, frame);
  920. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  921. if (ret < 0) {
  922. av_frame_free(&tmp);
  923. return ret;
  924. }
  925. av_frame_copy(frame, tmp);
  926. av_frame_free(&tmp);
  927. return 0;
  928. }
  929. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  930. {
  931. int ret = reget_buffer_internal(avctx, frame);
  932. if (ret < 0)
  933. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  934. return ret;
  935. }
  936. #if FF_API_GET_BUFFER
  937. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  938. {
  939. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  940. av_frame_unref(pic);
  941. }
  942. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  943. {
  944. av_assert0(0);
  945. return AVERROR_BUG;
  946. }
  947. #endif
  948. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  949. {
  950. int i;
  951. for (i = 0; i < count; i++) {
  952. int r = func(c, (char *)arg + i * size);
  953. if (ret)
  954. ret[i] = r;
  955. }
  956. return 0;
  957. }
  958. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  959. {
  960. int i;
  961. for (i = 0; i < count; i++) {
  962. int r = func(c, arg, i, 0);
  963. if (ret)
  964. ret[i] = r;
  965. }
  966. return 0;
  967. }
  968. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  969. unsigned int fourcc)
  970. {
  971. while (tags->pix_fmt >= 0) {
  972. if (tags->fourcc == fourcc)
  973. return tags->pix_fmt;
  974. tags++;
  975. }
  976. return AV_PIX_FMT_NONE;
  977. }
  978. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  979. {
  980. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  981. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  982. }
  983. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  984. {
  985. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  986. ++fmt;
  987. return fmt[0];
  988. }
  989. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  990. enum AVPixelFormat pix_fmt)
  991. {
  992. AVHWAccel *hwaccel = NULL;
  993. while ((hwaccel = av_hwaccel_next(hwaccel)))
  994. if (hwaccel->id == codec_id
  995. && hwaccel->pix_fmt == pix_fmt)
  996. return hwaccel;
  997. return NULL;
  998. }
  999. static int setup_hwaccel(AVCodecContext *avctx,
  1000. const enum AVPixelFormat fmt,
  1001. const char *name)
  1002. {
  1003. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  1004. int ret = 0;
  1005. if (!hwa) {
  1006. av_log(avctx, AV_LOG_ERROR,
  1007. "Could not find an AVHWAccel for the pixel format: %s",
  1008. name);
  1009. return AVERROR(ENOENT);
  1010. }
  1011. if (hwa->priv_data_size) {
  1012. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  1013. if (!avctx->internal->hwaccel_priv_data)
  1014. return AVERROR(ENOMEM);
  1015. }
  1016. if (hwa->init) {
  1017. ret = hwa->init(avctx);
  1018. if (ret < 0) {
  1019. av_freep(&avctx->internal->hwaccel_priv_data);
  1020. return ret;
  1021. }
  1022. }
  1023. avctx->hwaccel = hwa;
  1024. return 0;
  1025. }
  1026. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  1027. {
  1028. const AVPixFmtDescriptor *desc;
  1029. enum AVPixelFormat *choices;
  1030. enum AVPixelFormat ret;
  1031. unsigned n = 0;
  1032. while (fmt[n] != AV_PIX_FMT_NONE)
  1033. ++n;
  1034. choices = av_malloc_array(n + 1, sizeof(*choices));
  1035. if (!choices)
  1036. return AV_PIX_FMT_NONE;
  1037. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  1038. for (;;) {
  1039. ret = avctx->get_format(avctx, choices);
  1040. desc = av_pix_fmt_desc_get(ret);
  1041. if (!desc) {
  1042. ret = AV_PIX_FMT_NONE;
  1043. break;
  1044. }
  1045. if (avctx->hwaccel && avctx->hwaccel->uninit)
  1046. avctx->hwaccel->uninit(avctx);
  1047. av_freep(&avctx->internal->hwaccel_priv_data);
  1048. avctx->hwaccel = NULL;
  1049. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  1050. break;
  1051. if (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1052. break;
  1053. if (!setup_hwaccel(avctx, ret, desc->name))
  1054. break;
  1055. /* Remove failed hwaccel from choices */
  1056. for (n = 0; choices[n] != ret; n++)
  1057. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  1058. do
  1059. choices[n] = choices[n + 1];
  1060. while (choices[n] != AV_PIX_FMT_NONE);
  1061. }
  1062. av_freep(&choices);
  1063. return ret;
  1064. }
  1065. #if FF_API_AVFRAME_LAVC
  1066. void avcodec_get_frame_defaults(AVFrame *frame)
  1067. {
  1068. #if LIBAVCODEC_VERSION_MAJOR >= 55
  1069. // extended_data should explicitly be freed when needed, this code is unsafe currently
  1070. // also this is not compatible to the <55 ABI/API
  1071. if (frame->extended_data != frame->data && 0)
  1072. av_freep(&frame->extended_data);
  1073. #endif
  1074. memset(frame, 0, sizeof(AVFrame));
  1075. av_frame_unref(frame);
  1076. }
  1077. AVFrame *avcodec_alloc_frame(void)
  1078. {
  1079. return av_frame_alloc();
  1080. }
  1081. void avcodec_free_frame(AVFrame **frame)
  1082. {
  1083. av_frame_free(frame);
  1084. }
  1085. #endif
  1086. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  1087. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  1088. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  1089. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  1090. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  1091. int av_codec_get_max_lowres(const AVCodec *codec)
  1092. {
  1093. return codec->max_lowres;
  1094. }
  1095. static void get_subtitle_defaults(AVSubtitle *sub)
  1096. {
  1097. memset(sub, 0, sizeof(*sub));
  1098. sub->pts = AV_NOPTS_VALUE;
  1099. }
  1100. static int get_bit_rate(AVCodecContext *ctx)
  1101. {
  1102. int bit_rate;
  1103. int bits_per_sample;
  1104. switch (ctx->codec_type) {
  1105. case AVMEDIA_TYPE_VIDEO:
  1106. case AVMEDIA_TYPE_DATA:
  1107. case AVMEDIA_TYPE_SUBTITLE:
  1108. case AVMEDIA_TYPE_ATTACHMENT:
  1109. bit_rate = ctx->bit_rate;
  1110. break;
  1111. case AVMEDIA_TYPE_AUDIO:
  1112. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1113. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1114. break;
  1115. default:
  1116. bit_rate = 0;
  1117. break;
  1118. }
  1119. return bit_rate;
  1120. }
  1121. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1122. {
  1123. int ret = 0;
  1124. ff_unlock_avcodec();
  1125. ret = avcodec_open2(avctx, codec, options);
  1126. ff_lock_avcodec(avctx);
  1127. return ret;
  1128. }
  1129. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1130. {
  1131. int ret = 0;
  1132. AVDictionary *tmp = NULL;
  1133. if (avcodec_is_open(avctx))
  1134. return 0;
  1135. if ((!codec && !avctx->codec)) {
  1136. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  1137. return AVERROR(EINVAL);
  1138. }
  1139. if ((codec && avctx->codec && codec != avctx->codec)) {
  1140. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  1141. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  1142. return AVERROR(EINVAL);
  1143. }
  1144. if (!codec)
  1145. codec = avctx->codec;
  1146. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  1147. return AVERROR(EINVAL);
  1148. if (options)
  1149. av_dict_copy(&tmp, *options, 0);
  1150. ret = ff_lock_avcodec(avctx);
  1151. if (ret < 0)
  1152. return ret;
  1153. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  1154. if (!avctx->internal) {
  1155. ret = AVERROR(ENOMEM);
  1156. goto end;
  1157. }
  1158. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  1159. if (!avctx->internal->pool) {
  1160. ret = AVERROR(ENOMEM);
  1161. goto free_and_end;
  1162. }
  1163. avctx->internal->to_free = av_frame_alloc();
  1164. if (!avctx->internal->to_free) {
  1165. ret = AVERROR(ENOMEM);
  1166. goto free_and_end;
  1167. }
  1168. if (codec->priv_data_size > 0) {
  1169. if (!avctx->priv_data) {
  1170. avctx->priv_data = av_mallocz(codec->priv_data_size);
  1171. if (!avctx->priv_data) {
  1172. ret = AVERROR(ENOMEM);
  1173. goto end;
  1174. }
  1175. if (codec->priv_class) {
  1176. *(const AVClass **)avctx->priv_data = codec->priv_class;
  1177. av_opt_set_defaults(avctx->priv_data);
  1178. }
  1179. }
  1180. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  1181. goto free_and_end;
  1182. } else {
  1183. avctx->priv_data = NULL;
  1184. }
  1185. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  1186. goto free_and_end;
  1187. // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
  1188. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  1189. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
  1190. if (avctx->coded_width && avctx->coded_height)
  1191. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  1192. else if (avctx->width && avctx->height)
  1193. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1194. if (ret < 0)
  1195. goto free_and_end;
  1196. }
  1197. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  1198. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  1199. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  1200. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  1201. ff_set_dimensions(avctx, 0, 0);
  1202. }
  1203. if (avctx->width > 0 && avctx->height > 0) {
  1204. if (av_image_check_sar(avctx->width, avctx->height,
  1205. avctx->sample_aspect_ratio) < 0) {
  1206. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1207. avctx->sample_aspect_ratio.num,
  1208. avctx->sample_aspect_ratio.den);
  1209. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  1210. }
  1211. }
  1212. /* if the decoder init function was already called previously,
  1213. * free the already allocated subtitle_header before overwriting it */
  1214. if (av_codec_is_decoder(codec))
  1215. av_freep(&avctx->subtitle_header);
  1216. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1217. ret = AVERROR(EINVAL);
  1218. goto free_and_end;
  1219. }
  1220. avctx->codec = codec;
  1221. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  1222. avctx->codec_id == AV_CODEC_ID_NONE) {
  1223. avctx->codec_type = codec->type;
  1224. avctx->codec_id = codec->id;
  1225. }
  1226. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  1227. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  1228. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  1229. ret = AVERROR(EINVAL);
  1230. goto free_and_end;
  1231. }
  1232. avctx->frame_number = 0;
  1233. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  1234. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  1235. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1236. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  1237. AVCodec *codec2;
  1238. av_log(avctx, AV_LOG_ERROR,
  1239. "The %s '%s' is experimental but experimental codecs are not enabled, "
  1240. "add '-strict %d' if you want to use it.\n",
  1241. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  1242. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  1243. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  1244. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  1245. codec_string, codec2->name);
  1246. ret = AVERROR_EXPERIMENTAL;
  1247. goto free_and_end;
  1248. }
  1249. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  1250. (!avctx->time_base.num || !avctx->time_base.den)) {
  1251. avctx->time_base.num = 1;
  1252. avctx->time_base.den = avctx->sample_rate;
  1253. }
  1254. if (!HAVE_THREADS)
  1255. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1256. if (CONFIG_FRAME_THREAD_ENCODER) {
  1257. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  1258. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1259. ff_lock_avcodec(avctx);
  1260. if (ret < 0)
  1261. goto free_and_end;
  1262. }
  1263. if (HAVE_THREADS
  1264. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1265. ret = ff_thread_init(avctx);
  1266. if (ret < 0) {
  1267. goto free_and_end;
  1268. }
  1269. }
  1270. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  1271. avctx->thread_count = 1;
  1272. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1273. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1274. avctx->codec->max_lowres);
  1275. ret = AVERROR(EINVAL);
  1276. goto free_and_end;
  1277. }
  1278. #if FF_API_VISMV
  1279. if (avctx->debug_mv)
  1280. av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
  1281. "see the codecview filter instead.\n");
  1282. #endif
  1283. if (av_codec_is_encoder(avctx->codec)) {
  1284. int i;
  1285. if (avctx->codec->sample_fmts) {
  1286. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1287. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1288. break;
  1289. if (avctx->channels == 1 &&
  1290. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1291. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1292. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1293. break;
  1294. }
  1295. }
  1296. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1297. char buf[128];
  1298. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1299. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1300. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1301. ret = AVERROR(EINVAL);
  1302. goto free_and_end;
  1303. }
  1304. }
  1305. if (avctx->codec->pix_fmts) {
  1306. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1307. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1308. break;
  1309. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1310. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1311. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1312. char buf[128];
  1313. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1314. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1315. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1316. ret = AVERROR(EINVAL);
  1317. goto free_and_end;
  1318. }
  1319. }
  1320. if (avctx->codec->supported_samplerates) {
  1321. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1322. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1323. break;
  1324. if (avctx->codec->supported_samplerates[i] == 0) {
  1325. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1326. avctx->sample_rate);
  1327. ret = AVERROR(EINVAL);
  1328. goto free_and_end;
  1329. }
  1330. }
  1331. if (avctx->codec->channel_layouts) {
  1332. if (!avctx->channel_layout) {
  1333. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1334. } else {
  1335. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1336. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1337. break;
  1338. if (avctx->codec->channel_layouts[i] == 0) {
  1339. char buf[512];
  1340. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1341. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1342. ret = AVERROR(EINVAL);
  1343. goto free_and_end;
  1344. }
  1345. }
  1346. }
  1347. if (avctx->channel_layout && avctx->channels) {
  1348. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1349. if (channels != avctx->channels) {
  1350. char buf[512];
  1351. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1352. av_log(avctx, AV_LOG_ERROR,
  1353. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1354. buf, channels, avctx->channels);
  1355. ret = AVERROR(EINVAL);
  1356. goto free_and_end;
  1357. }
  1358. } else if (avctx->channel_layout) {
  1359. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1360. }
  1361. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1362. if (avctx->width <= 0 || avctx->height <= 0) {
  1363. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1364. ret = AVERROR(EINVAL);
  1365. goto free_and_end;
  1366. }
  1367. }
  1368. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1369. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1370. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  1371. }
  1372. if (!avctx->rc_initial_buffer_occupancy)
  1373. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1374. }
  1375. avctx->pts_correction_num_faulty_pts =
  1376. avctx->pts_correction_num_faulty_dts = 0;
  1377. avctx->pts_correction_last_pts =
  1378. avctx->pts_correction_last_dts = INT64_MIN;
  1379. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1380. || avctx->internal->frame_thread_encoder)) {
  1381. ret = avctx->codec->init(avctx);
  1382. if (ret < 0) {
  1383. goto free_and_end;
  1384. }
  1385. }
  1386. ret=0;
  1387. if (av_codec_is_decoder(avctx->codec)) {
  1388. if (!avctx->bit_rate)
  1389. avctx->bit_rate = get_bit_rate(avctx);
  1390. /* validate channel layout from the decoder */
  1391. if (avctx->channel_layout) {
  1392. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1393. if (!avctx->channels)
  1394. avctx->channels = channels;
  1395. else if (channels != avctx->channels) {
  1396. char buf[512];
  1397. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1398. av_log(avctx, AV_LOG_WARNING,
  1399. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1400. "ignoring specified channel layout\n",
  1401. buf, channels, avctx->channels);
  1402. avctx->channel_layout = 0;
  1403. }
  1404. }
  1405. if (avctx->channels && avctx->channels < 0 ||
  1406. avctx->channels > FF_SANE_NB_CHANNELS) {
  1407. ret = AVERROR(EINVAL);
  1408. goto free_and_end;
  1409. }
  1410. if (avctx->sub_charenc) {
  1411. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1412. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1413. "supported with subtitles codecs\n");
  1414. ret = AVERROR(EINVAL);
  1415. goto free_and_end;
  1416. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1417. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1418. "subtitles character encoding will be ignored\n",
  1419. avctx->codec_descriptor->name);
  1420. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1421. } else {
  1422. /* input character encoding is set for a text based subtitle
  1423. * codec at this point */
  1424. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1425. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1426. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1427. #if CONFIG_ICONV
  1428. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1429. if (cd == (iconv_t)-1) {
  1430. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1431. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1432. ret = AVERROR(errno);
  1433. goto free_and_end;
  1434. }
  1435. iconv_close(cd);
  1436. #else
  1437. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1438. "conversion needs a libavcodec built with iconv support "
  1439. "for this codec\n");
  1440. ret = AVERROR(ENOSYS);
  1441. goto free_and_end;
  1442. #endif
  1443. }
  1444. }
  1445. }
  1446. }
  1447. end:
  1448. ff_unlock_avcodec();
  1449. if (options) {
  1450. av_dict_free(options);
  1451. *options = tmp;
  1452. }
  1453. return ret;
  1454. free_and_end:
  1455. av_dict_free(&tmp);
  1456. av_freep(&avctx->priv_data);
  1457. if (avctx->internal) {
  1458. av_frame_free(&avctx->internal->to_free);
  1459. av_freep(&avctx->internal->pool);
  1460. }
  1461. av_freep(&avctx->internal);
  1462. avctx->codec = NULL;
  1463. goto end;
  1464. }
  1465. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
  1466. {
  1467. if (avpkt->size < 0) {
  1468. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1469. return AVERROR(EINVAL);
  1470. }
  1471. if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1472. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1473. size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  1474. return AVERROR(EINVAL);
  1475. }
  1476. if (avctx) {
  1477. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1478. if (!avpkt->data || avpkt->size < size) {
  1479. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1480. avpkt->data = avctx->internal->byte_buffer;
  1481. avpkt->size = avctx->internal->byte_buffer_size;
  1482. #if FF_API_DESTRUCT_PACKET
  1483. FF_DISABLE_DEPRECATION_WARNINGS
  1484. avpkt->destruct = NULL;
  1485. FF_ENABLE_DEPRECATION_WARNINGS
  1486. #endif
  1487. }
  1488. }
  1489. if (avpkt->data) {
  1490. AVBufferRef *buf = avpkt->buf;
  1491. #if FF_API_DESTRUCT_PACKET
  1492. FF_DISABLE_DEPRECATION_WARNINGS
  1493. void *destruct = avpkt->destruct;
  1494. FF_ENABLE_DEPRECATION_WARNINGS
  1495. #endif
  1496. if (avpkt->size < size) {
  1497. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1498. return AVERROR(EINVAL);
  1499. }
  1500. av_init_packet(avpkt);
  1501. #if FF_API_DESTRUCT_PACKET
  1502. FF_DISABLE_DEPRECATION_WARNINGS
  1503. avpkt->destruct = destruct;
  1504. FF_ENABLE_DEPRECATION_WARNINGS
  1505. #endif
  1506. avpkt->buf = buf;
  1507. avpkt->size = size;
  1508. return 0;
  1509. } else {
  1510. int ret = av_new_packet(avpkt, size);
  1511. if (ret < 0)
  1512. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1513. return ret;
  1514. }
  1515. }
  1516. int ff_alloc_packet(AVPacket *avpkt, int size)
  1517. {
  1518. return ff_alloc_packet2(NULL, avpkt, size);
  1519. }
  1520. /**
  1521. * Pad last frame with silence.
  1522. */
  1523. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1524. {
  1525. AVFrame *frame = NULL;
  1526. int ret;
  1527. if (!(frame = av_frame_alloc()))
  1528. return AVERROR(ENOMEM);
  1529. frame->format = src->format;
  1530. frame->channel_layout = src->channel_layout;
  1531. av_frame_set_channels(frame, av_frame_get_channels(src));
  1532. frame->nb_samples = s->frame_size;
  1533. ret = av_frame_get_buffer(frame, 32);
  1534. if (ret < 0)
  1535. goto fail;
  1536. ret = av_frame_copy_props(frame, src);
  1537. if (ret < 0)
  1538. goto fail;
  1539. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1540. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1541. goto fail;
  1542. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1543. frame->nb_samples - src->nb_samples,
  1544. s->channels, s->sample_fmt)) < 0)
  1545. goto fail;
  1546. *dst = frame;
  1547. return 0;
  1548. fail:
  1549. av_frame_free(&frame);
  1550. return ret;
  1551. }
  1552. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1553. AVPacket *avpkt,
  1554. const AVFrame *frame,
  1555. int *got_packet_ptr)
  1556. {
  1557. AVFrame *extended_frame = NULL;
  1558. AVFrame *padded_frame = NULL;
  1559. int ret;
  1560. AVPacket user_pkt = *avpkt;
  1561. int needs_realloc = !user_pkt.data;
  1562. *got_packet_ptr = 0;
  1563. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1564. av_free_packet(avpkt);
  1565. av_init_packet(avpkt);
  1566. return 0;
  1567. }
  1568. /* ensure that extended_data is properly set */
  1569. if (frame && !frame->extended_data) {
  1570. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1571. avctx->channels > AV_NUM_DATA_POINTERS) {
  1572. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1573. "with more than %d channels, but extended_data is not set.\n",
  1574. AV_NUM_DATA_POINTERS);
  1575. return AVERROR(EINVAL);
  1576. }
  1577. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1578. extended_frame = av_frame_alloc();
  1579. if (!extended_frame)
  1580. return AVERROR(ENOMEM);
  1581. memcpy(extended_frame, frame, sizeof(AVFrame));
  1582. extended_frame->extended_data = extended_frame->data;
  1583. frame = extended_frame;
  1584. }
  1585. /* check for valid frame size */
  1586. if (frame) {
  1587. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1588. if (frame->nb_samples > avctx->frame_size) {
  1589. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1590. ret = AVERROR(EINVAL);
  1591. goto end;
  1592. }
  1593. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1594. if (frame->nb_samples < avctx->frame_size &&
  1595. !avctx->internal->last_audio_frame) {
  1596. ret = pad_last_frame(avctx, &padded_frame, frame);
  1597. if (ret < 0)
  1598. goto end;
  1599. frame = padded_frame;
  1600. avctx->internal->last_audio_frame = 1;
  1601. }
  1602. if (frame->nb_samples != avctx->frame_size) {
  1603. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1604. ret = AVERROR(EINVAL);
  1605. goto end;
  1606. }
  1607. }
  1608. }
  1609. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1610. if (!ret) {
  1611. if (*got_packet_ptr) {
  1612. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1613. if (avpkt->pts == AV_NOPTS_VALUE)
  1614. avpkt->pts = frame->pts;
  1615. if (!avpkt->duration)
  1616. avpkt->duration = ff_samples_to_time_base(avctx,
  1617. frame->nb_samples);
  1618. }
  1619. avpkt->dts = avpkt->pts;
  1620. } else {
  1621. avpkt->size = 0;
  1622. }
  1623. }
  1624. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1625. needs_realloc = 0;
  1626. if (user_pkt.data) {
  1627. if (user_pkt.size >= avpkt->size) {
  1628. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1629. } else {
  1630. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1631. avpkt->size = user_pkt.size;
  1632. ret = -1;
  1633. }
  1634. avpkt->buf = user_pkt.buf;
  1635. avpkt->data = user_pkt.data;
  1636. #if FF_API_DESTRUCT_PACKET
  1637. FF_DISABLE_DEPRECATION_WARNINGS
  1638. avpkt->destruct = user_pkt.destruct;
  1639. FF_ENABLE_DEPRECATION_WARNINGS
  1640. #endif
  1641. } else {
  1642. if (av_dup_packet(avpkt) < 0) {
  1643. ret = AVERROR(ENOMEM);
  1644. }
  1645. }
  1646. }
  1647. if (!ret) {
  1648. if (needs_realloc && avpkt->data) {
  1649. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1650. if (ret >= 0)
  1651. avpkt->data = avpkt->buf->data;
  1652. }
  1653. avctx->frame_number++;
  1654. }
  1655. if (ret < 0 || !*got_packet_ptr) {
  1656. av_free_packet(avpkt);
  1657. av_init_packet(avpkt);
  1658. goto end;
  1659. }
  1660. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1661. * this needs to be moved to the encoders, but for now we can do it
  1662. * here to simplify things */
  1663. avpkt->flags |= AV_PKT_FLAG_KEY;
  1664. end:
  1665. av_frame_free(&padded_frame);
  1666. av_free(extended_frame);
  1667. return ret;
  1668. }
  1669. #if FF_API_OLD_ENCODE_AUDIO
  1670. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1671. uint8_t *buf, int buf_size,
  1672. const short *samples)
  1673. {
  1674. AVPacket pkt;
  1675. AVFrame *frame;
  1676. int ret, samples_size, got_packet;
  1677. av_init_packet(&pkt);
  1678. pkt.data = buf;
  1679. pkt.size = buf_size;
  1680. if (samples) {
  1681. frame = av_frame_alloc();
  1682. if (!frame)
  1683. return AVERROR(ENOMEM);
  1684. if (avctx->frame_size) {
  1685. frame->nb_samples = avctx->frame_size;
  1686. } else {
  1687. /* if frame_size is not set, the number of samples must be
  1688. * calculated from the buffer size */
  1689. int64_t nb_samples;
  1690. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1691. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1692. "support this codec\n");
  1693. av_frame_free(&frame);
  1694. return AVERROR(EINVAL);
  1695. }
  1696. nb_samples = (int64_t)buf_size * 8 /
  1697. (av_get_bits_per_sample(avctx->codec_id) *
  1698. avctx->channels);
  1699. if (nb_samples >= INT_MAX) {
  1700. av_frame_free(&frame);
  1701. return AVERROR(EINVAL);
  1702. }
  1703. frame->nb_samples = nb_samples;
  1704. }
  1705. /* it is assumed that the samples buffer is large enough based on the
  1706. * relevant parameters */
  1707. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1708. frame->nb_samples,
  1709. avctx->sample_fmt, 1);
  1710. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1711. avctx->sample_fmt,
  1712. (const uint8_t *)samples,
  1713. samples_size, 1)) < 0) {
  1714. av_frame_free(&frame);
  1715. return ret;
  1716. }
  1717. /* fabricate frame pts from sample count.
  1718. * this is needed because the avcodec_encode_audio() API does not have
  1719. * a way for the user to provide pts */
  1720. if (avctx->sample_rate && avctx->time_base.num)
  1721. frame->pts = ff_samples_to_time_base(avctx,
  1722. avctx->internal->sample_count);
  1723. else
  1724. frame->pts = AV_NOPTS_VALUE;
  1725. avctx->internal->sample_count += frame->nb_samples;
  1726. } else {
  1727. frame = NULL;
  1728. }
  1729. got_packet = 0;
  1730. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1731. if (!ret && got_packet && avctx->coded_frame) {
  1732. avctx->coded_frame->pts = pkt.pts;
  1733. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1734. }
  1735. /* free any side data since we cannot return it */
  1736. av_packet_free_side_data(&pkt);
  1737. if (frame && frame->extended_data != frame->data)
  1738. av_freep(&frame->extended_data);
  1739. av_frame_free(&frame);
  1740. return ret ? ret : pkt.size;
  1741. }
  1742. #endif
  1743. #if FF_API_OLD_ENCODE_VIDEO
  1744. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1745. const AVFrame *pict)
  1746. {
  1747. AVPacket pkt;
  1748. int ret, got_packet = 0;
  1749. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1750. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1751. return -1;
  1752. }
  1753. av_init_packet(&pkt);
  1754. pkt.data = buf;
  1755. pkt.size = buf_size;
  1756. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1757. if (!ret && got_packet && avctx->coded_frame) {
  1758. avctx->coded_frame->pts = pkt.pts;
  1759. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1760. }
  1761. /* free any side data since we cannot return it */
  1762. if (pkt.side_data_elems > 0) {
  1763. int i;
  1764. for (i = 0; i < pkt.side_data_elems; i++)
  1765. av_free(pkt.side_data[i].data);
  1766. av_freep(&pkt.side_data);
  1767. pkt.side_data_elems = 0;
  1768. }
  1769. return ret ? ret : pkt.size;
  1770. }
  1771. #endif
  1772. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1773. AVPacket *avpkt,
  1774. const AVFrame *frame,
  1775. int *got_packet_ptr)
  1776. {
  1777. int ret;
  1778. AVPacket user_pkt = *avpkt;
  1779. int needs_realloc = !user_pkt.data;
  1780. *got_packet_ptr = 0;
  1781. if(CONFIG_FRAME_THREAD_ENCODER &&
  1782. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1783. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1784. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1785. avctx->stats_out[0] = '\0';
  1786. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1787. av_free_packet(avpkt);
  1788. av_init_packet(avpkt);
  1789. avpkt->size = 0;
  1790. return 0;
  1791. }
  1792. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1793. return AVERROR(EINVAL);
  1794. av_assert0(avctx->codec->encode2);
  1795. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1796. av_assert0(ret <= 0);
  1797. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1798. needs_realloc = 0;
  1799. if (user_pkt.data) {
  1800. if (user_pkt.size >= avpkt->size) {
  1801. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1802. } else {
  1803. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1804. avpkt->size = user_pkt.size;
  1805. ret = -1;
  1806. }
  1807. avpkt->buf = user_pkt.buf;
  1808. avpkt->data = user_pkt.data;
  1809. #if FF_API_DESTRUCT_PACKET
  1810. FF_DISABLE_DEPRECATION_WARNINGS
  1811. avpkt->destruct = user_pkt.destruct;
  1812. FF_ENABLE_DEPRECATION_WARNINGS
  1813. #endif
  1814. } else {
  1815. if (av_dup_packet(avpkt) < 0) {
  1816. ret = AVERROR(ENOMEM);
  1817. }
  1818. }
  1819. }
  1820. if (!ret) {
  1821. if (!*got_packet_ptr)
  1822. avpkt->size = 0;
  1823. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1824. avpkt->pts = avpkt->dts = frame->pts;
  1825. if (needs_realloc && avpkt->data) {
  1826. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1827. if (ret >= 0)
  1828. avpkt->data = avpkt->buf->data;
  1829. }
  1830. avctx->frame_number++;
  1831. }
  1832. if (ret < 0 || !*got_packet_ptr)
  1833. av_free_packet(avpkt);
  1834. else
  1835. av_packet_merge_side_data(avpkt);
  1836. emms_c();
  1837. return ret;
  1838. }
  1839. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1840. const AVSubtitle *sub)
  1841. {
  1842. int ret;
  1843. if (sub->start_display_time) {
  1844. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1845. return -1;
  1846. }
  1847. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1848. avctx->frame_number++;
  1849. return ret;
  1850. }
  1851. /**
  1852. * Attempt to guess proper monotonic timestamps for decoded video frames
  1853. * which might have incorrect times. Input timestamps may wrap around, in
  1854. * which case the output will as well.
  1855. *
  1856. * @param pts the pts field of the decoded AVPacket, as passed through
  1857. * AVFrame.pkt_pts
  1858. * @param dts the dts field of the decoded AVPacket
  1859. * @return one of the input values, may be AV_NOPTS_VALUE
  1860. */
  1861. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1862. int64_t reordered_pts, int64_t dts)
  1863. {
  1864. int64_t pts = AV_NOPTS_VALUE;
  1865. if (dts != AV_NOPTS_VALUE) {
  1866. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1867. ctx->pts_correction_last_dts = dts;
  1868. } else if (reordered_pts != AV_NOPTS_VALUE)
  1869. ctx->pts_correction_last_dts = reordered_pts;
  1870. if (reordered_pts != AV_NOPTS_VALUE) {
  1871. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1872. ctx->pts_correction_last_pts = reordered_pts;
  1873. } else if(dts != AV_NOPTS_VALUE)
  1874. ctx->pts_correction_last_pts = dts;
  1875. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1876. && reordered_pts != AV_NOPTS_VALUE)
  1877. pts = reordered_pts;
  1878. else
  1879. pts = dts;
  1880. return pts;
  1881. }
  1882. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1883. {
  1884. int size = 0, ret;
  1885. const uint8_t *data;
  1886. uint32_t flags;
  1887. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1888. if (!data)
  1889. return 0;
  1890. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
  1891. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1892. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1893. return AVERROR(EINVAL);
  1894. }
  1895. if (size < 4)
  1896. goto fail;
  1897. flags = bytestream_get_le32(&data);
  1898. size -= 4;
  1899. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1900. if (size < 4)
  1901. goto fail;
  1902. avctx->channels = bytestream_get_le32(&data);
  1903. size -= 4;
  1904. }
  1905. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1906. if (size < 8)
  1907. goto fail;
  1908. avctx->channel_layout = bytestream_get_le64(&data);
  1909. size -= 8;
  1910. }
  1911. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1912. if (size < 4)
  1913. goto fail;
  1914. avctx->sample_rate = bytestream_get_le32(&data);
  1915. size -= 4;
  1916. }
  1917. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1918. if (size < 8)
  1919. goto fail;
  1920. avctx->width = bytestream_get_le32(&data);
  1921. avctx->height = bytestream_get_le32(&data);
  1922. size -= 8;
  1923. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1924. if (ret < 0)
  1925. return ret;
  1926. }
  1927. return 0;
  1928. fail:
  1929. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1930. return AVERROR_INVALIDDATA;
  1931. }
  1932. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1933. {
  1934. int size;
  1935. const uint8_t *side_metadata;
  1936. AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
  1937. side_metadata = av_packet_get_side_data(avctx->internal->pkt,
  1938. AV_PKT_DATA_STRINGS_METADATA, &size);
  1939. return av_packet_unpack_dictionary(side_metadata, size, frame_md);
  1940. }
  1941. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1942. {
  1943. int ret;
  1944. /* move the original frame to our backup */
  1945. av_frame_unref(avci->to_free);
  1946. av_frame_move_ref(avci->to_free, frame);
  1947. /* now copy everything except the AVBufferRefs back
  1948. * note that we make a COPY of the side data, so calling av_frame_free() on
  1949. * the caller's frame will work properly */
  1950. ret = av_frame_copy_props(frame, avci->to_free);
  1951. if (ret < 0)
  1952. return ret;
  1953. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1954. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1955. if (avci->to_free->extended_data != avci->to_free->data) {
  1956. int planes = av_frame_get_channels(avci->to_free);
  1957. int size = planes * sizeof(*frame->extended_data);
  1958. if (!size) {
  1959. av_frame_unref(frame);
  1960. return AVERROR_BUG;
  1961. }
  1962. frame->extended_data = av_malloc(size);
  1963. if (!frame->extended_data) {
  1964. av_frame_unref(frame);
  1965. return AVERROR(ENOMEM);
  1966. }
  1967. memcpy(frame->extended_data, avci->to_free->extended_data,
  1968. size);
  1969. } else
  1970. frame->extended_data = frame->data;
  1971. frame->format = avci->to_free->format;
  1972. frame->width = avci->to_free->width;
  1973. frame->height = avci->to_free->height;
  1974. frame->channel_layout = avci->to_free->channel_layout;
  1975. frame->nb_samples = avci->to_free->nb_samples;
  1976. av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
  1977. return 0;
  1978. }
  1979. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1980. int *got_picture_ptr,
  1981. const AVPacket *avpkt)
  1982. {
  1983. AVCodecInternal *avci = avctx->internal;
  1984. int ret;
  1985. // copy to ensure we do not change avpkt
  1986. AVPacket tmp = *avpkt;
  1987. if (!avctx->codec)
  1988. return AVERROR(EINVAL);
  1989. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1990. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1991. return AVERROR(EINVAL);
  1992. }
  1993. *got_picture_ptr = 0;
  1994. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1995. return AVERROR(EINVAL);
  1996. av_frame_unref(picture);
  1997. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1998. int did_split = av_packet_split_side_data(&tmp);
  1999. ret = apply_param_change(avctx, &tmp);
  2000. if (ret < 0) {
  2001. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  2002. if (avctx->err_recognition & AV_EF_EXPLODE)
  2003. goto fail;
  2004. }
  2005. avctx->internal->pkt = &tmp;
  2006. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2007. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  2008. &tmp);
  2009. else {
  2010. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  2011. &tmp);
  2012. picture->pkt_dts = avpkt->dts;
  2013. if(!avctx->has_b_frames){
  2014. av_frame_set_pkt_pos(picture, avpkt->pos);
  2015. }
  2016. //FIXME these should be under if(!avctx->has_b_frames)
  2017. /* get_buffer is supposed to set frame parameters */
  2018. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  2019. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  2020. if (!picture->width) picture->width = avctx->width;
  2021. if (!picture->height) picture->height = avctx->height;
  2022. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  2023. }
  2024. }
  2025. add_metadata_from_side_data(avctx, picture);
  2026. fail:
  2027. emms_c(); //needed to avoid an emms_c() call before every return;
  2028. avctx->internal->pkt = NULL;
  2029. if (did_split) {
  2030. av_packet_free_side_data(&tmp);
  2031. if(ret == tmp.size)
  2032. ret = avpkt->size;
  2033. }
  2034. if (*got_picture_ptr) {
  2035. if (!avctx->refcounted_frames) {
  2036. int err = unrefcount_frame(avci, picture);
  2037. if (err < 0)
  2038. return err;
  2039. }
  2040. avctx->frame_number++;
  2041. av_frame_set_best_effort_timestamp(picture,
  2042. guess_correct_pts(avctx,
  2043. picture->pkt_pts,
  2044. picture->pkt_dts));
  2045. } else
  2046. av_frame_unref(picture);
  2047. } else
  2048. ret = 0;
  2049. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  2050. * make sure it's set correctly */
  2051. av_assert0(!picture->extended_data || picture->extended_data == picture->data);
  2052. return ret;
  2053. }
  2054. #if FF_API_OLD_DECODE_AUDIO
  2055. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  2056. int *frame_size_ptr,
  2057. AVPacket *avpkt)
  2058. {
  2059. AVFrame *frame = av_frame_alloc();
  2060. int ret, got_frame = 0;
  2061. if (!frame)
  2062. return AVERROR(ENOMEM);
  2063. if (avctx->get_buffer != avcodec_default_get_buffer) {
  2064. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  2065. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  2066. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  2067. "avcodec_decode_audio4()\n");
  2068. avctx->get_buffer = avcodec_default_get_buffer;
  2069. avctx->release_buffer = avcodec_default_release_buffer;
  2070. }
  2071. ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
  2072. if (ret >= 0 && got_frame) {
  2073. int ch, plane_size;
  2074. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  2075. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  2076. frame->nb_samples,
  2077. avctx->sample_fmt, 1);
  2078. if (*frame_size_ptr < data_size) {
  2079. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  2080. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  2081. av_frame_free(&frame);
  2082. return AVERROR(EINVAL);
  2083. }
  2084. memcpy(samples, frame->extended_data[0], plane_size);
  2085. if (planar && avctx->channels > 1) {
  2086. uint8_t *out = ((uint8_t *)samples) + plane_size;
  2087. for (ch = 1; ch < avctx->channels; ch++) {
  2088. memcpy(out, frame->extended_data[ch], plane_size);
  2089. out += plane_size;
  2090. }
  2091. }
  2092. *frame_size_ptr = data_size;
  2093. } else {
  2094. *frame_size_ptr = 0;
  2095. }
  2096. av_frame_free(&frame);
  2097. return ret;
  2098. }
  2099. #endif
  2100. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  2101. AVFrame *frame,
  2102. int *got_frame_ptr,
  2103. const AVPacket *avpkt)
  2104. {
  2105. AVCodecInternal *avci = avctx->internal;
  2106. int ret = 0;
  2107. *got_frame_ptr = 0;
  2108. if (!avpkt->data && avpkt->size) {
  2109. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2110. return AVERROR(EINVAL);
  2111. }
  2112. if (!avctx->codec)
  2113. return AVERROR(EINVAL);
  2114. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  2115. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  2116. return AVERROR(EINVAL);
  2117. }
  2118. av_frame_unref(frame);
  2119. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  2120. uint8_t *side;
  2121. int side_size;
  2122. uint32_t discard_padding = 0;
  2123. // copy to ensure we do not change avpkt
  2124. AVPacket tmp = *avpkt;
  2125. int did_split = av_packet_split_side_data(&tmp);
  2126. ret = apply_param_change(avctx, &tmp);
  2127. if (ret < 0) {
  2128. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  2129. if (avctx->err_recognition & AV_EF_EXPLODE)
  2130. goto fail;
  2131. }
  2132. avctx->internal->pkt = &tmp;
  2133. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2134. ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  2135. else {
  2136. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  2137. frame->pkt_dts = avpkt->dts;
  2138. }
  2139. if (ret >= 0 && *got_frame_ptr) {
  2140. add_metadata_from_side_data(avctx, frame);
  2141. avctx->frame_number++;
  2142. av_frame_set_best_effort_timestamp(frame,
  2143. guess_correct_pts(avctx,
  2144. frame->pkt_pts,
  2145. frame->pkt_dts));
  2146. if (frame->format == AV_SAMPLE_FMT_NONE)
  2147. frame->format = avctx->sample_fmt;
  2148. if (!frame->channel_layout)
  2149. frame->channel_layout = avctx->channel_layout;
  2150. if (!av_frame_get_channels(frame))
  2151. av_frame_set_channels(frame, avctx->channels);
  2152. if (!frame->sample_rate)
  2153. frame->sample_rate = avctx->sample_rate;
  2154. }
  2155. side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  2156. if(side && side_size>=10) {
  2157. avctx->internal->skip_samples = AV_RL32(side);
  2158. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  2159. avctx->internal->skip_samples);
  2160. discard_padding = AV_RL32(side + 4);
  2161. }
  2162. if (avctx->internal->skip_samples && *got_frame_ptr) {
  2163. if(frame->nb_samples <= avctx->internal->skip_samples){
  2164. *got_frame_ptr = 0;
  2165. avctx->internal->skip_samples -= frame->nb_samples;
  2166. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  2167. avctx->internal->skip_samples);
  2168. } else {
  2169. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  2170. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  2171. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2172. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  2173. (AVRational){1, avctx->sample_rate},
  2174. avctx->pkt_timebase);
  2175. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  2176. frame->pkt_pts += diff_ts;
  2177. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  2178. frame->pkt_dts += diff_ts;
  2179. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2180. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2181. } else {
  2182. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  2183. }
  2184. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  2185. avctx->internal->skip_samples, frame->nb_samples);
  2186. frame->nb_samples -= avctx->internal->skip_samples;
  2187. avctx->internal->skip_samples = 0;
  2188. }
  2189. }
  2190. if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
  2191. if (discard_padding == frame->nb_samples) {
  2192. *got_frame_ptr = 0;
  2193. } else {
  2194. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2195. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  2196. (AVRational){1, avctx->sample_rate},
  2197. avctx->pkt_timebase);
  2198. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2199. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2200. } else {
  2201. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  2202. }
  2203. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  2204. discard_padding, frame->nb_samples);
  2205. frame->nb_samples -= discard_padding;
  2206. }
  2207. }
  2208. fail:
  2209. avctx->internal->pkt = NULL;
  2210. if (did_split) {
  2211. av_packet_free_side_data(&tmp);
  2212. if(ret == tmp.size)
  2213. ret = avpkt->size;
  2214. }
  2215. if (ret >= 0 && *got_frame_ptr) {
  2216. if (!avctx->refcounted_frames) {
  2217. int err = unrefcount_frame(avci, frame);
  2218. if (err < 0)
  2219. return err;
  2220. }
  2221. } else
  2222. av_frame_unref(frame);
  2223. }
  2224. return ret;
  2225. }
  2226. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  2227. static int recode_subtitle(AVCodecContext *avctx,
  2228. AVPacket *outpkt, const AVPacket *inpkt)
  2229. {
  2230. #if CONFIG_ICONV
  2231. iconv_t cd = (iconv_t)-1;
  2232. int ret = 0;
  2233. char *inb, *outb;
  2234. size_t inl, outl;
  2235. AVPacket tmp;
  2236. #endif
  2237. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  2238. return 0;
  2239. #if CONFIG_ICONV
  2240. cd = iconv_open("UTF-8", avctx->sub_charenc);
  2241. av_assert0(cd != (iconv_t)-1);
  2242. inb = inpkt->data;
  2243. inl = inpkt->size;
  2244. if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  2245. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  2246. ret = AVERROR(ENOMEM);
  2247. goto end;
  2248. }
  2249. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  2250. if (ret < 0)
  2251. goto end;
  2252. outpkt->buf = tmp.buf;
  2253. outpkt->data = tmp.data;
  2254. outpkt->size = tmp.size;
  2255. outb = outpkt->data;
  2256. outl = outpkt->size;
  2257. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  2258. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  2259. outl >= outpkt->size || inl != 0) {
  2260. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  2261. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2262. av_free_packet(&tmp);
  2263. ret = AVERROR(errno);
  2264. goto end;
  2265. }
  2266. outpkt->size -= outl;
  2267. memset(outpkt->data + outpkt->size, 0, outl);
  2268. end:
  2269. if (cd != (iconv_t)-1)
  2270. iconv_close(cd);
  2271. return ret;
  2272. #else
  2273. av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
  2274. return AVERROR(EINVAL);
  2275. #endif
  2276. }
  2277. static int utf8_check(const uint8_t *str)
  2278. {
  2279. const uint8_t *byte;
  2280. uint32_t codepoint, min;
  2281. while (*str) {
  2282. byte = str;
  2283. GET_UTF8(codepoint, *(byte++), return 0;);
  2284. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2285. 1 << (5 * (byte - str) - 4);
  2286. if (codepoint < min || codepoint >= 0x110000 ||
  2287. codepoint == 0xFFFE /* BOM */ ||
  2288. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2289. return 0;
  2290. str = byte;
  2291. }
  2292. return 1;
  2293. }
  2294. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2295. int *got_sub_ptr,
  2296. AVPacket *avpkt)
  2297. {
  2298. int i, ret = 0;
  2299. if (!avpkt->data && avpkt->size) {
  2300. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2301. return AVERROR(EINVAL);
  2302. }
  2303. if (!avctx->codec)
  2304. return AVERROR(EINVAL);
  2305. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2306. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2307. return AVERROR(EINVAL);
  2308. }
  2309. *got_sub_ptr = 0;
  2310. get_subtitle_defaults(sub);
  2311. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  2312. AVPacket pkt_recoded;
  2313. AVPacket tmp = *avpkt;
  2314. int did_split = av_packet_split_side_data(&tmp);
  2315. //apply_param_change(avctx, &tmp);
  2316. if (did_split) {
  2317. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  2318. * proper padding.
  2319. * If the side data is smaller than the buffer padding size, the
  2320. * remaining bytes should have already been filled with zeros by the
  2321. * original packet allocation anyway. */
  2322. memset(tmp.data + tmp.size, 0,
  2323. FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
  2324. }
  2325. pkt_recoded = tmp;
  2326. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2327. if (ret < 0) {
  2328. *got_sub_ptr = 0;
  2329. } else {
  2330. avctx->internal->pkt = &pkt_recoded;
  2331. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  2332. sub->pts = av_rescale_q(avpkt->pts,
  2333. avctx->pkt_timebase, AV_TIME_BASE_Q);
  2334. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2335. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2336. !!*got_sub_ptr >= !!sub->num_rects);
  2337. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2338. avctx->pkt_timebase.num) {
  2339. AVRational ms = { 1, 1000 };
  2340. sub->end_display_time = av_rescale_q(avpkt->duration,
  2341. avctx->pkt_timebase, ms);
  2342. }
  2343. for (i = 0; i < sub->num_rects; i++) {
  2344. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2345. av_log(avctx, AV_LOG_ERROR,
  2346. "Invalid UTF-8 in decoded subtitles text; "
  2347. "maybe missing -sub_charenc option\n");
  2348. avsubtitle_free(sub);
  2349. return AVERROR_INVALIDDATA;
  2350. }
  2351. }
  2352. if (tmp.data != pkt_recoded.data) { // did we recode?
  2353. /* prevent from destroying side data from original packet */
  2354. pkt_recoded.side_data = NULL;
  2355. pkt_recoded.side_data_elems = 0;
  2356. av_free_packet(&pkt_recoded);
  2357. }
  2358. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2359. sub->format = 0;
  2360. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2361. sub->format = 1;
  2362. avctx->internal->pkt = NULL;
  2363. }
  2364. if (did_split) {
  2365. av_packet_free_side_data(&tmp);
  2366. if(ret == tmp.size)
  2367. ret = avpkt->size;
  2368. }
  2369. if (*got_sub_ptr)
  2370. avctx->frame_number++;
  2371. }
  2372. return ret;
  2373. }
  2374. void avsubtitle_free(AVSubtitle *sub)
  2375. {
  2376. int i;
  2377. for (i = 0; i < sub->num_rects; i++) {
  2378. av_freep(&sub->rects[i]->pict.data[0]);
  2379. av_freep(&sub->rects[i]->pict.data[1]);
  2380. av_freep(&sub->rects[i]->pict.data[2]);
  2381. av_freep(&sub->rects[i]->pict.data[3]);
  2382. av_freep(&sub->rects[i]->text);
  2383. av_freep(&sub->rects[i]->ass);
  2384. av_freep(&sub->rects[i]);
  2385. }
  2386. av_freep(&sub->rects);
  2387. memset(sub, 0, sizeof(AVSubtitle));
  2388. }
  2389. av_cold int avcodec_close(AVCodecContext *avctx)
  2390. {
  2391. if (!avctx)
  2392. return 0;
  2393. if (avcodec_is_open(avctx)) {
  2394. FramePool *pool = avctx->internal->pool;
  2395. int i;
  2396. if (CONFIG_FRAME_THREAD_ENCODER &&
  2397. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2398. ff_frame_thread_encoder_free(avctx);
  2399. }
  2400. if (HAVE_THREADS && avctx->internal->thread_ctx)
  2401. ff_thread_free(avctx);
  2402. if (avctx->codec && avctx->codec->close)
  2403. avctx->codec->close(avctx);
  2404. avctx->coded_frame = NULL;
  2405. avctx->internal->byte_buffer_size = 0;
  2406. av_freep(&avctx->internal->byte_buffer);
  2407. av_frame_free(&avctx->internal->to_free);
  2408. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2409. av_buffer_pool_uninit(&pool->pools[i]);
  2410. av_freep(&avctx->internal->pool);
  2411. if (avctx->hwaccel && avctx->hwaccel->uninit)
  2412. avctx->hwaccel->uninit(avctx);
  2413. av_freep(&avctx->internal->hwaccel_priv_data);
  2414. av_freep(&avctx->internal);
  2415. }
  2416. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2417. av_opt_free(avctx->priv_data);
  2418. av_opt_free(avctx);
  2419. av_freep(&avctx->priv_data);
  2420. if (av_codec_is_encoder(avctx->codec))
  2421. av_freep(&avctx->extradata);
  2422. avctx->codec = NULL;
  2423. avctx->active_thread_type = 0;
  2424. return 0;
  2425. }
  2426. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2427. {
  2428. switch(id){
  2429. //This is for future deprecatec codec ids, its empty since
  2430. //last major bump but will fill up again over time, please don't remove it
  2431. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  2432. case AV_CODEC_ID_BRENDER_PIX_DEPRECATED : return AV_CODEC_ID_BRENDER_PIX;
  2433. case AV_CODEC_ID_OPUS_DEPRECATED : return AV_CODEC_ID_OPUS;
  2434. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  2435. case AV_CODEC_ID_PAF_AUDIO_DEPRECATED : return AV_CODEC_ID_PAF_AUDIO;
  2436. case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
  2437. case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
  2438. case AV_CODEC_ID_ADPCM_VIMA_DEPRECATED : return AV_CODEC_ID_ADPCM_VIMA;
  2439. case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
  2440. case AV_CODEC_ID_EXR_DEPRECATED : return AV_CODEC_ID_EXR;
  2441. case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
  2442. case AV_CODEC_ID_PAF_VIDEO_DEPRECATED : return AV_CODEC_ID_PAF_VIDEO;
  2443. case AV_CODEC_ID_WEBP_DEPRECATED : return AV_CODEC_ID_WEBP;
  2444. case AV_CODEC_ID_HEVC_DEPRECATED : return AV_CODEC_ID_HEVC;
  2445. case AV_CODEC_ID_MVC1_DEPRECATED : return AV_CODEC_ID_MVC1;
  2446. case AV_CODEC_ID_MVC2_DEPRECATED : return AV_CODEC_ID_MVC2;
  2447. case AV_CODEC_ID_SANM_DEPRECATED : return AV_CODEC_ID_SANM;
  2448. case AV_CODEC_ID_SGIRLE_DEPRECATED : return AV_CODEC_ID_SGIRLE;
  2449. case AV_CODEC_ID_VP7_DEPRECATED : return AV_CODEC_ID_VP7;
  2450. default : return id;
  2451. }
  2452. }
  2453. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2454. {
  2455. AVCodec *p, *experimental = NULL;
  2456. p = first_avcodec;
  2457. id= remap_deprecated_codec_id(id);
  2458. while (p) {
  2459. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2460. p->id == id) {
  2461. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  2462. experimental = p;
  2463. } else
  2464. return p;
  2465. }
  2466. p = p->next;
  2467. }
  2468. return experimental;
  2469. }
  2470. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2471. {
  2472. return find_encdec(id, 1);
  2473. }
  2474. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2475. {
  2476. AVCodec *p;
  2477. if (!name)
  2478. return NULL;
  2479. p = first_avcodec;
  2480. while (p) {
  2481. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2482. return p;
  2483. p = p->next;
  2484. }
  2485. return NULL;
  2486. }
  2487. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2488. {
  2489. return find_encdec(id, 0);
  2490. }
  2491. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2492. {
  2493. AVCodec *p;
  2494. if (!name)
  2495. return NULL;
  2496. p = first_avcodec;
  2497. while (p) {
  2498. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2499. return p;
  2500. p = p->next;
  2501. }
  2502. return NULL;
  2503. }
  2504. const char *avcodec_get_name(enum AVCodecID id)
  2505. {
  2506. const AVCodecDescriptor *cd;
  2507. AVCodec *codec;
  2508. if (id == AV_CODEC_ID_NONE)
  2509. return "none";
  2510. cd = avcodec_descriptor_get(id);
  2511. if (cd)
  2512. return cd->name;
  2513. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2514. codec = avcodec_find_decoder(id);
  2515. if (codec)
  2516. return codec->name;
  2517. codec = avcodec_find_encoder(id);
  2518. if (codec)
  2519. return codec->name;
  2520. return "unknown_codec";
  2521. }
  2522. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2523. {
  2524. int i, len, ret = 0;
  2525. #define TAG_PRINT(x) \
  2526. (((x) >= '0' && (x) <= '9') || \
  2527. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2528. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2529. for (i = 0; i < 4; i++) {
  2530. len = snprintf(buf, buf_size,
  2531. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2532. buf += len;
  2533. buf_size = buf_size > len ? buf_size - len : 0;
  2534. ret += len;
  2535. codec_tag >>= 8;
  2536. }
  2537. return ret;
  2538. }
  2539. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2540. {
  2541. const char *codec_type;
  2542. const char *codec_name;
  2543. const char *profile = NULL;
  2544. const AVCodec *p;
  2545. int bitrate;
  2546. AVRational display_aspect_ratio;
  2547. if (!buf || buf_size <= 0)
  2548. return;
  2549. codec_type = av_get_media_type_string(enc->codec_type);
  2550. codec_name = avcodec_get_name(enc->codec_id);
  2551. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2552. if (enc->codec)
  2553. p = enc->codec;
  2554. else
  2555. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2556. avcodec_find_decoder(enc->codec_id);
  2557. if (p)
  2558. profile = av_get_profile_name(p, enc->profile);
  2559. }
  2560. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2561. codec_name);
  2562. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2563. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2564. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2565. if (profile)
  2566. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2567. if (enc->codec_tag) {
  2568. char tag_buf[32];
  2569. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2570. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2571. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2572. }
  2573. switch (enc->codec_type) {
  2574. case AVMEDIA_TYPE_VIDEO:
  2575. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  2576. char detail[256] = "(";
  2577. const char *colorspace_name;
  2578. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2579. ", %s",
  2580. av_get_pix_fmt_name(enc->pix_fmt));
  2581. if (enc->bits_per_raw_sample &&
  2582. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  2583. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2584. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2585. av_strlcatf(detail, sizeof(detail),
  2586. enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
  2587. colorspace_name = av_get_colorspace_name(enc->colorspace);
  2588. if (colorspace_name)
  2589. av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
  2590. if (strlen(detail) > 1) {
  2591. detail[strlen(detail) - 2] = 0;
  2592. av_strlcatf(buf, buf_size, "%s)", detail);
  2593. }
  2594. }
  2595. if (enc->width) {
  2596. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2597. ", %dx%d",
  2598. enc->width, enc->height);
  2599. if (enc->sample_aspect_ratio.num) {
  2600. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2601. enc->width * enc->sample_aspect_ratio.num,
  2602. enc->height * enc->sample_aspect_ratio.den,
  2603. 1024 * 1024);
  2604. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2605. " [SAR %d:%d DAR %d:%d]",
  2606. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2607. display_aspect_ratio.num, display_aspect_ratio.den);
  2608. }
  2609. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2610. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2611. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2612. ", %d/%d",
  2613. enc->time_base.num / g, enc->time_base.den / g);
  2614. }
  2615. }
  2616. if (encode) {
  2617. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2618. ", q=%d-%d", enc->qmin, enc->qmax);
  2619. }
  2620. break;
  2621. case AVMEDIA_TYPE_AUDIO:
  2622. if (enc->sample_rate) {
  2623. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2624. ", %d Hz", enc->sample_rate);
  2625. }
  2626. av_strlcat(buf, ", ", buf_size);
  2627. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2628. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2629. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2630. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2631. }
  2632. if ( enc->bits_per_raw_sample > 0
  2633. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  2634. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2635. " (%d bit)", enc->bits_per_raw_sample);
  2636. break;
  2637. case AVMEDIA_TYPE_DATA:
  2638. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2639. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2640. if (g)
  2641. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2642. ", %d/%d",
  2643. enc->time_base.num / g, enc->time_base.den / g);
  2644. }
  2645. break;
  2646. case AVMEDIA_TYPE_SUBTITLE:
  2647. if (enc->width)
  2648. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2649. ", %dx%d", enc->width, enc->height);
  2650. break;
  2651. default:
  2652. return;
  2653. }
  2654. if (encode) {
  2655. if (enc->flags & CODEC_FLAG_PASS1)
  2656. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2657. ", pass 1");
  2658. if (enc->flags & CODEC_FLAG_PASS2)
  2659. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2660. ", pass 2");
  2661. }
  2662. bitrate = get_bit_rate(enc);
  2663. if (bitrate != 0) {
  2664. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2665. ", %d kb/s", bitrate / 1000);
  2666. } else if (enc->rc_max_rate > 0) {
  2667. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2668. ", max. %d kb/s", enc->rc_max_rate / 1000);
  2669. }
  2670. }
  2671. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2672. {
  2673. const AVProfile *p;
  2674. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2675. return NULL;
  2676. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2677. if (p->profile == profile)
  2678. return p->name;
  2679. return NULL;
  2680. }
  2681. unsigned avcodec_version(void)
  2682. {
  2683. // av_assert0(AV_CODEC_ID_V410==164);
  2684. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2685. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2686. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2687. av_assert0(AV_CODEC_ID_SRT==94216);
  2688. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2689. av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
  2690. av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
  2691. av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
  2692. av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
  2693. av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
  2694. return LIBAVCODEC_VERSION_INT;
  2695. }
  2696. const char *avcodec_configuration(void)
  2697. {
  2698. return FFMPEG_CONFIGURATION;
  2699. }
  2700. const char *avcodec_license(void)
  2701. {
  2702. #define LICENSE_PREFIX "libavcodec license: "
  2703. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2704. }
  2705. void avcodec_flush_buffers(AVCodecContext *avctx)
  2706. {
  2707. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2708. ff_thread_flush(avctx);
  2709. else if (avctx->codec->flush)
  2710. avctx->codec->flush(avctx);
  2711. avctx->pts_correction_last_pts =
  2712. avctx->pts_correction_last_dts = INT64_MIN;
  2713. if (!avctx->refcounted_frames)
  2714. av_frame_unref(avctx->internal->to_free);
  2715. }
  2716. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2717. {
  2718. switch (codec_id) {
  2719. case AV_CODEC_ID_8SVX_EXP:
  2720. case AV_CODEC_ID_8SVX_FIB:
  2721. case AV_CODEC_ID_ADPCM_CT:
  2722. case AV_CODEC_ID_ADPCM_IMA_APC:
  2723. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2724. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2725. case AV_CODEC_ID_ADPCM_IMA_WS:
  2726. case AV_CODEC_ID_ADPCM_G722:
  2727. case AV_CODEC_ID_ADPCM_YAMAHA:
  2728. return 4;
  2729. case AV_CODEC_ID_DSD_LSBF:
  2730. case AV_CODEC_ID_DSD_MSBF:
  2731. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  2732. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  2733. case AV_CODEC_ID_PCM_ALAW:
  2734. case AV_CODEC_ID_PCM_MULAW:
  2735. case AV_CODEC_ID_PCM_S8:
  2736. case AV_CODEC_ID_PCM_S8_PLANAR:
  2737. case AV_CODEC_ID_PCM_U8:
  2738. case AV_CODEC_ID_PCM_ZORK:
  2739. return 8;
  2740. case AV_CODEC_ID_PCM_S16BE:
  2741. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2742. case AV_CODEC_ID_PCM_S16LE:
  2743. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2744. case AV_CODEC_ID_PCM_U16BE:
  2745. case AV_CODEC_ID_PCM_U16LE:
  2746. return 16;
  2747. case AV_CODEC_ID_PCM_S24DAUD:
  2748. case AV_CODEC_ID_PCM_S24BE:
  2749. case AV_CODEC_ID_PCM_S24LE:
  2750. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2751. case AV_CODEC_ID_PCM_U24BE:
  2752. case AV_CODEC_ID_PCM_U24LE:
  2753. return 24;
  2754. case AV_CODEC_ID_PCM_S32BE:
  2755. case AV_CODEC_ID_PCM_S32LE:
  2756. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2757. case AV_CODEC_ID_PCM_U32BE:
  2758. case AV_CODEC_ID_PCM_U32LE:
  2759. case AV_CODEC_ID_PCM_F32BE:
  2760. case AV_CODEC_ID_PCM_F32LE:
  2761. return 32;
  2762. case AV_CODEC_ID_PCM_F64BE:
  2763. case AV_CODEC_ID_PCM_F64LE:
  2764. return 64;
  2765. default:
  2766. return 0;
  2767. }
  2768. }
  2769. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2770. {
  2771. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2772. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2773. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2774. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2775. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2776. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2777. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2778. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2779. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2780. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2781. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2782. };
  2783. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2784. return AV_CODEC_ID_NONE;
  2785. if (be < 0 || be > 1)
  2786. be = AV_NE(1, 0);
  2787. return map[fmt][be];
  2788. }
  2789. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2790. {
  2791. switch (codec_id) {
  2792. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2793. return 2;
  2794. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2795. return 3;
  2796. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2797. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2798. case AV_CODEC_ID_ADPCM_IMA_QT:
  2799. case AV_CODEC_ID_ADPCM_SWF:
  2800. case AV_CODEC_ID_ADPCM_MS:
  2801. return 4;
  2802. default:
  2803. return av_get_exact_bits_per_sample(codec_id);
  2804. }
  2805. }
  2806. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2807. {
  2808. int id, sr, ch, ba, tag, bps;
  2809. id = avctx->codec_id;
  2810. sr = avctx->sample_rate;
  2811. ch = avctx->channels;
  2812. ba = avctx->block_align;
  2813. tag = avctx->codec_tag;
  2814. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2815. /* codecs with an exact constant bits per sample */
  2816. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2817. return (frame_bytes * 8LL) / (bps * ch);
  2818. bps = avctx->bits_per_coded_sample;
  2819. /* codecs with a fixed packet duration */
  2820. switch (id) {
  2821. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2822. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2823. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2824. case AV_CODEC_ID_AMR_NB:
  2825. case AV_CODEC_ID_EVRC:
  2826. case AV_CODEC_ID_GSM:
  2827. case AV_CODEC_ID_QCELP:
  2828. case AV_CODEC_ID_RA_288: return 160;
  2829. case AV_CODEC_ID_AMR_WB:
  2830. case AV_CODEC_ID_GSM_MS: return 320;
  2831. case AV_CODEC_ID_MP1: return 384;
  2832. case AV_CODEC_ID_ATRAC1: return 512;
  2833. case AV_CODEC_ID_ATRAC3: return 1024;
  2834. case AV_CODEC_ID_MP2:
  2835. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2836. case AV_CODEC_ID_AC3: return 1536;
  2837. }
  2838. if (sr > 0) {
  2839. /* calc from sample rate */
  2840. if (id == AV_CODEC_ID_TTA)
  2841. return 256 * sr / 245;
  2842. if (ch > 0) {
  2843. /* calc from sample rate and channels */
  2844. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2845. return (480 << (sr / 22050)) / ch;
  2846. }
  2847. }
  2848. if (ba > 0) {
  2849. /* calc from block_align */
  2850. if (id == AV_CODEC_ID_SIPR) {
  2851. switch (ba) {
  2852. case 20: return 160;
  2853. case 19: return 144;
  2854. case 29: return 288;
  2855. case 37: return 480;
  2856. }
  2857. } else if (id == AV_CODEC_ID_ILBC) {
  2858. switch (ba) {
  2859. case 38: return 160;
  2860. case 50: return 240;
  2861. }
  2862. }
  2863. }
  2864. if (frame_bytes > 0) {
  2865. /* calc from frame_bytes only */
  2866. if (id == AV_CODEC_ID_TRUESPEECH)
  2867. return 240 * (frame_bytes / 32);
  2868. if (id == AV_CODEC_ID_NELLYMOSER)
  2869. return 256 * (frame_bytes / 64);
  2870. if (id == AV_CODEC_ID_RA_144)
  2871. return 160 * (frame_bytes / 20);
  2872. if (id == AV_CODEC_ID_G723_1)
  2873. return 240 * (frame_bytes / 24);
  2874. if (bps > 0) {
  2875. /* calc from frame_bytes and bits_per_coded_sample */
  2876. if (id == AV_CODEC_ID_ADPCM_G726)
  2877. return frame_bytes * 8 / bps;
  2878. }
  2879. if (ch > 0) {
  2880. /* calc from frame_bytes and channels */
  2881. switch (id) {
  2882. case AV_CODEC_ID_ADPCM_AFC:
  2883. return frame_bytes / (9 * ch) * 16;
  2884. case AV_CODEC_ID_ADPCM_DTK:
  2885. return frame_bytes / (16 * ch) * 28;
  2886. case AV_CODEC_ID_ADPCM_4XM:
  2887. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2888. return (frame_bytes - 4 * ch) * 2 / ch;
  2889. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2890. return (frame_bytes - 4) * 2 / ch;
  2891. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2892. return (frame_bytes - 8) * 2 / ch;
  2893. case AV_CODEC_ID_ADPCM_XA:
  2894. return (frame_bytes / 128) * 224 / ch;
  2895. case AV_CODEC_ID_INTERPLAY_DPCM:
  2896. return (frame_bytes - 6 - ch) / ch;
  2897. case AV_CODEC_ID_ROQ_DPCM:
  2898. return (frame_bytes - 8) / ch;
  2899. case AV_CODEC_ID_XAN_DPCM:
  2900. return (frame_bytes - 2 * ch) / ch;
  2901. case AV_CODEC_ID_MACE3:
  2902. return 3 * frame_bytes / ch;
  2903. case AV_CODEC_ID_MACE6:
  2904. return 6 * frame_bytes / ch;
  2905. case AV_CODEC_ID_PCM_LXF:
  2906. return 2 * (frame_bytes / (5 * ch));
  2907. case AV_CODEC_ID_IAC:
  2908. case AV_CODEC_ID_IMC:
  2909. return 4 * frame_bytes / ch;
  2910. }
  2911. if (tag) {
  2912. /* calc from frame_bytes, channels, and codec_tag */
  2913. if (id == AV_CODEC_ID_SOL_DPCM) {
  2914. if (tag == 3)
  2915. return frame_bytes / ch;
  2916. else
  2917. return frame_bytes * 2 / ch;
  2918. }
  2919. }
  2920. if (ba > 0) {
  2921. /* calc from frame_bytes, channels, and block_align */
  2922. int blocks = frame_bytes / ba;
  2923. switch (avctx->codec_id) {
  2924. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2925. if (bps < 2 || bps > 5)
  2926. return 0;
  2927. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  2928. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2929. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2930. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2931. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2932. case AV_CODEC_ID_ADPCM_IMA_RAD:
  2933. return blocks * ((ba - 4 * ch) * 2 / ch);
  2934. case AV_CODEC_ID_ADPCM_MS:
  2935. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2936. }
  2937. }
  2938. if (bps > 0) {
  2939. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2940. switch (avctx->codec_id) {
  2941. case AV_CODEC_ID_PCM_DVD:
  2942. if(bps<4)
  2943. return 0;
  2944. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2945. case AV_CODEC_ID_PCM_BLURAY:
  2946. if(bps<4)
  2947. return 0;
  2948. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2949. case AV_CODEC_ID_S302M:
  2950. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2951. }
  2952. }
  2953. }
  2954. }
  2955. /* Fall back on using frame_size */
  2956. if (avctx->frame_size > 1 && frame_bytes)
  2957. return avctx->frame_size;
  2958. //For WMA we currently have no other means to calculate duration thus we
  2959. //do it here by assuming CBR, which is true for all known cases.
  2960. if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
  2961. if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
  2962. return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
  2963. }
  2964. return 0;
  2965. }
  2966. #if !HAVE_THREADS
  2967. int ff_thread_init(AVCodecContext *s)
  2968. {
  2969. return -1;
  2970. }
  2971. #endif
  2972. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2973. {
  2974. unsigned int n = 0;
  2975. while (v >= 0xff) {
  2976. *s++ = 0xff;
  2977. v -= 0xff;
  2978. n++;
  2979. }
  2980. *s = v;
  2981. n++;
  2982. return n;
  2983. }
  2984. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2985. {
  2986. int i;
  2987. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2988. return i;
  2989. }
  2990. #if FF_API_MISSING_SAMPLE
  2991. FF_DISABLE_DEPRECATION_WARNINGS
  2992. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2993. {
  2994. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2995. "version to the newest one from Git. If the problem still "
  2996. "occurs, it means that your file has a feature which has not "
  2997. "been implemented.\n", feature);
  2998. if(want_sample)
  2999. av_log_ask_for_sample(avc, NULL);
  3000. }
  3001. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  3002. {
  3003. va_list argument_list;
  3004. va_start(argument_list, msg);
  3005. if (msg)
  3006. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  3007. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  3008. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  3009. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  3010. va_end(argument_list);
  3011. }
  3012. FF_ENABLE_DEPRECATION_WARNINGS
  3013. #endif /* FF_API_MISSING_SAMPLE */
  3014. static AVHWAccel *first_hwaccel = NULL;
  3015. static AVHWAccel **last_hwaccel = &first_hwaccel;
  3016. void av_register_hwaccel(AVHWAccel *hwaccel)
  3017. {
  3018. AVHWAccel **p = last_hwaccel;
  3019. hwaccel->next = NULL;
  3020. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  3021. p = &(*p)->next;
  3022. last_hwaccel = &hwaccel->next;
  3023. }
  3024. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  3025. {
  3026. return hwaccel ? hwaccel->next : first_hwaccel;
  3027. }
  3028. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  3029. {
  3030. if (lockmgr_cb) {
  3031. if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  3032. return -1;
  3033. if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  3034. return -1;
  3035. codec_mutex = NULL;
  3036. avformat_mutex = NULL;
  3037. }
  3038. lockmgr_cb = cb;
  3039. if (lockmgr_cb) {
  3040. if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  3041. return -1;
  3042. if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  3043. return -1;
  3044. }
  3045. return 0;
  3046. }
  3047. int ff_lock_avcodec(AVCodecContext *log_ctx)
  3048. {
  3049. if (lockmgr_cb) {
  3050. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  3051. return -1;
  3052. }
  3053. entangled_thread_counter++;
  3054. if (entangled_thread_counter != 1) {
  3055. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  3056. if (!lockmgr_cb)
  3057. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  3058. ff_avcodec_locked = 1;
  3059. ff_unlock_avcodec();
  3060. return AVERROR(EINVAL);
  3061. }
  3062. av_assert0(!ff_avcodec_locked);
  3063. ff_avcodec_locked = 1;
  3064. return 0;
  3065. }
  3066. int ff_unlock_avcodec(void)
  3067. {
  3068. av_assert0(ff_avcodec_locked);
  3069. ff_avcodec_locked = 0;
  3070. entangled_thread_counter--;
  3071. if (lockmgr_cb) {
  3072. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  3073. return -1;
  3074. }
  3075. return 0;
  3076. }
  3077. int avpriv_lock_avformat(void)
  3078. {
  3079. if (lockmgr_cb) {
  3080. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  3081. return -1;
  3082. }
  3083. return 0;
  3084. }
  3085. int avpriv_unlock_avformat(void)
  3086. {
  3087. if (lockmgr_cb) {
  3088. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  3089. return -1;
  3090. }
  3091. return 0;
  3092. }
  3093. unsigned int avpriv_toupper4(unsigned int x)
  3094. {
  3095. return av_toupper(x & 0xFF) +
  3096. (av_toupper((x >> 8) & 0xFF) << 8) +
  3097. (av_toupper((x >> 16) & 0xFF) << 16) +
  3098. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  3099. }
  3100. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  3101. {
  3102. int ret;
  3103. dst->owner = src->owner;
  3104. ret = av_frame_ref(dst->f, src->f);
  3105. if (ret < 0)
  3106. return ret;
  3107. if (src->progress &&
  3108. !(dst->progress = av_buffer_ref(src->progress))) {
  3109. ff_thread_release_buffer(dst->owner, dst);
  3110. return AVERROR(ENOMEM);
  3111. }
  3112. return 0;
  3113. }
  3114. #if !HAVE_THREADS
  3115. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  3116. {
  3117. return ff_get_format(avctx, fmt);
  3118. }
  3119. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  3120. {
  3121. f->owner = avctx;
  3122. return ff_get_buffer(avctx, f->f, flags);
  3123. }
  3124. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  3125. {
  3126. if (f->f)
  3127. av_frame_unref(f->f);
  3128. }
  3129. void ff_thread_finish_setup(AVCodecContext *avctx)
  3130. {
  3131. }
  3132. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  3133. {
  3134. }
  3135. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  3136. {
  3137. }
  3138. int ff_thread_can_start_frame(AVCodecContext *avctx)
  3139. {
  3140. return 1;
  3141. }
  3142. int ff_alloc_entries(AVCodecContext *avctx, int count)
  3143. {
  3144. return 0;
  3145. }
  3146. void ff_reset_entries(AVCodecContext *avctx)
  3147. {
  3148. }
  3149. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  3150. {
  3151. }
  3152. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  3153. {
  3154. }
  3155. #endif
  3156. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  3157. {
  3158. AVCodec *c= avcodec_find_decoder(codec_id);
  3159. if(!c)
  3160. c= avcodec_find_encoder(codec_id);
  3161. if(c)
  3162. return c->type;
  3163. if (codec_id <= AV_CODEC_ID_NONE)
  3164. return AVMEDIA_TYPE_UNKNOWN;
  3165. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  3166. return AVMEDIA_TYPE_VIDEO;
  3167. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  3168. return AVMEDIA_TYPE_AUDIO;
  3169. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  3170. return AVMEDIA_TYPE_SUBTITLE;
  3171. return AVMEDIA_TYPE_UNKNOWN;
  3172. }
  3173. int avcodec_is_open(AVCodecContext *s)
  3174. {
  3175. return !!s->internal;
  3176. }
  3177. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  3178. {
  3179. int ret;
  3180. char *str;
  3181. ret = av_bprint_finalize(buf, &str);
  3182. if (ret < 0)
  3183. return ret;
  3184. avctx->extradata = str;
  3185. /* Note: the string is NUL terminated (so extradata can be read as a
  3186. * string), but the ending character is not accounted in the size (in
  3187. * binary formats you are likely not supposed to mux that character). When
  3188. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  3189. * zeros. */
  3190. avctx->extradata_size = buf->len;
  3191. return 0;
  3192. }
  3193. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  3194. const uint8_t *end,
  3195. uint32_t *av_restrict state)
  3196. {
  3197. int i;
  3198. av_assert0(p <= end);
  3199. if (p >= end)
  3200. return end;
  3201. for (i = 0; i < 3; i++) {
  3202. uint32_t tmp = *state << 8;
  3203. *state = tmp + *(p++);
  3204. if (tmp == 0x100 || p == end)
  3205. return p;
  3206. }
  3207. while (p < end) {
  3208. if (p[-1] > 1 ) p += 3;
  3209. else if (p[-2] ) p += 2;
  3210. else if (p[-3]|(p[-1]-1)) p++;
  3211. else {
  3212. p++;
  3213. break;
  3214. }
  3215. }
  3216. p = FFMIN(p, end) - 4;
  3217. *state = AV_RB32(p);
  3218. return p + 4;
  3219. }