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.

3860 lines
127KB

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