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.

3325 lines
108KB

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