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.

3635 lines
119KB

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