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.

3783 lines
124KB

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