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.

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