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.

3854 lines
127KB

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