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.

3867 lines
128KB

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