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.

3909 lines
129KB

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