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.

3785 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 *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. if (pkt) {
  649. uint8_t *packet_sd;
  650. AVFrameSideData *frame_sd;
  651. int size;
  652. frame->pkt_pts = pkt->pts;
  653. av_frame_set_pkt_pos (frame, pkt->pos);
  654. av_frame_set_pkt_duration(frame, pkt->duration);
  655. av_frame_set_pkt_size (frame, pkt->size);
  656. /* copy the replaygain data to the output frame */
  657. packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_REPLAYGAIN, &size);
  658. if (packet_sd) {
  659. frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REPLAYGAIN, size);
  660. if (!frame_sd)
  661. return AVERROR(ENOMEM);
  662. memcpy(frame_sd->data, packet_sd, size);
  663. }
  664. /* copy the displaymatrix to the output frame */
  665. packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX, &size);
  666. if (packet_sd) {
  667. frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX, size);
  668. if (!frame_sd)
  669. return AVERROR(ENOMEM);
  670. memcpy(frame_sd->data, packet_sd, size);
  671. }
  672. /* copy the stereo3d format to the output frame */
  673. packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_STEREO3D, &size);
  674. if (packet_sd) {
  675. frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_STEREO3D, size);
  676. if (!frame_sd)
  677. return AVERROR(ENOMEM);
  678. memcpy(frame_sd->data, packet_sd, size);
  679. }
  680. } else {
  681. frame->pkt_pts = AV_NOPTS_VALUE;
  682. av_frame_set_pkt_pos (frame, -1);
  683. av_frame_set_pkt_duration(frame, 0);
  684. av_frame_set_pkt_size (frame, -1);
  685. }
  686. frame->reordered_opaque = avctx->reordered_opaque;
  687. if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
  688. frame->color_primaries = avctx->color_primaries;
  689. if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
  690. frame->color_trc = avctx->color_trc;
  691. if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
  692. av_frame_set_colorspace(frame, avctx->colorspace);
  693. if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
  694. av_frame_set_color_range(frame, avctx->color_range);
  695. if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
  696. frame->chroma_location = avctx->chroma_sample_location;
  697. switch (avctx->codec->type) {
  698. case AVMEDIA_TYPE_VIDEO:
  699. frame->format = avctx->pix_fmt;
  700. if (!frame->sample_aspect_ratio.num)
  701. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  702. if (frame->width && frame->height &&
  703. av_image_check_sar(frame->width, frame->height,
  704. frame->sample_aspect_ratio) < 0) {
  705. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  706. frame->sample_aspect_ratio.num,
  707. frame->sample_aspect_ratio.den);
  708. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  709. }
  710. break;
  711. case AVMEDIA_TYPE_AUDIO:
  712. if (!frame->sample_rate)
  713. frame->sample_rate = avctx->sample_rate;
  714. if (frame->format < 0)
  715. frame->format = avctx->sample_fmt;
  716. if (!frame->channel_layout) {
  717. if (avctx->channel_layout) {
  718. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  719. avctx->channels) {
  720. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  721. "configuration.\n");
  722. return AVERROR(EINVAL);
  723. }
  724. frame->channel_layout = avctx->channel_layout;
  725. } else {
  726. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  727. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  728. avctx->channels);
  729. return AVERROR(ENOSYS);
  730. }
  731. }
  732. }
  733. av_frame_set_channels(frame, avctx->channels);
  734. break;
  735. }
  736. return 0;
  737. }
  738. #if FF_API_GET_BUFFER
  739. FF_DISABLE_DEPRECATION_WARNINGS
  740. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  741. {
  742. return avcodec_default_get_buffer2(avctx, frame, 0);
  743. }
  744. typedef struct CompatReleaseBufPriv {
  745. AVCodecContext avctx;
  746. AVFrame frame;
  747. uint8_t avframe_padding[1024]; // hack to allow linking to a avutil with larger AVFrame
  748. } CompatReleaseBufPriv;
  749. static void compat_free_buffer(void *opaque, uint8_t *data)
  750. {
  751. CompatReleaseBufPriv *priv = opaque;
  752. if (priv->avctx.release_buffer)
  753. priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  754. av_freep(&priv);
  755. }
  756. static void compat_release_buffer(void *opaque, uint8_t *data)
  757. {
  758. AVBufferRef *buf = opaque;
  759. av_buffer_unref(&buf);
  760. }
  761. FF_ENABLE_DEPRECATION_WARNINGS
  762. #endif
  763. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  764. {
  765. return ff_init_buffer_info(avctx, frame);
  766. }
  767. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  768. {
  769. const AVHWAccel *hwaccel = avctx->hwaccel;
  770. int override_dimensions = 1;
  771. int ret;
  772. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  773. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  774. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  775. return AVERROR(EINVAL);
  776. }
  777. }
  778. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  779. if (frame->width <= 0 || frame->height <= 0) {
  780. frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
  781. frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
  782. override_dimensions = 0;
  783. }
  784. }
  785. ret = ff_decode_frame_props(avctx, frame);
  786. if (ret < 0)
  787. return ret;
  788. if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
  789. return ret;
  790. if (hwaccel && hwaccel->alloc_frame) {
  791. ret = hwaccel->alloc_frame(avctx, frame);
  792. goto end;
  793. }
  794. #if FF_API_GET_BUFFER
  795. FF_DISABLE_DEPRECATION_WARNINGS
  796. /*
  797. * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
  798. * We wrap each plane in its own AVBuffer. Each of those has a reference to
  799. * a dummy AVBuffer as its private data, unreffing it on free.
  800. * When all the planes are freed, the dummy buffer's free callback calls
  801. * release_buffer().
  802. */
  803. if (avctx->get_buffer) {
  804. CompatReleaseBufPriv *priv = NULL;
  805. AVBufferRef *dummy_buf = NULL;
  806. int planes, i, ret;
  807. if (flags & AV_GET_BUFFER_FLAG_REF)
  808. frame->reference = 1;
  809. ret = avctx->get_buffer(avctx, frame);
  810. if (ret < 0)
  811. return ret;
  812. /* return if the buffers are already set up
  813. * this would happen e.g. when a custom get_buffer() calls
  814. * avcodec_default_get_buffer
  815. */
  816. if (frame->buf[0])
  817. goto end0;
  818. priv = av_mallocz(sizeof(*priv));
  819. if (!priv) {
  820. ret = AVERROR(ENOMEM);
  821. goto fail;
  822. }
  823. priv->avctx = *avctx;
  824. priv->frame = *frame;
  825. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  826. if (!dummy_buf) {
  827. ret = AVERROR(ENOMEM);
  828. goto fail;
  829. }
  830. #define WRAP_PLANE(ref_out, data, data_size) \
  831. do { \
  832. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  833. if (!dummy_ref) { \
  834. ret = AVERROR(ENOMEM); \
  835. goto fail; \
  836. } \
  837. ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
  838. dummy_ref, 0); \
  839. if (!ref_out) { \
  840. av_frame_unref(frame); \
  841. ret = AVERROR(ENOMEM); \
  842. goto fail; \
  843. } \
  844. } while (0)
  845. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  846. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  847. planes = av_pix_fmt_count_planes(frame->format);
  848. /* workaround for AVHWAccel plane count of 0, buf[0] is used as
  849. check for allocated buffers: make libavcodec happy */
  850. if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  851. planes = 1;
  852. if (!desc || planes <= 0) {
  853. ret = AVERROR(EINVAL);
  854. goto fail;
  855. }
  856. for (i = 0; i < planes; i++) {
  857. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  858. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  859. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  860. }
  861. } else {
  862. int planar = av_sample_fmt_is_planar(frame->format);
  863. planes = planar ? avctx->channels : 1;
  864. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  865. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  866. frame->extended_buf = av_malloc_array(sizeof(*frame->extended_buf),
  867. frame->nb_extended_buf);
  868. if (!frame->extended_buf) {
  869. ret = AVERROR(ENOMEM);
  870. goto fail;
  871. }
  872. }
  873. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  874. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  875. for (i = 0; i < frame->nb_extended_buf; i++)
  876. WRAP_PLANE(frame->extended_buf[i],
  877. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  878. frame->linesize[0]);
  879. }
  880. av_buffer_unref(&dummy_buf);
  881. end0:
  882. frame->width = avctx->width;
  883. frame->height = avctx->height;
  884. return 0;
  885. fail:
  886. avctx->release_buffer(avctx, frame);
  887. av_freep(&priv);
  888. av_buffer_unref(&dummy_buf);
  889. return ret;
  890. }
  891. FF_ENABLE_DEPRECATION_WARNINGS
  892. #endif
  893. ret = avctx->get_buffer2(avctx, frame, flags);
  894. end:
  895. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
  896. frame->width = avctx->width;
  897. frame->height = avctx->height;
  898. }
  899. return ret;
  900. }
  901. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  902. {
  903. int ret = get_buffer_internal(avctx, frame, flags);
  904. if (ret < 0)
  905. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  906. return ret;
  907. }
  908. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  909. {
  910. AVFrame *tmp;
  911. int ret;
  912. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  913. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  914. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  915. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  916. av_frame_unref(frame);
  917. }
  918. ff_init_buffer_info(avctx, frame);
  919. if (!frame->data[0])
  920. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  921. if (av_frame_is_writable(frame))
  922. return ff_decode_frame_props(avctx, frame);
  923. tmp = av_frame_alloc();
  924. if (!tmp)
  925. return AVERROR(ENOMEM);
  926. av_frame_move_ref(tmp, frame);
  927. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  928. if (ret < 0) {
  929. av_frame_free(&tmp);
  930. return ret;
  931. }
  932. av_frame_copy(frame, tmp);
  933. av_frame_free(&tmp);
  934. return 0;
  935. }
  936. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  937. {
  938. int ret = reget_buffer_internal(avctx, frame);
  939. if (ret < 0)
  940. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  941. return ret;
  942. }
  943. #if FF_API_GET_BUFFER
  944. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  945. {
  946. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  947. av_frame_unref(pic);
  948. }
  949. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  950. {
  951. av_assert0(0);
  952. return AVERROR_BUG;
  953. }
  954. #endif
  955. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  956. {
  957. int i;
  958. for (i = 0; i < count; i++) {
  959. int r = func(c, (char *)arg + i * size);
  960. if (ret)
  961. ret[i] = r;
  962. }
  963. return 0;
  964. }
  965. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  966. {
  967. int i;
  968. for (i = 0; i < count; i++) {
  969. int r = func(c, arg, i, 0);
  970. if (ret)
  971. ret[i] = r;
  972. }
  973. return 0;
  974. }
  975. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  976. unsigned int fourcc)
  977. {
  978. while (tags->pix_fmt >= 0) {
  979. if (tags->fourcc == fourcc)
  980. return tags->pix_fmt;
  981. tags++;
  982. }
  983. return AV_PIX_FMT_NONE;
  984. }
  985. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  986. {
  987. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  988. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  989. }
  990. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  991. {
  992. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  993. ++fmt;
  994. return fmt[0];
  995. }
  996. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  997. enum AVPixelFormat pix_fmt)
  998. {
  999. AVHWAccel *hwaccel = NULL;
  1000. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1001. if (hwaccel->id == codec_id
  1002. && hwaccel->pix_fmt == pix_fmt)
  1003. return hwaccel;
  1004. return NULL;
  1005. }
  1006. static int setup_hwaccel(AVCodecContext *avctx,
  1007. const enum AVPixelFormat fmt,
  1008. const char *name)
  1009. {
  1010. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  1011. int ret = 0;
  1012. if (!hwa) {
  1013. av_log(avctx, AV_LOG_ERROR,
  1014. "Could not find an AVHWAccel for the pixel format: %s",
  1015. name);
  1016. return AVERROR(ENOENT);
  1017. }
  1018. if (hwa->priv_data_size) {
  1019. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  1020. if (!avctx->internal->hwaccel_priv_data)
  1021. return AVERROR(ENOMEM);
  1022. }
  1023. if (hwa->init) {
  1024. ret = hwa->init(avctx);
  1025. if (ret < 0) {
  1026. av_freep(&avctx->internal->hwaccel_priv_data);
  1027. return ret;
  1028. }
  1029. }
  1030. avctx->hwaccel = hwa;
  1031. return 0;
  1032. }
  1033. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  1034. {
  1035. const AVPixFmtDescriptor *desc;
  1036. enum AVPixelFormat *choices;
  1037. enum AVPixelFormat ret;
  1038. unsigned n = 0;
  1039. while (fmt[n] != AV_PIX_FMT_NONE)
  1040. ++n;
  1041. choices = av_malloc_array(n + 1, sizeof(*choices));
  1042. if (!choices)
  1043. return AV_PIX_FMT_NONE;
  1044. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  1045. for (;;) {
  1046. ret = avctx->get_format(avctx, choices);
  1047. desc = av_pix_fmt_desc_get(ret);
  1048. if (!desc) {
  1049. ret = AV_PIX_FMT_NONE;
  1050. break;
  1051. }
  1052. if (avctx->hwaccel && avctx->hwaccel->uninit)
  1053. avctx->hwaccel->uninit(avctx);
  1054. av_freep(&avctx->internal->hwaccel_priv_data);
  1055. avctx->hwaccel = NULL;
  1056. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  1057. break;
  1058. if (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1059. break;
  1060. if (!setup_hwaccel(avctx, ret, desc->name))
  1061. break;
  1062. /* Remove failed hwaccel from choices */
  1063. for (n = 0; choices[n] != ret; n++)
  1064. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  1065. do
  1066. choices[n] = choices[n + 1];
  1067. while (choices[n++] != AV_PIX_FMT_NONE);
  1068. }
  1069. av_freep(&choices);
  1070. return ret;
  1071. }
  1072. #if FF_API_AVFRAME_LAVC
  1073. void avcodec_get_frame_defaults(AVFrame *frame)
  1074. {
  1075. #if LIBAVCODEC_VERSION_MAJOR >= 55
  1076. // extended_data should explicitly be freed when needed, this code is unsafe currently
  1077. // also this is not compatible to the <55 ABI/API
  1078. if (frame->extended_data != frame->data && 0)
  1079. av_freep(&frame->extended_data);
  1080. #endif
  1081. memset(frame, 0, sizeof(AVFrame));
  1082. av_frame_unref(frame);
  1083. }
  1084. AVFrame *avcodec_alloc_frame(void)
  1085. {
  1086. return av_frame_alloc();
  1087. }
  1088. void avcodec_free_frame(AVFrame **frame)
  1089. {
  1090. av_frame_free(frame);
  1091. }
  1092. #endif
  1093. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  1094. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  1095. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  1096. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  1097. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  1098. int av_codec_get_max_lowres(const AVCodec *codec)
  1099. {
  1100. return codec->max_lowres;
  1101. }
  1102. static void get_subtitle_defaults(AVSubtitle *sub)
  1103. {
  1104. memset(sub, 0, sizeof(*sub));
  1105. sub->pts = AV_NOPTS_VALUE;
  1106. }
  1107. static int get_bit_rate(AVCodecContext *ctx)
  1108. {
  1109. int bit_rate;
  1110. int bits_per_sample;
  1111. switch (ctx->codec_type) {
  1112. case AVMEDIA_TYPE_VIDEO:
  1113. case AVMEDIA_TYPE_DATA:
  1114. case AVMEDIA_TYPE_SUBTITLE:
  1115. case AVMEDIA_TYPE_ATTACHMENT:
  1116. bit_rate = ctx->bit_rate;
  1117. break;
  1118. case AVMEDIA_TYPE_AUDIO:
  1119. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1120. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1121. break;
  1122. default:
  1123. bit_rate = 0;
  1124. break;
  1125. }
  1126. return bit_rate;
  1127. }
  1128. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1129. {
  1130. int ret = 0;
  1131. ff_unlock_avcodec();
  1132. ret = avcodec_open2(avctx, codec, options);
  1133. ff_lock_avcodec(avctx);
  1134. return ret;
  1135. }
  1136. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1137. {
  1138. int ret = 0;
  1139. AVDictionary *tmp = NULL;
  1140. if (avcodec_is_open(avctx))
  1141. return 0;
  1142. if ((!codec && !avctx->codec)) {
  1143. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  1144. return AVERROR(EINVAL);
  1145. }
  1146. if ((codec && avctx->codec && codec != avctx->codec)) {
  1147. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  1148. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  1149. return AVERROR(EINVAL);
  1150. }
  1151. if (!codec)
  1152. codec = avctx->codec;
  1153. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  1154. return AVERROR(EINVAL);
  1155. if (options)
  1156. av_dict_copy(&tmp, *options, 0);
  1157. ret = ff_lock_avcodec(avctx);
  1158. if (ret < 0)
  1159. return ret;
  1160. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  1161. if (!avctx->internal) {
  1162. ret = AVERROR(ENOMEM);
  1163. goto end;
  1164. }
  1165. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  1166. if (!avctx->internal->pool) {
  1167. ret = AVERROR(ENOMEM);
  1168. goto free_and_end;
  1169. }
  1170. avctx->internal->to_free = av_frame_alloc();
  1171. if (!avctx->internal->to_free) {
  1172. ret = AVERROR(ENOMEM);
  1173. goto free_and_end;
  1174. }
  1175. if (codec->priv_data_size > 0) {
  1176. if (!avctx->priv_data) {
  1177. avctx->priv_data = av_mallocz(codec->priv_data_size);
  1178. if (!avctx->priv_data) {
  1179. ret = AVERROR(ENOMEM);
  1180. goto end;
  1181. }
  1182. if (codec->priv_class) {
  1183. *(const AVClass **)avctx->priv_data = codec->priv_class;
  1184. av_opt_set_defaults(avctx->priv_data);
  1185. }
  1186. }
  1187. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  1188. goto free_and_end;
  1189. } else {
  1190. avctx->priv_data = NULL;
  1191. }
  1192. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  1193. goto free_and_end;
  1194. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  1195. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist\n", codec->name);
  1196. ret = AVERROR(EINVAL);
  1197. goto free_and_end;
  1198. }
  1199. // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
  1200. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  1201. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
  1202. if (avctx->coded_width && avctx->coded_height)
  1203. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  1204. else if (avctx->width && avctx->height)
  1205. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1206. if (ret < 0)
  1207. goto free_and_end;
  1208. }
  1209. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  1210. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  1211. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  1212. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  1213. ff_set_dimensions(avctx, 0, 0);
  1214. }
  1215. if (avctx->width > 0 && avctx->height > 0) {
  1216. if (av_image_check_sar(avctx->width, avctx->height,
  1217. avctx->sample_aspect_ratio) < 0) {
  1218. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1219. avctx->sample_aspect_ratio.num,
  1220. avctx->sample_aspect_ratio.den);
  1221. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  1222. }
  1223. }
  1224. /* if the decoder init function was already called previously,
  1225. * free the already allocated subtitle_header before overwriting it */
  1226. if (av_codec_is_decoder(codec))
  1227. av_freep(&avctx->subtitle_header);
  1228. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1229. ret = AVERROR(EINVAL);
  1230. goto free_and_end;
  1231. }
  1232. avctx->codec = codec;
  1233. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  1234. avctx->codec_id == AV_CODEC_ID_NONE) {
  1235. avctx->codec_type = codec->type;
  1236. avctx->codec_id = codec->id;
  1237. }
  1238. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  1239. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  1240. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  1241. ret = AVERROR(EINVAL);
  1242. goto free_and_end;
  1243. }
  1244. avctx->frame_number = 0;
  1245. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  1246. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  1247. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1248. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  1249. AVCodec *codec2;
  1250. av_log(avctx, AV_LOG_ERROR,
  1251. "The %s '%s' is experimental but experimental codecs are not enabled, "
  1252. "add '-strict %d' if you want to use it.\n",
  1253. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  1254. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  1255. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  1256. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  1257. codec_string, codec2->name);
  1258. ret = AVERROR_EXPERIMENTAL;
  1259. goto free_and_end;
  1260. }
  1261. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  1262. (!avctx->time_base.num || !avctx->time_base.den)) {
  1263. avctx->time_base.num = 1;
  1264. avctx->time_base.den = avctx->sample_rate;
  1265. }
  1266. if (!HAVE_THREADS)
  1267. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1268. if (CONFIG_FRAME_THREAD_ENCODER) {
  1269. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  1270. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1271. ff_lock_avcodec(avctx);
  1272. if (ret < 0)
  1273. goto free_and_end;
  1274. }
  1275. if (HAVE_THREADS
  1276. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1277. ret = ff_thread_init(avctx);
  1278. if (ret < 0) {
  1279. goto free_and_end;
  1280. }
  1281. }
  1282. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  1283. avctx->thread_count = 1;
  1284. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1285. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1286. avctx->codec->max_lowres);
  1287. ret = AVERROR(EINVAL);
  1288. goto free_and_end;
  1289. }
  1290. #if FF_API_VISMV
  1291. if (avctx->debug_mv)
  1292. av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
  1293. "see the codecview filter instead.\n");
  1294. #endif
  1295. if (av_codec_is_encoder(avctx->codec)) {
  1296. int i;
  1297. if (avctx->codec->sample_fmts) {
  1298. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1299. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1300. break;
  1301. if (avctx->channels == 1 &&
  1302. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1303. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1304. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1305. break;
  1306. }
  1307. }
  1308. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1309. char buf[128];
  1310. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1311. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1312. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1313. ret = AVERROR(EINVAL);
  1314. goto free_and_end;
  1315. }
  1316. }
  1317. if (avctx->codec->pix_fmts) {
  1318. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1319. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1320. break;
  1321. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1322. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1323. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1324. char buf[128];
  1325. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1326. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1327. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1328. ret = AVERROR(EINVAL);
  1329. goto free_and_end;
  1330. }
  1331. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  1332. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
  1333. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  1334. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  1335. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  1336. avctx->color_range = AVCOL_RANGE_JPEG;
  1337. }
  1338. if (avctx->codec->supported_samplerates) {
  1339. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1340. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1341. break;
  1342. if (avctx->codec->supported_samplerates[i] == 0) {
  1343. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1344. avctx->sample_rate);
  1345. ret = AVERROR(EINVAL);
  1346. goto free_and_end;
  1347. }
  1348. }
  1349. if (avctx->codec->channel_layouts) {
  1350. if (!avctx->channel_layout) {
  1351. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1352. } else {
  1353. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1354. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1355. break;
  1356. if (avctx->codec->channel_layouts[i] == 0) {
  1357. char buf[512];
  1358. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1359. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1360. ret = AVERROR(EINVAL);
  1361. goto free_and_end;
  1362. }
  1363. }
  1364. }
  1365. if (avctx->channel_layout && avctx->channels) {
  1366. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1367. if (channels != avctx->channels) {
  1368. char buf[512];
  1369. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1370. av_log(avctx, AV_LOG_ERROR,
  1371. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1372. buf, channels, avctx->channels);
  1373. ret = AVERROR(EINVAL);
  1374. goto free_and_end;
  1375. }
  1376. } else if (avctx->channel_layout) {
  1377. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1378. }
  1379. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1380. if (avctx->width <= 0 || avctx->height <= 0) {
  1381. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1382. ret = AVERROR(EINVAL);
  1383. goto free_and_end;
  1384. }
  1385. }
  1386. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1387. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1388. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  1389. }
  1390. if (!avctx->rc_initial_buffer_occupancy)
  1391. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1392. }
  1393. avctx->pts_correction_num_faulty_pts =
  1394. avctx->pts_correction_num_faulty_dts = 0;
  1395. avctx->pts_correction_last_pts =
  1396. avctx->pts_correction_last_dts = INT64_MIN;
  1397. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1398. || avctx->internal->frame_thread_encoder)) {
  1399. ret = avctx->codec->init(avctx);
  1400. if (ret < 0) {
  1401. goto free_and_end;
  1402. }
  1403. }
  1404. ret=0;
  1405. #if FF_API_AUDIOENC_DELAY
  1406. if (av_codec_is_encoder(avctx->codec))
  1407. avctx->delay = avctx->initial_padding;
  1408. #endif
  1409. if (av_codec_is_decoder(avctx->codec)) {
  1410. if (!avctx->bit_rate)
  1411. avctx->bit_rate = get_bit_rate(avctx);
  1412. /* validate channel layout from the decoder */
  1413. if (avctx->channel_layout) {
  1414. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1415. if (!avctx->channels)
  1416. avctx->channels = channels;
  1417. else if (channels != avctx->channels) {
  1418. char buf[512];
  1419. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1420. av_log(avctx, AV_LOG_WARNING,
  1421. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1422. "ignoring specified channel layout\n",
  1423. buf, channels, avctx->channels);
  1424. avctx->channel_layout = 0;
  1425. }
  1426. }
  1427. if (avctx->channels && avctx->channels < 0 ||
  1428. avctx->channels > FF_SANE_NB_CHANNELS) {
  1429. ret = AVERROR(EINVAL);
  1430. goto free_and_end;
  1431. }
  1432. if (avctx->sub_charenc) {
  1433. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1434. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1435. "supported with subtitles codecs\n");
  1436. ret = AVERROR(EINVAL);
  1437. goto free_and_end;
  1438. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1439. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1440. "subtitles character encoding will be ignored\n",
  1441. avctx->codec_descriptor->name);
  1442. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1443. } else {
  1444. /* input character encoding is set for a text based subtitle
  1445. * codec at this point */
  1446. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1447. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1448. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1449. #if CONFIG_ICONV
  1450. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1451. if (cd == (iconv_t)-1) {
  1452. ret = AVERROR(errno);
  1453. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1454. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1455. goto free_and_end;
  1456. }
  1457. iconv_close(cd);
  1458. #else
  1459. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1460. "conversion needs a libavcodec built with iconv support "
  1461. "for this codec\n");
  1462. ret = AVERROR(ENOSYS);
  1463. goto free_and_end;
  1464. #endif
  1465. }
  1466. }
  1467. }
  1468. #if FF_API_AVCTX_TIMEBASE
  1469. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1470. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  1471. #endif
  1472. }
  1473. end:
  1474. ff_unlock_avcodec();
  1475. if (options) {
  1476. av_dict_free(options);
  1477. *options = tmp;
  1478. }
  1479. return ret;
  1480. free_and_end:
  1481. av_dict_free(&tmp);
  1482. av_freep(&avctx->priv_data);
  1483. if (avctx->internal) {
  1484. av_frame_free(&avctx->internal->to_free);
  1485. av_freep(&avctx->internal->pool);
  1486. }
  1487. av_freep(&avctx->internal);
  1488. avctx->codec = NULL;
  1489. goto end;
  1490. }
  1491. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
  1492. {
  1493. if (avpkt->size < 0) {
  1494. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1495. return AVERROR(EINVAL);
  1496. }
  1497. if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1498. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1499. size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  1500. return AVERROR(EINVAL);
  1501. }
  1502. if (avctx) {
  1503. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1504. if (!avpkt->data || avpkt->size < size) {
  1505. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1506. avpkt->data = avctx->internal->byte_buffer;
  1507. avpkt->size = avctx->internal->byte_buffer_size;
  1508. #if FF_API_DESTRUCT_PACKET
  1509. FF_DISABLE_DEPRECATION_WARNINGS
  1510. avpkt->destruct = NULL;
  1511. FF_ENABLE_DEPRECATION_WARNINGS
  1512. #endif
  1513. }
  1514. }
  1515. if (avpkt->data) {
  1516. AVBufferRef *buf = avpkt->buf;
  1517. #if FF_API_DESTRUCT_PACKET
  1518. FF_DISABLE_DEPRECATION_WARNINGS
  1519. void *destruct = avpkt->destruct;
  1520. FF_ENABLE_DEPRECATION_WARNINGS
  1521. #endif
  1522. if (avpkt->size < size) {
  1523. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1524. return AVERROR(EINVAL);
  1525. }
  1526. av_init_packet(avpkt);
  1527. #if FF_API_DESTRUCT_PACKET
  1528. FF_DISABLE_DEPRECATION_WARNINGS
  1529. avpkt->destruct = destruct;
  1530. FF_ENABLE_DEPRECATION_WARNINGS
  1531. #endif
  1532. avpkt->buf = buf;
  1533. avpkt->size = size;
  1534. return 0;
  1535. } else {
  1536. int ret = av_new_packet(avpkt, size);
  1537. if (ret < 0)
  1538. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1539. return ret;
  1540. }
  1541. }
  1542. int ff_alloc_packet(AVPacket *avpkt, int size)
  1543. {
  1544. return ff_alloc_packet2(NULL, avpkt, size);
  1545. }
  1546. /**
  1547. * Pad last frame with silence.
  1548. */
  1549. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1550. {
  1551. AVFrame *frame = NULL;
  1552. int ret;
  1553. if (!(frame = av_frame_alloc()))
  1554. return AVERROR(ENOMEM);
  1555. frame->format = src->format;
  1556. frame->channel_layout = src->channel_layout;
  1557. av_frame_set_channels(frame, av_frame_get_channels(src));
  1558. frame->nb_samples = s->frame_size;
  1559. ret = av_frame_get_buffer(frame, 32);
  1560. if (ret < 0)
  1561. goto fail;
  1562. ret = av_frame_copy_props(frame, src);
  1563. if (ret < 0)
  1564. goto fail;
  1565. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1566. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1567. goto fail;
  1568. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1569. frame->nb_samples - src->nb_samples,
  1570. s->channels, s->sample_fmt)) < 0)
  1571. goto fail;
  1572. *dst = frame;
  1573. return 0;
  1574. fail:
  1575. av_frame_free(&frame);
  1576. return ret;
  1577. }
  1578. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1579. AVPacket *avpkt,
  1580. const AVFrame *frame,
  1581. int *got_packet_ptr)
  1582. {
  1583. AVFrame *extended_frame = NULL;
  1584. AVFrame *padded_frame = NULL;
  1585. int ret;
  1586. AVPacket user_pkt = *avpkt;
  1587. int needs_realloc = !user_pkt.data;
  1588. *got_packet_ptr = 0;
  1589. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1590. av_free_packet(avpkt);
  1591. av_init_packet(avpkt);
  1592. return 0;
  1593. }
  1594. /* ensure that extended_data is properly set */
  1595. if (frame && !frame->extended_data) {
  1596. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1597. avctx->channels > AV_NUM_DATA_POINTERS) {
  1598. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1599. "with more than %d channels, but extended_data is not set.\n",
  1600. AV_NUM_DATA_POINTERS);
  1601. return AVERROR(EINVAL);
  1602. }
  1603. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1604. extended_frame = av_frame_alloc();
  1605. if (!extended_frame)
  1606. return AVERROR(ENOMEM);
  1607. memcpy(extended_frame, frame, sizeof(AVFrame));
  1608. extended_frame->extended_data = extended_frame->data;
  1609. frame = extended_frame;
  1610. }
  1611. /* check for valid frame size */
  1612. if (frame) {
  1613. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1614. if (frame->nb_samples > avctx->frame_size) {
  1615. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1616. ret = AVERROR(EINVAL);
  1617. goto end;
  1618. }
  1619. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1620. if (frame->nb_samples < avctx->frame_size &&
  1621. !avctx->internal->last_audio_frame) {
  1622. ret = pad_last_frame(avctx, &padded_frame, frame);
  1623. if (ret < 0)
  1624. goto end;
  1625. frame = padded_frame;
  1626. avctx->internal->last_audio_frame = 1;
  1627. }
  1628. if (frame->nb_samples != avctx->frame_size) {
  1629. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1630. ret = AVERROR(EINVAL);
  1631. goto end;
  1632. }
  1633. }
  1634. }
  1635. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1636. if (!ret) {
  1637. if (*got_packet_ptr) {
  1638. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1639. if (avpkt->pts == AV_NOPTS_VALUE)
  1640. avpkt->pts = frame->pts;
  1641. if (!avpkt->duration)
  1642. avpkt->duration = ff_samples_to_time_base(avctx,
  1643. frame->nb_samples);
  1644. }
  1645. avpkt->dts = avpkt->pts;
  1646. } else {
  1647. avpkt->size = 0;
  1648. }
  1649. }
  1650. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1651. needs_realloc = 0;
  1652. if (user_pkt.data) {
  1653. if (user_pkt.size >= avpkt->size) {
  1654. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1655. } else {
  1656. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1657. avpkt->size = user_pkt.size;
  1658. ret = -1;
  1659. }
  1660. avpkt->buf = user_pkt.buf;
  1661. avpkt->data = user_pkt.data;
  1662. #if FF_API_DESTRUCT_PACKET
  1663. FF_DISABLE_DEPRECATION_WARNINGS
  1664. avpkt->destruct = user_pkt.destruct;
  1665. FF_ENABLE_DEPRECATION_WARNINGS
  1666. #endif
  1667. } else {
  1668. if (av_dup_packet(avpkt) < 0) {
  1669. ret = AVERROR(ENOMEM);
  1670. }
  1671. }
  1672. }
  1673. if (!ret) {
  1674. if (needs_realloc && avpkt->data) {
  1675. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1676. if (ret >= 0)
  1677. avpkt->data = avpkt->buf->data;
  1678. }
  1679. avctx->frame_number++;
  1680. }
  1681. if (ret < 0 || !*got_packet_ptr) {
  1682. av_free_packet(avpkt);
  1683. av_init_packet(avpkt);
  1684. goto end;
  1685. }
  1686. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1687. * this needs to be moved to the encoders, but for now we can do it
  1688. * here to simplify things */
  1689. avpkt->flags |= AV_PKT_FLAG_KEY;
  1690. end:
  1691. av_frame_free(&padded_frame);
  1692. av_free(extended_frame);
  1693. #if FF_API_AUDIOENC_DELAY
  1694. avctx->delay = avctx->initial_padding;
  1695. #endif
  1696. return ret;
  1697. }
  1698. #if FF_API_OLD_ENCODE_AUDIO
  1699. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1700. uint8_t *buf, int buf_size,
  1701. const short *samples)
  1702. {
  1703. AVPacket pkt;
  1704. AVFrame *frame;
  1705. int ret, samples_size, got_packet;
  1706. av_init_packet(&pkt);
  1707. pkt.data = buf;
  1708. pkt.size = buf_size;
  1709. if (samples) {
  1710. frame = av_frame_alloc();
  1711. if (!frame)
  1712. return AVERROR(ENOMEM);
  1713. if (avctx->frame_size) {
  1714. frame->nb_samples = avctx->frame_size;
  1715. } else {
  1716. /* if frame_size is not set, the number of samples must be
  1717. * calculated from the buffer size */
  1718. int64_t nb_samples;
  1719. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1720. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1721. "support this codec\n");
  1722. av_frame_free(&frame);
  1723. return AVERROR(EINVAL);
  1724. }
  1725. nb_samples = (int64_t)buf_size * 8 /
  1726. (av_get_bits_per_sample(avctx->codec_id) *
  1727. avctx->channels);
  1728. if (nb_samples >= INT_MAX) {
  1729. av_frame_free(&frame);
  1730. return AVERROR(EINVAL);
  1731. }
  1732. frame->nb_samples = nb_samples;
  1733. }
  1734. /* it is assumed that the samples buffer is large enough based on the
  1735. * relevant parameters */
  1736. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1737. frame->nb_samples,
  1738. avctx->sample_fmt, 1);
  1739. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1740. avctx->sample_fmt,
  1741. (const uint8_t *)samples,
  1742. samples_size, 1)) < 0) {
  1743. av_frame_free(&frame);
  1744. return ret;
  1745. }
  1746. /* fabricate frame pts from sample count.
  1747. * this is needed because the avcodec_encode_audio() API does not have
  1748. * a way for the user to provide pts */
  1749. if (avctx->sample_rate && avctx->time_base.num)
  1750. frame->pts = ff_samples_to_time_base(avctx,
  1751. avctx->internal->sample_count);
  1752. else
  1753. frame->pts = AV_NOPTS_VALUE;
  1754. avctx->internal->sample_count += frame->nb_samples;
  1755. } else {
  1756. frame = NULL;
  1757. }
  1758. got_packet = 0;
  1759. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1760. if (!ret && got_packet && avctx->coded_frame) {
  1761. avctx->coded_frame->pts = pkt.pts;
  1762. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1763. }
  1764. /* free any side data since we cannot return it */
  1765. av_packet_free_side_data(&pkt);
  1766. if (frame && frame->extended_data != frame->data)
  1767. av_freep(&frame->extended_data);
  1768. av_frame_free(&frame);
  1769. return ret ? ret : pkt.size;
  1770. }
  1771. #endif
  1772. #if FF_API_OLD_ENCODE_VIDEO
  1773. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1774. const AVFrame *pict)
  1775. {
  1776. AVPacket pkt;
  1777. int ret, got_packet = 0;
  1778. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1779. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1780. return -1;
  1781. }
  1782. av_init_packet(&pkt);
  1783. pkt.data = buf;
  1784. pkt.size = buf_size;
  1785. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1786. if (!ret && got_packet && avctx->coded_frame) {
  1787. avctx->coded_frame->pts = pkt.pts;
  1788. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1789. }
  1790. /* free any side data since we cannot return it */
  1791. if (pkt.side_data_elems > 0) {
  1792. int i;
  1793. for (i = 0; i < pkt.side_data_elems; i++)
  1794. av_free(pkt.side_data[i].data);
  1795. av_freep(&pkt.side_data);
  1796. pkt.side_data_elems = 0;
  1797. }
  1798. return ret ? ret : pkt.size;
  1799. }
  1800. #endif
  1801. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1802. AVPacket *avpkt,
  1803. const AVFrame *frame,
  1804. int *got_packet_ptr)
  1805. {
  1806. int ret;
  1807. AVPacket user_pkt = *avpkt;
  1808. int needs_realloc = !user_pkt.data;
  1809. *got_packet_ptr = 0;
  1810. if(CONFIG_FRAME_THREAD_ENCODER &&
  1811. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1812. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1813. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1814. avctx->stats_out[0] = '\0';
  1815. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1816. av_free_packet(avpkt);
  1817. av_init_packet(avpkt);
  1818. avpkt->size = 0;
  1819. return 0;
  1820. }
  1821. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1822. return AVERROR(EINVAL);
  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 != enc->color_primaries ||
  2642. enc->colorspace != 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_MP2:
  2907. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2908. case AV_CODEC_ID_AC3: return 1536;
  2909. }
  2910. if (sr > 0) {
  2911. /* calc from sample rate */
  2912. if (id == AV_CODEC_ID_TTA)
  2913. return 256 * sr / 245;
  2914. if (ch > 0) {
  2915. /* calc from sample rate and channels */
  2916. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2917. return (480 << (sr / 22050)) / ch;
  2918. }
  2919. }
  2920. if (ba > 0) {
  2921. /* calc from block_align */
  2922. if (id == AV_CODEC_ID_SIPR) {
  2923. switch (ba) {
  2924. case 20: return 160;
  2925. case 19: return 144;
  2926. case 29: return 288;
  2927. case 37: return 480;
  2928. }
  2929. } else if (id == AV_CODEC_ID_ILBC) {
  2930. switch (ba) {
  2931. case 38: return 160;
  2932. case 50: return 240;
  2933. }
  2934. }
  2935. }
  2936. if (frame_bytes > 0) {
  2937. /* calc from frame_bytes only */
  2938. if (id == AV_CODEC_ID_TRUESPEECH)
  2939. return 240 * (frame_bytes / 32);
  2940. if (id == AV_CODEC_ID_NELLYMOSER)
  2941. return 256 * (frame_bytes / 64);
  2942. if (id == AV_CODEC_ID_RA_144)
  2943. return 160 * (frame_bytes / 20);
  2944. if (id == AV_CODEC_ID_G723_1)
  2945. return 240 * (frame_bytes / 24);
  2946. if (bps > 0) {
  2947. /* calc from frame_bytes and bits_per_coded_sample */
  2948. if (id == AV_CODEC_ID_ADPCM_G726)
  2949. return frame_bytes * 8 / bps;
  2950. }
  2951. if (ch > 0) {
  2952. /* calc from frame_bytes and channels */
  2953. switch (id) {
  2954. case AV_CODEC_ID_ADPCM_AFC:
  2955. return frame_bytes / (9 * ch) * 16;
  2956. case AV_CODEC_ID_ADPCM_DTK:
  2957. return frame_bytes / (16 * ch) * 28;
  2958. case AV_CODEC_ID_ADPCM_4XM:
  2959. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2960. return (frame_bytes - 4 * ch) * 2 / ch;
  2961. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2962. return (frame_bytes - 4) * 2 / ch;
  2963. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2964. return (frame_bytes - 8) * 2 / ch;
  2965. case AV_CODEC_ID_ADPCM_XA:
  2966. return (frame_bytes / 128) * 224 / ch;
  2967. case AV_CODEC_ID_INTERPLAY_DPCM:
  2968. return (frame_bytes - 6 - ch) / ch;
  2969. case AV_CODEC_ID_ROQ_DPCM:
  2970. return (frame_bytes - 8) / ch;
  2971. case AV_CODEC_ID_XAN_DPCM:
  2972. return (frame_bytes - 2 * ch) / ch;
  2973. case AV_CODEC_ID_MACE3:
  2974. return 3 * frame_bytes / ch;
  2975. case AV_CODEC_ID_MACE6:
  2976. return 6 * frame_bytes / ch;
  2977. case AV_CODEC_ID_PCM_LXF:
  2978. return 2 * (frame_bytes / (5 * ch));
  2979. case AV_CODEC_ID_IAC:
  2980. case AV_CODEC_ID_IMC:
  2981. return 4 * frame_bytes / ch;
  2982. }
  2983. if (tag) {
  2984. /* calc from frame_bytes, channels, and codec_tag */
  2985. if (id == AV_CODEC_ID_SOL_DPCM) {
  2986. if (tag == 3)
  2987. return frame_bytes / ch;
  2988. else
  2989. return frame_bytes * 2 / ch;
  2990. }
  2991. }
  2992. if (ba > 0) {
  2993. /* calc from frame_bytes, channels, and block_align */
  2994. int blocks = frame_bytes / ba;
  2995. switch (avctx->codec_id) {
  2996. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2997. if (bps < 2 || bps > 5)
  2998. return 0;
  2999. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  3000. case AV_CODEC_ID_ADPCM_IMA_DK3:
  3001. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  3002. case AV_CODEC_ID_ADPCM_IMA_DK4:
  3003. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  3004. case AV_CODEC_ID_ADPCM_IMA_RAD:
  3005. return blocks * ((ba - 4 * ch) * 2 / ch);
  3006. case AV_CODEC_ID_ADPCM_MS:
  3007. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  3008. }
  3009. }
  3010. if (bps > 0) {
  3011. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  3012. switch (avctx->codec_id) {
  3013. case AV_CODEC_ID_PCM_DVD:
  3014. if(bps<4)
  3015. return 0;
  3016. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  3017. case AV_CODEC_ID_PCM_BLURAY:
  3018. if(bps<4)
  3019. return 0;
  3020. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  3021. case AV_CODEC_ID_S302M:
  3022. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  3023. }
  3024. }
  3025. }
  3026. }
  3027. /* Fall back on using frame_size */
  3028. if (avctx->frame_size > 1 && frame_bytes)
  3029. return avctx->frame_size;
  3030. //For WMA we currently have no other means to calculate duration thus we
  3031. //do it here by assuming CBR, which is true for all known cases.
  3032. if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
  3033. if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
  3034. return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
  3035. }
  3036. return 0;
  3037. }
  3038. #if !HAVE_THREADS
  3039. int ff_thread_init(AVCodecContext *s)
  3040. {
  3041. return -1;
  3042. }
  3043. #endif
  3044. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  3045. {
  3046. unsigned int n = 0;
  3047. while (v >= 0xff) {
  3048. *s++ = 0xff;
  3049. v -= 0xff;
  3050. n++;
  3051. }
  3052. *s = v;
  3053. n++;
  3054. return n;
  3055. }
  3056. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  3057. {
  3058. int i;
  3059. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  3060. return i;
  3061. }
  3062. #if FF_API_MISSING_SAMPLE
  3063. FF_DISABLE_DEPRECATION_WARNINGS
  3064. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  3065. {
  3066. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  3067. "version to the newest one from Git. If the problem still "
  3068. "occurs, it means that your file has a feature which has not "
  3069. "been implemented.\n", feature);
  3070. if(want_sample)
  3071. av_log_ask_for_sample(avc, NULL);
  3072. }
  3073. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  3074. {
  3075. va_list argument_list;
  3076. va_start(argument_list, msg);
  3077. if (msg)
  3078. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  3079. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  3080. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  3081. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  3082. va_end(argument_list);
  3083. }
  3084. FF_ENABLE_DEPRECATION_WARNINGS
  3085. #endif /* FF_API_MISSING_SAMPLE */
  3086. static AVHWAccel *first_hwaccel = NULL;
  3087. static AVHWAccel **last_hwaccel = &first_hwaccel;
  3088. void av_register_hwaccel(AVHWAccel *hwaccel)
  3089. {
  3090. AVHWAccel **p = last_hwaccel;
  3091. hwaccel->next = NULL;
  3092. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  3093. p = &(*p)->next;
  3094. last_hwaccel = &hwaccel->next;
  3095. }
  3096. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  3097. {
  3098. return hwaccel ? hwaccel->next : first_hwaccel;
  3099. }
  3100. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  3101. {
  3102. if (lockmgr_cb) {
  3103. // There is no good way to rollback a failure to destroy the
  3104. // mutex, so we ignore failures.
  3105. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  3106. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  3107. lockmgr_cb = NULL;
  3108. codec_mutex = NULL;
  3109. avformat_mutex = NULL;
  3110. }
  3111. if (cb) {
  3112. void *new_codec_mutex = NULL;
  3113. void *new_avformat_mutex = NULL;
  3114. int err;
  3115. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  3116. return err > 0 ? AVERROR_UNKNOWN : err;
  3117. }
  3118. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  3119. // Ignore failures to destroy the newly created mutex.
  3120. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  3121. return err > 0 ? AVERROR_UNKNOWN : err;
  3122. }
  3123. lockmgr_cb = cb;
  3124. codec_mutex = new_codec_mutex;
  3125. avformat_mutex = new_avformat_mutex;
  3126. }
  3127. return 0;
  3128. }
  3129. int ff_lock_avcodec(AVCodecContext *log_ctx)
  3130. {
  3131. if (lockmgr_cb) {
  3132. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  3133. return -1;
  3134. }
  3135. entangled_thread_counter++;
  3136. if (entangled_thread_counter != 1) {
  3137. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  3138. if (!lockmgr_cb)
  3139. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  3140. ff_avcodec_locked = 1;
  3141. ff_unlock_avcodec();
  3142. return AVERROR(EINVAL);
  3143. }
  3144. av_assert0(!ff_avcodec_locked);
  3145. ff_avcodec_locked = 1;
  3146. return 0;
  3147. }
  3148. int ff_unlock_avcodec(void)
  3149. {
  3150. av_assert0(ff_avcodec_locked);
  3151. ff_avcodec_locked = 0;
  3152. entangled_thread_counter--;
  3153. if (lockmgr_cb) {
  3154. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  3155. return -1;
  3156. }
  3157. return 0;
  3158. }
  3159. int avpriv_lock_avformat(void)
  3160. {
  3161. if (lockmgr_cb) {
  3162. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  3163. return -1;
  3164. }
  3165. return 0;
  3166. }
  3167. int avpriv_unlock_avformat(void)
  3168. {
  3169. if (lockmgr_cb) {
  3170. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  3171. return -1;
  3172. }
  3173. return 0;
  3174. }
  3175. unsigned int avpriv_toupper4(unsigned int x)
  3176. {
  3177. return av_toupper(x & 0xFF) +
  3178. (av_toupper((x >> 8) & 0xFF) << 8) +
  3179. (av_toupper((x >> 16) & 0xFF) << 16) +
  3180. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  3181. }
  3182. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  3183. {
  3184. int ret;
  3185. dst->owner = src->owner;
  3186. ret = av_frame_ref(dst->f, src->f);
  3187. if (ret < 0)
  3188. return ret;
  3189. if (src->progress &&
  3190. !(dst->progress = av_buffer_ref(src->progress))) {
  3191. ff_thread_release_buffer(dst->owner, dst);
  3192. return AVERROR(ENOMEM);
  3193. }
  3194. return 0;
  3195. }
  3196. #if !HAVE_THREADS
  3197. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  3198. {
  3199. return ff_get_format(avctx, fmt);
  3200. }
  3201. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  3202. {
  3203. f->owner = avctx;
  3204. return ff_get_buffer(avctx, f->f, flags);
  3205. }
  3206. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  3207. {
  3208. if (f->f)
  3209. av_frame_unref(f->f);
  3210. }
  3211. void ff_thread_finish_setup(AVCodecContext *avctx)
  3212. {
  3213. }
  3214. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  3215. {
  3216. }
  3217. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  3218. {
  3219. }
  3220. int ff_thread_can_start_frame(AVCodecContext *avctx)
  3221. {
  3222. return 1;
  3223. }
  3224. int ff_alloc_entries(AVCodecContext *avctx, int count)
  3225. {
  3226. return 0;
  3227. }
  3228. void ff_reset_entries(AVCodecContext *avctx)
  3229. {
  3230. }
  3231. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  3232. {
  3233. }
  3234. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  3235. {
  3236. }
  3237. #endif
  3238. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  3239. {
  3240. AVCodec *c= avcodec_find_decoder(codec_id);
  3241. if(!c)
  3242. c= avcodec_find_encoder(codec_id);
  3243. if(c)
  3244. return c->type;
  3245. if (codec_id <= AV_CODEC_ID_NONE)
  3246. return AVMEDIA_TYPE_UNKNOWN;
  3247. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  3248. return AVMEDIA_TYPE_VIDEO;
  3249. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  3250. return AVMEDIA_TYPE_AUDIO;
  3251. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  3252. return AVMEDIA_TYPE_SUBTITLE;
  3253. return AVMEDIA_TYPE_UNKNOWN;
  3254. }
  3255. int avcodec_is_open(AVCodecContext *s)
  3256. {
  3257. return !!s->internal;
  3258. }
  3259. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  3260. {
  3261. int ret;
  3262. char *str;
  3263. ret = av_bprint_finalize(buf, &str);
  3264. if (ret < 0)
  3265. return ret;
  3266. avctx->extradata = str;
  3267. /* Note: the string is NUL terminated (so extradata can be read as a
  3268. * string), but the ending character is not accounted in the size (in
  3269. * binary formats you are likely not supposed to mux that character). When
  3270. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  3271. * zeros. */
  3272. avctx->extradata_size = buf->len;
  3273. return 0;
  3274. }
  3275. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  3276. const uint8_t *end,
  3277. uint32_t *av_restrict state)
  3278. {
  3279. int i;
  3280. av_assert0(p <= end);
  3281. if (p >= end)
  3282. return end;
  3283. for (i = 0; i < 3; i++) {
  3284. uint32_t tmp = *state << 8;
  3285. *state = tmp + *(p++);
  3286. if (tmp == 0x100 || p == end)
  3287. return p;
  3288. }
  3289. while (p < end) {
  3290. if (p[-1] > 1 ) p += 3;
  3291. else if (p[-2] ) p += 2;
  3292. else if (p[-3]|(p[-1]-1)) p++;
  3293. else {
  3294. p++;
  3295. break;
  3296. }
  3297. }
  3298. p = FFMIN(p, end) - 4;
  3299. *state = AV_RB32(p);
  3300. return p + 4;
  3301. }