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.

3833 lines
126KB

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