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.

3788 lines
124KB

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