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.

3562 lines
116KB

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