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.

3461 lines
112KB

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