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.

2683 lines
86KB

  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 "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/avassert.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "thread.h"
  40. #include "frame_thread_encoder.h"
  41. #include "audioconvert.h"
  42. #include "internal.h"
  43. #include "bytestream.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. static int volatile entangled_thread_counter = 0;
  49. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  50. static void *codec_mutex;
  51. static void *avformat_mutex;
  52. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  53. {
  54. if (min_size < *size)
  55. return ptr;
  56. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  57. ptr = av_realloc(ptr, min_size);
  58. /* we could set this to the unmodified min_size but this is safer
  59. * if the user lost the ptr and uses NULL now
  60. */
  61. if (!ptr)
  62. min_size = 0;
  63. *size = min_size;
  64. return ptr;
  65. }
  66. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  67. {
  68. void **p = ptr;
  69. if (min_size < *size)
  70. return 0;
  71. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  72. av_free(*p);
  73. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  74. if (!*p)
  75. min_size = 0;
  76. *size = min_size;
  77. return 1;
  78. }
  79. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  80. {
  81. ff_fast_malloc(ptr, size, min_size, 0);
  82. }
  83. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  84. {
  85. uint8_t **p = ptr;
  86. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  87. av_freep(p);
  88. *size = 0;
  89. return;
  90. }
  91. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  92. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  93. }
  94. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  95. {
  96. uint8_t **p = ptr;
  97. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  98. av_freep(p);
  99. *size = 0;
  100. return;
  101. }
  102. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  103. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  104. }
  105. /* encoder management */
  106. static AVCodec *first_avcodec = NULL;
  107. AVCodec *av_codec_next(const AVCodec *c)
  108. {
  109. if (c)
  110. return c->next;
  111. else
  112. return first_avcodec;
  113. }
  114. static void avcodec_init(void)
  115. {
  116. static int initialized = 0;
  117. if (initialized != 0)
  118. return;
  119. initialized = 1;
  120. ff_dsputil_static_init();
  121. }
  122. int av_codec_is_encoder(const AVCodec *codec)
  123. {
  124. return codec && (codec->encode_sub || codec->encode2);
  125. }
  126. int av_codec_is_decoder(const AVCodec *codec)
  127. {
  128. return codec && codec->decode;
  129. }
  130. void avcodec_register(AVCodec *codec)
  131. {
  132. AVCodec **p;
  133. avcodec_init();
  134. p = &first_avcodec;
  135. while (*p != NULL)
  136. p = &(*p)->next;
  137. *p = codec;
  138. codec->next = NULL;
  139. if (codec->init_static_data)
  140. codec->init_static_data(codec);
  141. }
  142. unsigned avcodec_get_edge_width(void)
  143. {
  144. return EDGE_WIDTH;
  145. }
  146. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  147. {
  148. s->coded_width = width;
  149. s->coded_height = height;
  150. s->width = -((-width ) >> s->lowres);
  151. s->height = -((-height) >> s->lowres);
  152. }
  153. #define INTERNAL_BUFFER_SIZE (32 + 1)
  154. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  155. int linesize_align[AV_NUM_DATA_POINTERS])
  156. {
  157. int i;
  158. int w_align = 1;
  159. int h_align = 1;
  160. switch (s->pix_fmt) {
  161. case AV_PIX_FMT_YUV420P:
  162. case AV_PIX_FMT_YUYV422:
  163. case AV_PIX_FMT_UYVY422:
  164. case AV_PIX_FMT_YUV422P:
  165. case AV_PIX_FMT_YUV440P:
  166. case AV_PIX_FMT_YUV444P:
  167. case AV_PIX_FMT_GBRP:
  168. case AV_PIX_FMT_GRAY8:
  169. case AV_PIX_FMT_GRAY16BE:
  170. case AV_PIX_FMT_GRAY16LE:
  171. case AV_PIX_FMT_YUVJ420P:
  172. case AV_PIX_FMT_YUVJ422P:
  173. case AV_PIX_FMT_YUVJ440P:
  174. case AV_PIX_FMT_YUVJ444P:
  175. case AV_PIX_FMT_YUVA420P:
  176. case AV_PIX_FMT_YUVA422P:
  177. case AV_PIX_FMT_YUVA444P:
  178. case AV_PIX_FMT_YUV420P9LE:
  179. case AV_PIX_FMT_YUV420P9BE:
  180. case AV_PIX_FMT_YUV420P10LE:
  181. case AV_PIX_FMT_YUV420P10BE:
  182. case AV_PIX_FMT_YUV420P12LE:
  183. case AV_PIX_FMT_YUV420P12BE:
  184. case AV_PIX_FMT_YUV420P14LE:
  185. case AV_PIX_FMT_YUV420P14BE:
  186. case AV_PIX_FMT_YUV422P9LE:
  187. case AV_PIX_FMT_YUV422P9BE:
  188. case AV_PIX_FMT_YUV422P10LE:
  189. case AV_PIX_FMT_YUV422P10BE:
  190. case AV_PIX_FMT_YUV422P12LE:
  191. case AV_PIX_FMT_YUV422P12BE:
  192. case AV_PIX_FMT_YUV422P14LE:
  193. case AV_PIX_FMT_YUV422P14BE:
  194. case AV_PIX_FMT_YUV444P9LE:
  195. case AV_PIX_FMT_YUV444P9BE:
  196. case AV_PIX_FMT_YUV444P10LE:
  197. case AV_PIX_FMT_YUV444P10BE:
  198. case AV_PIX_FMT_YUV444P12LE:
  199. case AV_PIX_FMT_YUV444P12BE:
  200. case AV_PIX_FMT_YUV444P14LE:
  201. case AV_PIX_FMT_YUV444P14BE:
  202. case AV_PIX_FMT_GBRP9LE:
  203. case AV_PIX_FMT_GBRP9BE:
  204. case AV_PIX_FMT_GBRP10LE:
  205. case AV_PIX_FMT_GBRP10BE:
  206. case AV_PIX_FMT_GBRP12LE:
  207. case AV_PIX_FMT_GBRP12BE:
  208. case AV_PIX_FMT_GBRP14LE:
  209. case AV_PIX_FMT_GBRP14BE:
  210. w_align = 16; //FIXME assume 16 pixel per macroblock
  211. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  212. break;
  213. case AV_PIX_FMT_YUV411P:
  214. case AV_PIX_FMT_UYYVYY411:
  215. w_align = 32;
  216. h_align = 8;
  217. break;
  218. case AV_PIX_FMT_YUV410P:
  219. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  220. w_align = 64;
  221. h_align = 64;
  222. }
  223. break;
  224. case AV_PIX_FMT_RGB555:
  225. if (s->codec_id == AV_CODEC_ID_RPZA) {
  226. w_align = 4;
  227. h_align = 4;
  228. }
  229. break;
  230. case AV_PIX_FMT_PAL8:
  231. case AV_PIX_FMT_BGR8:
  232. case AV_PIX_FMT_RGB8:
  233. if (s->codec_id == AV_CODEC_ID_SMC) {
  234. w_align = 4;
  235. h_align = 4;
  236. }
  237. break;
  238. case AV_PIX_FMT_BGR24:
  239. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  240. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  241. w_align = 4;
  242. h_align = 4;
  243. }
  244. break;
  245. default:
  246. w_align = 1;
  247. h_align = 1;
  248. break;
  249. }
  250. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  251. w_align = FFMAX(w_align, 8);
  252. }
  253. *width = FFALIGN(*width, w_align);
  254. *height = FFALIGN(*height, h_align);
  255. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  256. // some of the optimized chroma MC reads one line too much
  257. // which is also done in mpeg decoders with lowres > 0
  258. *height += 2;
  259. for (i = 0; i < 4; i++)
  260. linesize_align[i] = STRIDE_ALIGN;
  261. }
  262. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  263. {
  264. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  265. int chroma_shift = desc->log2_chroma_w;
  266. int linesize_align[AV_NUM_DATA_POINTERS];
  267. int align;
  268. avcodec_align_dimensions2(s, width, height, linesize_align);
  269. align = FFMAX(linesize_align[0], linesize_align[3]);
  270. linesize_align[1] <<= chroma_shift;
  271. linesize_align[2] <<= chroma_shift;
  272. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  273. *width = FFALIGN(*width, align);
  274. }
  275. void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
  276. {
  277. if (s->pkt) {
  278. frame->pkt_pts = s->pkt->pts;
  279. frame->pkt_pos = s->pkt->pos;
  280. frame->pkt_duration = s->pkt->duration;
  281. } else {
  282. frame->pkt_pts = AV_NOPTS_VALUE;
  283. frame->pkt_pos = -1;
  284. frame->pkt_duration = 0;
  285. }
  286. frame->reordered_opaque = s->reordered_opaque;
  287. switch (s->codec->type) {
  288. case AVMEDIA_TYPE_VIDEO:
  289. frame->width = s->width;
  290. frame->height = s->height;
  291. frame->format = s->pix_fmt;
  292. frame->sample_aspect_ratio = s->sample_aspect_ratio;
  293. break;
  294. case AVMEDIA_TYPE_AUDIO:
  295. frame->sample_rate = s->sample_rate;
  296. frame->format = s->sample_fmt;
  297. frame->channel_layout = s->channel_layout;
  298. frame->channels = s->channels;
  299. break;
  300. }
  301. }
  302. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  303. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  304. int buf_size, int align)
  305. {
  306. int ch, planar, needed_size, ret = 0;
  307. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  308. frame->nb_samples, sample_fmt,
  309. align);
  310. if (buf_size < needed_size)
  311. return AVERROR(EINVAL);
  312. planar = av_sample_fmt_is_planar(sample_fmt);
  313. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  314. if (!(frame->extended_data = av_mallocz(nb_channels *
  315. sizeof(*frame->extended_data))))
  316. return AVERROR(ENOMEM);
  317. } else {
  318. frame->extended_data = frame->data;
  319. }
  320. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  321. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  322. sample_fmt, align)) < 0) {
  323. if (frame->extended_data != frame->data)
  324. av_freep(&frame->extended_data);
  325. return ret;
  326. }
  327. if (frame->extended_data != frame->data) {
  328. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  329. frame->data[ch] = frame->extended_data[ch];
  330. }
  331. return ret;
  332. }
  333. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  334. {
  335. AVCodecInternal *avci = avctx->internal;
  336. InternalBuffer *buf;
  337. int buf_size, ret;
  338. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  339. frame->nb_samples, avctx->sample_fmt,
  340. 0);
  341. if (buf_size < 0)
  342. return AVERROR(EINVAL);
  343. /* allocate InternalBuffer if needed */
  344. if (!avci->buffer) {
  345. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  346. if (!avci->buffer)
  347. return AVERROR(ENOMEM);
  348. }
  349. buf = avci->buffer;
  350. /* if there is a previously-used internal buffer, check its size and
  351. * channel count to see if we can reuse it */
  352. if (buf->extended_data) {
  353. /* if current buffer is too small, free it */
  354. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  355. av_free(buf->extended_data[0]);
  356. if (buf->extended_data != buf->data)
  357. av_free(buf->extended_data);
  358. buf->extended_data = NULL;
  359. buf->data[0] = NULL;
  360. }
  361. /* if number of channels has changed, reset and/or free extended data
  362. * pointers but leave data buffer in buf->data[0] for reuse */
  363. if (buf->nb_channels != avctx->channels) {
  364. if (buf->extended_data != buf->data)
  365. av_free(buf->extended_data);
  366. buf->extended_data = NULL;
  367. }
  368. }
  369. /* if there is no previous buffer or the previous buffer cannot be used
  370. * as-is, allocate a new buffer and/or rearrange the channel pointers */
  371. if (!buf->extended_data) {
  372. if (!buf->data[0]) {
  373. if (!(buf->data[0] = av_mallocz(buf_size)))
  374. return AVERROR(ENOMEM);
  375. buf->audio_data_size = buf_size;
  376. }
  377. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  378. avctx->sample_fmt, buf->data[0],
  379. buf->audio_data_size, 0)))
  380. return ret;
  381. if (frame->extended_data == frame->data)
  382. buf->extended_data = buf->data;
  383. else
  384. buf->extended_data = frame->extended_data;
  385. memcpy(buf->data, frame->data, sizeof(frame->data));
  386. buf->linesize[0] = frame->linesize[0];
  387. buf->nb_channels = avctx->channels;
  388. } else {
  389. /* copy InternalBuffer info to the AVFrame */
  390. frame->extended_data = buf->extended_data;
  391. frame->linesize[0] = buf->linesize[0];
  392. memcpy(frame->data, buf->data, sizeof(frame->data));
  393. }
  394. frame->type = FF_BUFFER_TYPE_INTERNAL;
  395. ff_init_buffer_info(avctx, frame);
  396. if (avctx->debug & FF_DEBUG_BUFFERS)
  397. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  398. "internal audio buffer used\n", frame);
  399. return 0;
  400. }
  401. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  402. {
  403. int i;
  404. int w = s->width;
  405. int h = s->height;
  406. InternalBuffer *buf;
  407. AVCodecInternal *avci = s->internal;
  408. if (pic->data[0] != NULL) {
  409. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  410. return -1;
  411. }
  412. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  413. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  414. return -1;
  415. }
  416. if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
  417. av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  418. return -1;
  419. }
  420. if (!avci->buffer) {
  421. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  422. sizeof(InternalBuffer));
  423. }
  424. buf = &avci->buffer[avci->buffer_count];
  425. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  426. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  427. av_freep(&buf->base[i]);
  428. buf->data[i] = NULL;
  429. }
  430. }
  431. if (!buf->base[0]) {
  432. int h_chroma_shift, v_chroma_shift;
  433. int size[4] = { 0 };
  434. int tmpsize;
  435. int unaligned;
  436. AVPicture picture;
  437. int stride_align[AV_NUM_DATA_POINTERS];
  438. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  439. const int pixel_size = desc->comp[0].step_minus1 + 1;
  440. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  441. avcodec_align_dimensions2(s, &w, &h, stride_align);
  442. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  443. w += EDGE_WIDTH * 2;
  444. h += EDGE_WIDTH * 2;
  445. }
  446. do {
  447. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  448. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  449. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  450. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  451. w += w & ~(w - 1);
  452. unaligned = 0;
  453. for (i = 0; i < 4; i++)
  454. unaligned |= picture.linesize[i] % stride_align[i];
  455. } while (unaligned);
  456. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  457. if (tmpsize < 0)
  458. return -1;
  459. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  460. size[i] = picture.data[i + 1] - picture.data[i];
  461. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  462. memset(buf->base, 0, sizeof(buf->base));
  463. memset(buf->data, 0, sizeof(buf->data));
  464. for (i = 0; i < 4 && size[i]; i++) {
  465. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  466. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  467. buf->linesize[i] = picture.linesize[i];
  468. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  469. if (buf->base[i] == NULL)
  470. return AVERROR(ENOMEM);
  471. memset(buf->base[i], 128, size[i]);
  472. // no edge if EDGE EMU or not planar YUV
  473. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  474. buf->data[i] = buf->base[i];
  475. else
  476. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  477. }
  478. for (; i < AV_NUM_DATA_POINTERS; i++) {
  479. buf->base[i] = buf->data[i] = NULL;
  480. buf->linesize[i] = 0;
  481. }
  482. if (size[1] && !size[2])
  483. avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  484. buf->width = s->width;
  485. buf->height = s->height;
  486. buf->pix_fmt = s->pix_fmt;
  487. }
  488. pic->type = FF_BUFFER_TYPE_INTERNAL;
  489. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  490. pic->base[i] = buf->base[i];
  491. pic->data[i] = buf->data[i];
  492. pic->linesize[i] = buf->linesize[i];
  493. }
  494. pic->extended_data = pic->data;
  495. avci->buffer_count++;
  496. pic->width = buf->width;
  497. pic->height = buf->height;
  498. pic->format = buf->pix_fmt;
  499. ff_init_buffer_info(s, pic);
  500. if (s->debug & FF_DEBUG_BUFFERS)
  501. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  502. "buffers used\n", pic, avci->buffer_count);
  503. return 0;
  504. }
  505. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  506. {
  507. switch (avctx->codec_type) {
  508. case AVMEDIA_TYPE_VIDEO:
  509. return video_get_buffer(avctx, frame);
  510. case AVMEDIA_TYPE_AUDIO:
  511. return audio_get_buffer(avctx, frame);
  512. default:
  513. return -1;
  514. }
  515. }
  516. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  517. {
  518. int i;
  519. InternalBuffer *buf, *last;
  520. AVCodecInternal *avci = s->internal;
  521. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  522. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  523. assert(avci->buffer_count);
  524. if (avci->buffer) {
  525. buf = NULL; /* avoids warning */
  526. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  527. buf = &avci->buffer[i];
  528. if (buf->data[0] == pic->data[0])
  529. break;
  530. }
  531. av_assert0(i < avci->buffer_count);
  532. avci->buffer_count--;
  533. last = &avci->buffer[avci->buffer_count];
  534. if (buf != last)
  535. FFSWAP(InternalBuffer, *buf, *last);
  536. }
  537. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  538. pic->data[i] = NULL;
  539. // pic->base[i]=NULL;
  540. if (s->debug & FF_DEBUG_BUFFERS)
  541. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  542. "buffers used\n", pic, avci->buffer_count);
  543. }
  544. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  545. {
  546. AVFrame temp_pic;
  547. int i, ret;
  548. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  549. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  550. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  551. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  552. s->release_buffer(s, pic);
  553. }
  554. ff_init_buffer_info(s, pic);
  555. /* If no picture return a new buffer */
  556. if (pic->data[0] == NULL) {
  557. /* We will copy from buffer, so must be readable */
  558. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  559. return s->get_buffer(s, pic);
  560. }
  561. assert(s->pix_fmt == pic->format);
  562. /* If internal buffer type return the same buffer */
  563. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  564. return 0;
  565. }
  566. /*
  567. * Not internal type and reget_buffer not overridden, emulate cr buffer
  568. */
  569. temp_pic = *pic;
  570. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  571. pic->data[i] = pic->base[i] = NULL;
  572. pic->opaque = NULL;
  573. /* Allocate new frame */
  574. if ((ret = s->get_buffer(s, pic)))
  575. return ret;
  576. /* Copy image data from old buffer to new buffer */
  577. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  578. s->height);
  579. s->release_buffer(s, &temp_pic); // Release old frame
  580. return 0;
  581. }
  582. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  583. {
  584. int i;
  585. for (i = 0; i < count; i++) {
  586. int r = func(c, (char *)arg + i * size);
  587. if (ret)
  588. ret[i] = r;
  589. }
  590. return 0;
  591. }
  592. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  593. {
  594. int i;
  595. for (i = 0; i < count; i++) {
  596. int r = func(c, arg, i, 0);
  597. if (ret)
  598. ret[i] = r;
  599. }
  600. return 0;
  601. }
  602. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  603. {
  604. while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  605. ++fmt;
  606. return fmt[0];
  607. }
  608. void avcodec_get_frame_defaults(AVFrame *frame)
  609. {
  610. #if LIBAVCODEC_VERSION_MAJOR >= 55
  611. // extended_data should explicitly be freed when needed, this code is unsafe currently
  612. // also this is not compatible to the <55 ABI/API
  613. if (frame->extended_data != frame->data && 0)
  614. av_freep(&frame->extended_data);
  615. #endif
  616. memset(frame, 0, sizeof(AVFrame));
  617. frame->pts =
  618. frame->pkt_dts =
  619. frame->pkt_pts =
  620. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  621. frame->pkt_duration = 0;
  622. frame->pkt_pos = -1;
  623. frame->key_frame = 1;
  624. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  625. frame->format = -1; /* unknown */
  626. frame->extended_data = frame->data;
  627. }
  628. AVFrame *avcodec_alloc_frame(void)
  629. {
  630. AVFrame *frame = av_malloc(sizeof(AVFrame));
  631. if (frame == NULL)
  632. return NULL;
  633. frame->extended_data = NULL;
  634. avcodec_get_frame_defaults(frame);
  635. return frame;
  636. }
  637. void avcodec_free_frame(AVFrame **frame)
  638. {
  639. AVFrame *f;
  640. if (!frame || !*frame)
  641. return;
  642. f = *frame;
  643. if (f->extended_data != f->data)
  644. av_freep(&f->extended_data);
  645. av_freep(frame);
  646. }
  647. #define MAKE_ACCESSORS(str, name, type, field) \
  648. type av_##name##_get_##field(const str *s) { return s->field; } \
  649. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  650. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  651. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  652. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  653. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  654. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  655. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  656. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  657. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  658. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  659. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  660. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  661. {
  662. memset(sub, 0, sizeof(*sub));
  663. sub->pts = AV_NOPTS_VALUE;
  664. }
  665. static int get_bit_rate(AVCodecContext *ctx)
  666. {
  667. int bit_rate;
  668. int bits_per_sample;
  669. switch (ctx->codec_type) {
  670. case AVMEDIA_TYPE_VIDEO:
  671. case AVMEDIA_TYPE_DATA:
  672. case AVMEDIA_TYPE_SUBTITLE:
  673. case AVMEDIA_TYPE_ATTACHMENT:
  674. bit_rate = ctx->bit_rate;
  675. break;
  676. case AVMEDIA_TYPE_AUDIO:
  677. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  678. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  679. break;
  680. default:
  681. bit_rate = 0;
  682. break;
  683. }
  684. return bit_rate;
  685. }
  686. #if FF_API_AVCODEC_OPEN
  687. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  688. {
  689. return avcodec_open2(avctx, codec, NULL);
  690. }
  691. #endif
  692. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  693. {
  694. int ret = 0;
  695. entangled_thread_counter--;
  696. /* Release any user-supplied mutex. */
  697. if (ff_lockmgr_cb) {
  698. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  699. }
  700. ret = avcodec_open2(avctx, codec, options);
  701. /* If there is a user-supplied mutex locking routine, call it. */
  702. if (ff_lockmgr_cb) {
  703. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  704. return -1;
  705. }
  706. entangled_thread_counter++;
  707. return ret;
  708. }
  709. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  710. {
  711. int ret = 0;
  712. AVDictionary *tmp = NULL;
  713. if (avcodec_is_open(avctx))
  714. return 0;
  715. if ((!codec && !avctx->codec)) {
  716. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  717. return AVERROR(EINVAL);
  718. }
  719. if ((codec && avctx->codec && codec != avctx->codec)) {
  720. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  721. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  722. return AVERROR(EINVAL);
  723. }
  724. if (!codec)
  725. codec = avctx->codec;
  726. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  727. return AVERROR(EINVAL);
  728. if (options)
  729. av_dict_copy(&tmp, *options, 0);
  730. /* If there is a user-supplied mutex locking routine, call it. */
  731. if (ff_lockmgr_cb) {
  732. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  733. return -1;
  734. }
  735. entangled_thread_counter++;
  736. if (entangled_thread_counter != 1) {
  737. av_log(avctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  738. ret = -1;
  739. goto end;
  740. }
  741. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  742. if (!avctx->internal) {
  743. ret = AVERROR(ENOMEM);
  744. goto end;
  745. }
  746. if (codec->priv_data_size > 0) {
  747. if (!avctx->priv_data) {
  748. avctx->priv_data = av_mallocz(codec->priv_data_size);
  749. if (!avctx->priv_data) {
  750. ret = AVERROR(ENOMEM);
  751. goto end;
  752. }
  753. if (codec->priv_class) {
  754. *(const AVClass **)avctx->priv_data = codec->priv_class;
  755. av_opt_set_defaults(avctx->priv_data);
  756. }
  757. }
  758. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  759. goto free_and_end;
  760. } else {
  761. avctx->priv_data = NULL;
  762. }
  763. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  764. goto free_and_end;
  765. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  766. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  767. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
  768. ret = -1;
  769. goto free_and_end;
  770. }
  771. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  772. if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
  773. if (avctx->coded_width && avctx->coded_height)
  774. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  775. else if (avctx->width && avctx->height)
  776. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  777. }
  778. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  779. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  780. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  781. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  782. avcodec_set_dimensions(avctx, 0, 0);
  783. }
  784. /* if the decoder init function was already called previously,
  785. * free the already allocated subtitle_header before overwriting it */
  786. if (av_codec_is_decoder(codec))
  787. av_freep(&avctx->subtitle_header);
  788. #define SANE_NB_CHANNELS 128U
  789. if (avctx->channels > SANE_NB_CHANNELS) {
  790. ret = AVERROR(EINVAL);
  791. goto free_and_end;
  792. }
  793. avctx->codec = codec;
  794. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  795. avctx->codec_id == AV_CODEC_ID_NONE) {
  796. avctx->codec_type = codec->type;
  797. avctx->codec_id = codec->id;
  798. }
  799. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  800. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  801. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  802. ret = AVERROR(EINVAL);
  803. goto free_and_end;
  804. }
  805. avctx->frame_number = 0;
  806. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  807. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  808. (!avctx->time_base.num || !avctx->time_base.den)) {
  809. avctx->time_base.num = 1;
  810. avctx->time_base.den = avctx->sample_rate;
  811. }
  812. if (!HAVE_THREADS)
  813. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  814. if (HAVE_THREADS) {
  815. entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  816. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  817. entangled_thread_counter++;
  818. if (ret < 0)
  819. goto free_and_end;
  820. }
  821. if (HAVE_THREADS && !avctx->thread_opaque
  822. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  823. ret = ff_thread_init(avctx);
  824. if (ret < 0) {
  825. goto free_and_end;
  826. }
  827. }
  828. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  829. avctx->thread_count = 1;
  830. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  831. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  832. avctx->codec->max_lowres);
  833. ret = AVERROR(EINVAL);
  834. goto free_and_end;
  835. }
  836. if (av_codec_is_encoder(avctx->codec)) {
  837. int i;
  838. if (avctx->codec->sample_fmts) {
  839. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  840. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  841. break;
  842. if (avctx->channels == 1 &&
  843. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  844. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  845. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  846. break;
  847. }
  848. }
  849. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  850. char buf[128];
  851. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  852. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  853. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  854. ret = AVERROR(EINVAL);
  855. goto free_and_end;
  856. }
  857. }
  858. if (avctx->codec->pix_fmts) {
  859. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  860. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  861. break;
  862. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  863. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  864. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  865. char buf[128];
  866. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  867. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  868. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  869. ret = AVERROR(EINVAL);
  870. goto free_and_end;
  871. }
  872. }
  873. if (avctx->codec->supported_samplerates) {
  874. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  875. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  876. break;
  877. if (avctx->codec->supported_samplerates[i] == 0) {
  878. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  879. avctx->sample_rate);
  880. ret = AVERROR(EINVAL);
  881. goto free_and_end;
  882. }
  883. }
  884. if (avctx->codec->channel_layouts) {
  885. if (!avctx->channel_layout) {
  886. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  887. } else {
  888. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  889. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  890. break;
  891. if (avctx->codec->channel_layouts[i] == 0) {
  892. char buf[512];
  893. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  894. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  895. ret = AVERROR(EINVAL);
  896. goto free_and_end;
  897. }
  898. }
  899. }
  900. if (avctx->channel_layout && avctx->channels) {
  901. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  902. if (channels != avctx->channels) {
  903. char buf[512];
  904. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  905. av_log(avctx, AV_LOG_ERROR,
  906. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  907. buf, channels, avctx->channels);
  908. ret = AVERROR(EINVAL);
  909. goto free_and_end;
  910. }
  911. } else if (avctx->channel_layout) {
  912. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  913. }
  914. }
  915. avctx->pts_correction_num_faulty_pts =
  916. avctx->pts_correction_num_faulty_dts = 0;
  917. avctx->pts_correction_last_pts =
  918. avctx->pts_correction_last_dts = INT64_MIN;
  919. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  920. || avctx->internal->frame_thread_encoder)) {
  921. ret = avctx->codec->init(avctx);
  922. if (ret < 0) {
  923. goto free_and_end;
  924. }
  925. }
  926. ret=0;
  927. if (av_codec_is_decoder(avctx->codec)) {
  928. if (!avctx->bit_rate)
  929. avctx->bit_rate = get_bit_rate(avctx);
  930. /* validate channel layout from the decoder */
  931. if (avctx->channel_layout) {
  932. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  933. if (!avctx->channels)
  934. avctx->channels = channels;
  935. else if (channels != avctx->channels) {
  936. char buf[512];
  937. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  938. av_log(avctx, AV_LOG_WARNING,
  939. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  940. "ignoring specified channel layout\n",
  941. buf, channels, avctx->channels);
  942. avctx->channel_layout = 0;
  943. }
  944. }
  945. }
  946. end:
  947. entangled_thread_counter--;
  948. /* Release any user-supplied mutex. */
  949. if (ff_lockmgr_cb) {
  950. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  951. }
  952. if (options) {
  953. av_dict_free(options);
  954. *options = tmp;
  955. }
  956. return ret;
  957. free_and_end:
  958. av_dict_free(&tmp);
  959. av_freep(&avctx->priv_data);
  960. av_freep(&avctx->internal);
  961. avctx->codec = NULL;
  962. goto end;
  963. }
  964. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  965. {
  966. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  967. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  968. return AVERROR(EINVAL);
  969. }
  970. if (avctx) {
  971. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  972. if (!avpkt->data || avpkt->size < size) {
  973. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  974. avpkt->data = avctx->internal->byte_buffer;
  975. avpkt->size = avctx->internal->byte_buffer_size;
  976. avpkt->destruct = NULL;
  977. }
  978. }
  979. if (avpkt->data) {
  980. void *destruct = avpkt->destruct;
  981. if (avpkt->size < size) {
  982. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  983. return AVERROR(EINVAL);
  984. }
  985. av_init_packet(avpkt);
  986. avpkt->destruct = destruct;
  987. avpkt->size = size;
  988. return 0;
  989. } else {
  990. int ret = av_new_packet(avpkt, size);
  991. if (ret < 0)
  992. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  993. return ret;
  994. }
  995. }
  996. int ff_alloc_packet(AVPacket *avpkt, int size)
  997. {
  998. return ff_alloc_packet2(NULL, avpkt, size);
  999. }
  1000. /**
  1001. * Pad last frame with silence.
  1002. */
  1003. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1004. {
  1005. AVFrame *frame = NULL;
  1006. uint8_t *buf = NULL;
  1007. int ret;
  1008. if (!(frame = avcodec_alloc_frame()))
  1009. return AVERROR(ENOMEM);
  1010. *frame = *src;
  1011. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  1012. s->frame_size, s->sample_fmt, 0)) < 0)
  1013. goto fail;
  1014. if (!(buf = av_malloc(ret))) {
  1015. ret = AVERROR(ENOMEM);
  1016. goto fail;
  1017. }
  1018. frame->nb_samples = s->frame_size;
  1019. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  1020. buf, ret, 0)) < 0)
  1021. goto fail;
  1022. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1023. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1024. goto fail;
  1025. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1026. frame->nb_samples - src->nb_samples,
  1027. s->channels, s->sample_fmt)) < 0)
  1028. goto fail;
  1029. *dst = frame;
  1030. return 0;
  1031. fail:
  1032. if (frame->extended_data != frame->data)
  1033. av_freep(&frame->extended_data);
  1034. av_freep(&buf);
  1035. av_freep(&frame);
  1036. return ret;
  1037. }
  1038. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1039. AVPacket *avpkt,
  1040. const AVFrame *frame,
  1041. int *got_packet_ptr)
  1042. {
  1043. AVFrame tmp;
  1044. AVFrame *padded_frame = NULL;
  1045. int ret;
  1046. AVPacket user_pkt = *avpkt;
  1047. int needs_realloc = !user_pkt.data;
  1048. *got_packet_ptr = 0;
  1049. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1050. av_free_packet(avpkt);
  1051. av_init_packet(avpkt);
  1052. return 0;
  1053. }
  1054. /* ensure that extended_data is properly set */
  1055. if (frame && !frame->extended_data) {
  1056. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1057. avctx->channels > AV_NUM_DATA_POINTERS) {
  1058. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1059. "with more than %d channels, but extended_data is not set.\n",
  1060. AV_NUM_DATA_POINTERS);
  1061. return AVERROR(EINVAL);
  1062. }
  1063. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1064. tmp = *frame;
  1065. tmp.extended_data = tmp.data;
  1066. frame = &tmp;
  1067. }
  1068. /* check for valid frame size */
  1069. if (frame) {
  1070. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1071. if (frame->nb_samples > avctx->frame_size) {
  1072. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1073. return AVERROR(EINVAL);
  1074. }
  1075. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1076. if (frame->nb_samples < avctx->frame_size &&
  1077. !avctx->internal->last_audio_frame) {
  1078. ret = pad_last_frame(avctx, &padded_frame, frame);
  1079. if (ret < 0)
  1080. return ret;
  1081. frame = padded_frame;
  1082. avctx->internal->last_audio_frame = 1;
  1083. }
  1084. if (frame->nb_samples != avctx->frame_size) {
  1085. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1086. ret = AVERROR(EINVAL);
  1087. goto end;
  1088. }
  1089. }
  1090. }
  1091. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1092. if (!ret) {
  1093. if (*got_packet_ptr) {
  1094. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1095. if (avpkt->pts == AV_NOPTS_VALUE)
  1096. avpkt->pts = frame->pts;
  1097. if (!avpkt->duration)
  1098. avpkt->duration = ff_samples_to_time_base(avctx,
  1099. frame->nb_samples);
  1100. }
  1101. avpkt->dts = avpkt->pts;
  1102. } else {
  1103. avpkt->size = 0;
  1104. }
  1105. }
  1106. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1107. needs_realloc = 0;
  1108. if (user_pkt.data) {
  1109. if (user_pkt.size >= avpkt->size) {
  1110. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1111. } else {
  1112. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1113. avpkt->size = user_pkt.size;
  1114. ret = -1;
  1115. }
  1116. avpkt->data = user_pkt.data;
  1117. avpkt->destruct = user_pkt.destruct;
  1118. } else {
  1119. if (av_dup_packet(avpkt) < 0) {
  1120. ret = AVERROR(ENOMEM);
  1121. }
  1122. }
  1123. }
  1124. if (!ret) {
  1125. if (needs_realloc && avpkt->data) {
  1126. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1127. if (new_data)
  1128. avpkt->data = new_data;
  1129. }
  1130. avctx->frame_number++;
  1131. }
  1132. if (ret < 0 || !*got_packet_ptr) {
  1133. av_free_packet(avpkt);
  1134. av_init_packet(avpkt);
  1135. goto end;
  1136. }
  1137. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1138. * this needs to be moved to the encoders, but for now we can do it
  1139. * here to simplify things */
  1140. avpkt->flags |= AV_PKT_FLAG_KEY;
  1141. end:
  1142. if (padded_frame) {
  1143. av_freep(&padded_frame->data[0]);
  1144. if (padded_frame->extended_data != padded_frame->data)
  1145. av_freep(&padded_frame->extended_data);
  1146. av_freep(&padded_frame);
  1147. }
  1148. return ret;
  1149. }
  1150. #if FF_API_OLD_DECODE_AUDIO
  1151. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1152. uint8_t *buf, int buf_size,
  1153. const short *samples)
  1154. {
  1155. AVPacket pkt;
  1156. AVFrame frame0 = { 0 };
  1157. AVFrame *frame;
  1158. int ret, samples_size, got_packet;
  1159. av_init_packet(&pkt);
  1160. pkt.data = buf;
  1161. pkt.size = buf_size;
  1162. if (samples) {
  1163. frame = &frame0;
  1164. avcodec_get_frame_defaults(frame);
  1165. if (avctx->frame_size) {
  1166. frame->nb_samples = avctx->frame_size;
  1167. } else {
  1168. /* if frame_size is not set, the number of samples must be
  1169. * calculated from the buffer size */
  1170. int64_t nb_samples;
  1171. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1172. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1173. "support this codec\n");
  1174. return AVERROR(EINVAL);
  1175. }
  1176. nb_samples = (int64_t)buf_size * 8 /
  1177. (av_get_bits_per_sample(avctx->codec_id) *
  1178. avctx->channels);
  1179. if (nb_samples >= INT_MAX)
  1180. return AVERROR(EINVAL);
  1181. frame->nb_samples = nb_samples;
  1182. }
  1183. /* it is assumed that the samples buffer is large enough based on the
  1184. * relevant parameters */
  1185. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1186. frame->nb_samples,
  1187. avctx->sample_fmt, 1);
  1188. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1189. avctx->sample_fmt,
  1190. (const uint8_t *)samples,
  1191. samples_size, 1)))
  1192. return ret;
  1193. /* fabricate frame pts from sample count.
  1194. * this is needed because the avcodec_encode_audio() API does not have
  1195. * a way for the user to provide pts */
  1196. if (avctx->sample_rate && avctx->time_base.num)
  1197. frame->pts = ff_samples_to_time_base(avctx,
  1198. avctx->internal->sample_count);
  1199. else
  1200. frame->pts = AV_NOPTS_VALUE;
  1201. avctx->internal->sample_count += frame->nb_samples;
  1202. } else {
  1203. frame = NULL;
  1204. }
  1205. got_packet = 0;
  1206. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1207. if (!ret && got_packet && avctx->coded_frame) {
  1208. avctx->coded_frame->pts = pkt.pts;
  1209. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1210. }
  1211. /* free any side data since we cannot return it */
  1212. ff_packet_free_side_data(&pkt);
  1213. if (frame && frame->extended_data != frame->data)
  1214. av_freep(&frame->extended_data);
  1215. return ret ? ret : pkt.size;
  1216. }
  1217. #endif
  1218. #if FF_API_OLD_ENCODE_VIDEO
  1219. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1220. const AVFrame *pict)
  1221. {
  1222. AVPacket pkt;
  1223. int ret, got_packet = 0;
  1224. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1225. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1226. return -1;
  1227. }
  1228. av_init_packet(&pkt);
  1229. pkt.data = buf;
  1230. pkt.size = buf_size;
  1231. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1232. if (!ret && got_packet && avctx->coded_frame) {
  1233. avctx->coded_frame->pts = pkt.pts;
  1234. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1235. }
  1236. /* free any side data since we cannot return it */
  1237. if (pkt.side_data_elems > 0) {
  1238. int i;
  1239. for (i = 0; i < pkt.side_data_elems; i++)
  1240. av_free(pkt.side_data[i].data);
  1241. av_freep(&pkt.side_data);
  1242. pkt.side_data_elems = 0;
  1243. }
  1244. return ret ? ret : pkt.size;
  1245. }
  1246. #endif
  1247. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1248. AVPacket *avpkt,
  1249. const AVFrame *frame,
  1250. int *got_packet_ptr)
  1251. {
  1252. int ret;
  1253. AVPacket user_pkt = *avpkt;
  1254. int needs_realloc = !user_pkt.data;
  1255. *got_packet_ptr = 0;
  1256. if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1257. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1258. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1259. av_free_packet(avpkt);
  1260. av_init_packet(avpkt);
  1261. avpkt->size = 0;
  1262. return 0;
  1263. }
  1264. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1265. return AVERROR(EINVAL);
  1266. av_assert0(avctx->codec->encode2);
  1267. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1268. av_assert0(ret <= 0);
  1269. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1270. needs_realloc = 0;
  1271. if (user_pkt.data) {
  1272. if (user_pkt.size >= avpkt->size) {
  1273. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1274. } else {
  1275. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1276. avpkt->size = user_pkt.size;
  1277. ret = -1;
  1278. }
  1279. avpkt->data = user_pkt.data;
  1280. avpkt->destruct = user_pkt.destruct;
  1281. } else {
  1282. if (av_dup_packet(avpkt) < 0) {
  1283. ret = AVERROR(ENOMEM);
  1284. }
  1285. }
  1286. }
  1287. if (!ret) {
  1288. if (!*got_packet_ptr)
  1289. avpkt->size = 0;
  1290. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1291. avpkt->pts = avpkt->dts = frame->pts;
  1292. if (needs_realloc && avpkt->data &&
  1293. avpkt->destruct == av_destruct_packet) {
  1294. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1295. if (new_data)
  1296. avpkt->data = new_data;
  1297. }
  1298. avctx->frame_number++;
  1299. }
  1300. if (ret < 0 || !*got_packet_ptr)
  1301. av_free_packet(avpkt);
  1302. emms_c();
  1303. return ret;
  1304. }
  1305. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1306. const AVSubtitle *sub)
  1307. {
  1308. int ret;
  1309. if (sub->start_display_time) {
  1310. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1311. return -1;
  1312. }
  1313. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1314. avctx->frame_number++;
  1315. return ret;
  1316. }
  1317. /**
  1318. * Attempt to guess proper monotonic timestamps for decoded video frames
  1319. * which might have incorrect times. Input timestamps may wrap around, in
  1320. * which case the output will as well.
  1321. *
  1322. * @param pts the pts field of the decoded AVPacket, as passed through
  1323. * AVFrame.pkt_pts
  1324. * @param dts the dts field of the decoded AVPacket
  1325. * @return one of the input values, may be AV_NOPTS_VALUE
  1326. */
  1327. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1328. int64_t reordered_pts, int64_t dts)
  1329. {
  1330. int64_t pts = AV_NOPTS_VALUE;
  1331. if (dts != AV_NOPTS_VALUE) {
  1332. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1333. ctx->pts_correction_last_dts = dts;
  1334. }
  1335. if (reordered_pts != AV_NOPTS_VALUE) {
  1336. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1337. ctx->pts_correction_last_pts = reordered_pts;
  1338. }
  1339. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1340. && reordered_pts != AV_NOPTS_VALUE)
  1341. pts = reordered_pts;
  1342. else
  1343. pts = dts;
  1344. return pts;
  1345. }
  1346. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1347. {
  1348. int size = 0;
  1349. const uint8_t *data;
  1350. uint32_t flags;
  1351. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1352. return;
  1353. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1354. if (!data || size < 4)
  1355. return;
  1356. flags = bytestream_get_le32(&data);
  1357. size -= 4;
  1358. if (size < 4) /* Required for any of the changes */
  1359. return;
  1360. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1361. avctx->channels = bytestream_get_le32(&data);
  1362. size -= 4;
  1363. }
  1364. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1365. if (size < 8)
  1366. return;
  1367. avctx->channel_layout = bytestream_get_le64(&data);
  1368. size -= 8;
  1369. }
  1370. if (size < 4)
  1371. return;
  1372. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1373. avctx->sample_rate = bytestream_get_le32(&data);
  1374. size -= 4;
  1375. }
  1376. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1377. if (size < 8)
  1378. return;
  1379. avctx->width = bytestream_get_le32(&data);
  1380. avctx->height = bytestream_get_le32(&data);
  1381. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1382. size -= 8;
  1383. }
  1384. }
  1385. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1386. {
  1387. int size, ret = 0;
  1388. const uint8_t *side_metadata;
  1389. const uint8_t *end;
  1390. av_dict_free(&avctx->metadata);
  1391. side_metadata = av_packet_get_side_data(avctx->pkt,
  1392. AV_PKT_DATA_STRINGS_METADATA, &size);
  1393. if (!side_metadata)
  1394. goto end;
  1395. end = side_metadata + size;
  1396. while (side_metadata < end) {
  1397. const uint8_t *key = side_metadata;
  1398. const uint8_t *val = side_metadata + strlen(key) + 1;
  1399. int ret = av_dict_set(&frame->metadata, key, val, 0);
  1400. if (ret < 0)
  1401. break;
  1402. side_metadata = val + strlen(val) + 1;
  1403. }
  1404. end:
  1405. avctx->metadata = frame->metadata;
  1406. return ret;
  1407. }
  1408. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1409. int *got_picture_ptr,
  1410. const AVPacket *avpkt)
  1411. {
  1412. int ret;
  1413. // copy to ensure we do not change avpkt
  1414. AVPacket tmp = *avpkt;
  1415. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1416. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1417. return AVERROR(EINVAL);
  1418. }
  1419. *got_picture_ptr = 0;
  1420. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1421. return AVERROR(EINVAL);
  1422. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1423. int did_split = av_packet_split_side_data(&tmp);
  1424. apply_param_change(avctx, &tmp);
  1425. avctx->pkt = &tmp;
  1426. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1427. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1428. &tmp);
  1429. else {
  1430. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1431. &tmp);
  1432. picture->pkt_dts = avpkt->dts;
  1433. if(!avctx->has_b_frames){
  1434. picture->pkt_pos = avpkt->pos;
  1435. }
  1436. //FIXME these should be under if(!avctx->has_b_frames)
  1437. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1438. if (!picture->width) picture->width = avctx->width;
  1439. if (!picture->height) picture->height = avctx->height;
  1440. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1441. }
  1442. add_metadata_from_side_data(avctx, picture);
  1443. emms_c(); //needed to avoid an emms_c() call before every return;
  1444. avctx->pkt = NULL;
  1445. if (did_split) {
  1446. ff_packet_free_side_data(&tmp);
  1447. if(ret == tmp.size)
  1448. ret = avpkt->size;
  1449. }
  1450. if (*got_picture_ptr){
  1451. avctx->frame_number++;
  1452. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1453. picture->pkt_pts,
  1454. picture->pkt_dts);
  1455. }
  1456. } else
  1457. ret = 0;
  1458. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1459. * make sure it's set correctly */
  1460. picture->extended_data = picture->data;
  1461. return ret;
  1462. }
  1463. #if FF_API_OLD_DECODE_AUDIO
  1464. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1465. int *frame_size_ptr,
  1466. AVPacket *avpkt)
  1467. {
  1468. AVFrame frame;
  1469. int ret, got_frame = 0;
  1470. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1471. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1472. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1473. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1474. "avcodec_decode_audio4()\n");
  1475. avctx->get_buffer = avcodec_default_get_buffer;
  1476. avctx->release_buffer = avcodec_default_release_buffer;
  1477. }
  1478. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1479. if (ret >= 0 && got_frame) {
  1480. int ch, plane_size;
  1481. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1482. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1483. frame.nb_samples,
  1484. avctx->sample_fmt, 1);
  1485. if (*frame_size_ptr < data_size) {
  1486. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1487. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1488. return AVERROR(EINVAL);
  1489. }
  1490. memcpy(samples, frame.extended_data[0], plane_size);
  1491. if (planar && avctx->channels > 1) {
  1492. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1493. for (ch = 1; ch < avctx->channels; ch++) {
  1494. memcpy(out, frame.extended_data[ch], plane_size);
  1495. out += plane_size;
  1496. }
  1497. }
  1498. *frame_size_ptr = data_size;
  1499. } else {
  1500. *frame_size_ptr = 0;
  1501. }
  1502. return ret;
  1503. }
  1504. #endif
  1505. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1506. AVFrame *frame,
  1507. int *got_frame_ptr,
  1508. const AVPacket *avpkt)
  1509. {
  1510. int planar, channels;
  1511. int ret = 0;
  1512. *got_frame_ptr = 0;
  1513. if (!avpkt->data && avpkt->size) {
  1514. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1515. return AVERROR(EINVAL);
  1516. }
  1517. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1518. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1519. return AVERROR(EINVAL);
  1520. }
  1521. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1522. uint8_t *side;
  1523. int side_size;
  1524. // copy to ensure we do not change avpkt
  1525. AVPacket tmp = *avpkt;
  1526. int did_split = av_packet_split_side_data(&tmp);
  1527. apply_param_change(avctx, &tmp);
  1528. avctx->pkt = &tmp;
  1529. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1530. if (ret >= 0 && *got_frame_ptr) {
  1531. avctx->frame_number++;
  1532. frame->pkt_dts = avpkt->dts;
  1533. frame->best_effort_timestamp = guess_correct_pts(avctx,
  1534. frame->pkt_pts,
  1535. frame->pkt_dts);
  1536. if (frame->format == AV_SAMPLE_FMT_NONE)
  1537. frame->format = avctx->sample_fmt;
  1538. if (!frame->channel_layout)
  1539. frame->channel_layout = avctx->channel_layout;
  1540. if (!frame->channels)
  1541. frame->channels = avctx->channels;
  1542. if (!frame->sample_rate)
  1543. frame->sample_rate = avctx->sample_rate;
  1544. }
  1545. add_metadata_from_side_data(avctx, frame);
  1546. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1547. if(side && side_size>=10) {
  1548. avctx->internal->skip_samples = AV_RL32(side);
  1549. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1550. avctx->internal->skip_samples);
  1551. }
  1552. if (avctx->internal->skip_samples) {
  1553. if(frame->nb_samples <= avctx->internal->skip_samples){
  1554. *got_frame_ptr = 0;
  1555. avctx->internal->skip_samples -= frame->nb_samples;
  1556. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1557. avctx->internal->skip_samples);
  1558. } else {
  1559. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1560. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1561. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1562. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1563. (AVRational){1, avctx->sample_rate},
  1564. avctx->pkt_timebase);
  1565. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1566. frame->pkt_pts += diff_ts;
  1567. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1568. frame->pkt_dts += diff_ts;
  1569. if (frame->pkt_duration >= diff_ts)
  1570. frame->pkt_duration -= diff_ts;
  1571. } else {
  1572. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1573. }
  1574. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1575. avctx->internal->skip_samples, frame->nb_samples);
  1576. frame->nb_samples -= avctx->internal->skip_samples;
  1577. avctx->internal->skip_samples = 0;
  1578. }
  1579. }
  1580. avctx->pkt = NULL;
  1581. if (did_split) {
  1582. ff_packet_free_side_data(&tmp);
  1583. if(ret == tmp.size)
  1584. ret = avpkt->size;
  1585. }
  1586. }
  1587. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1588. * make sure it's set correctly; assume decoders that actually use
  1589. * extended_data are doing it correctly */
  1590. if (*got_frame_ptr) {
  1591. planar = av_sample_fmt_is_planar(frame->format);
  1592. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1593. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1594. frame->extended_data = frame->data;
  1595. } else {
  1596. frame->extended_data = NULL;
  1597. }
  1598. return ret;
  1599. }
  1600. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1601. int *got_sub_ptr,
  1602. AVPacket *avpkt)
  1603. {
  1604. int ret;
  1605. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1606. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1607. return AVERROR(EINVAL);
  1608. }
  1609. avctx->pkt = avpkt;
  1610. *got_sub_ptr = 0;
  1611. avcodec_get_subtitle_defaults(sub);
  1612. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1613. sub->pts = av_rescale_q(avpkt->pts,
  1614. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1615. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1616. if (*got_sub_ptr)
  1617. avctx->frame_number++;
  1618. return ret;
  1619. }
  1620. void avsubtitle_free(AVSubtitle *sub)
  1621. {
  1622. int i;
  1623. for (i = 0; i < sub->num_rects; i++) {
  1624. av_freep(&sub->rects[i]->pict.data[0]);
  1625. av_freep(&sub->rects[i]->pict.data[1]);
  1626. av_freep(&sub->rects[i]->pict.data[2]);
  1627. av_freep(&sub->rects[i]->pict.data[3]);
  1628. av_freep(&sub->rects[i]->text);
  1629. av_freep(&sub->rects[i]->ass);
  1630. av_freep(&sub->rects[i]);
  1631. }
  1632. av_freep(&sub->rects);
  1633. memset(sub, 0, sizeof(AVSubtitle));
  1634. }
  1635. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  1636. {
  1637. int ret = 0;
  1638. entangled_thread_counter--;
  1639. /* Release any user-supplied mutex. */
  1640. if (ff_lockmgr_cb) {
  1641. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1642. }
  1643. ret = avcodec_close(avctx);
  1644. /* If there is a user-supplied mutex locking routine, call it. */
  1645. if (ff_lockmgr_cb) {
  1646. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1647. return -1;
  1648. }
  1649. entangled_thread_counter++;
  1650. return ret;
  1651. }
  1652. av_cold int avcodec_close(AVCodecContext *avctx)
  1653. {
  1654. /* If there is a user-supplied mutex locking routine, call it. */
  1655. if (ff_lockmgr_cb) {
  1656. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1657. return -1;
  1658. }
  1659. entangled_thread_counter++;
  1660. if (entangled_thread_counter != 1) {
  1661. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1662. entangled_thread_counter--;
  1663. return -1;
  1664. }
  1665. if (avcodec_is_open(avctx)) {
  1666. if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1667. entangled_thread_counter --;
  1668. ff_frame_thread_encoder_free(avctx);
  1669. entangled_thread_counter ++;
  1670. }
  1671. if (HAVE_THREADS && avctx->thread_opaque)
  1672. ff_thread_free(avctx);
  1673. if (avctx->codec && avctx->codec->close)
  1674. avctx->codec->close(avctx);
  1675. avcodec_default_free_buffers(avctx);
  1676. avctx->coded_frame = NULL;
  1677. avctx->internal->byte_buffer_size = 0;
  1678. av_freep(&avctx->internal->byte_buffer);
  1679. av_freep(&avctx->internal);
  1680. av_dict_free(&avctx->metadata);
  1681. }
  1682. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1683. av_opt_free(avctx->priv_data);
  1684. av_opt_free(avctx);
  1685. av_freep(&avctx->priv_data);
  1686. if (av_codec_is_encoder(avctx->codec))
  1687. av_freep(&avctx->extradata);
  1688. avctx->codec = NULL;
  1689. avctx->active_thread_type = 0;
  1690. entangled_thread_counter--;
  1691. /* Release any user-supplied mutex. */
  1692. if (ff_lockmgr_cb) {
  1693. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1694. }
  1695. return 0;
  1696. }
  1697. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  1698. {
  1699. switch(id){
  1700. //This is for future deprecatec codec ids, its empty since
  1701. //last major bump but will fill up again over time, please don't remove it
  1702. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  1703. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  1704. default : return id;
  1705. }
  1706. }
  1707. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1708. {
  1709. AVCodec *p, *experimental = NULL;
  1710. p = first_avcodec;
  1711. id= remap_deprecated_codec_id(id);
  1712. while (p) {
  1713. if (av_codec_is_encoder(p) && p->id == id) {
  1714. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1715. experimental = p;
  1716. } else
  1717. return p;
  1718. }
  1719. p = p->next;
  1720. }
  1721. return experimental;
  1722. }
  1723. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1724. {
  1725. AVCodec *p;
  1726. if (!name)
  1727. return NULL;
  1728. p = first_avcodec;
  1729. while (p) {
  1730. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1731. return p;
  1732. p = p->next;
  1733. }
  1734. return NULL;
  1735. }
  1736. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1737. {
  1738. AVCodec *p, *experimental=NULL;
  1739. p = first_avcodec;
  1740. id= remap_deprecated_codec_id(id);
  1741. while (p) {
  1742. if (av_codec_is_decoder(p) && p->id == id) {
  1743. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1744. experimental = p;
  1745. } else
  1746. return p;
  1747. }
  1748. p = p->next;
  1749. }
  1750. return experimental;
  1751. }
  1752. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1753. {
  1754. AVCodec *p;
  1755. if (!name)
  1756. return NULL;
  1757. p = first_avcodec;
  1758. while (p) {
  1759. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1760. return p;
  1761. p = p->next;
  1762. }
  1763. return NULL;
  1764. }
  1765. const char *avcodec_get_name(enum AVCodecID id)
  1766. {
  1767. const AVCodecDescriptor *cd;
  1768. AVCodec *codec;
  1769. if (id == AV_CODEC_ID_NONE)
  1770. return "none";
  1771. cd = avcodec_descriptor_get(id);
  1772. if (cd)
  1773. return cd->name;
  1774. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1775. codec = avcodec_find_decoder(id);
  1776. if (codec)
  1777. return codec->name;
  1778. codec = avcodec_find_encoder(id);
  1779. if (codec)
  1780. return codec->name;
  1781. return "unknown_codec";
  1782. }
  1783. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1784. {
  1785. int i, len, ret = 0;
  1786. #define IS_PRINT(x) \
  1787. (((x) >= '0' && (x) <= '9') || \
  1788. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1789. ((x) == '.' || (x) == ' ' || (x) == '-'))
  1790. for (i = 0; i < 4; i++) {
  1791. len = snprintf(buf, buf_size,
  1792. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1793. buf += len;
  1794. buf_size = buf_size > len ? buf_size - len : 0;
  1795. ret += len;
  1796. codec_tag >>= 8;
  1797. }
  1798. return ret;
  1799. }
  1800. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1801. {
  1802. const char *codec_type;
  1803. const char *codec_name;
  1804. const char *profile = NULL;
  1805. const AVCodec *p;
  1806. int bitrate;
  1807. AVRational display_aspect_ratio;
  1808. if (!buf || buf_size <= 0)
  1809. return;
  1810. codec_type = av_get_media_type_string(enc->codec_type);
  1811. codec_name = avcodec_get_name(enc->codec_id);
  1812. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1813. if (enc->codec)
  1814. p = enc->codec;
  1815. else
  1816. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1817. avcodec_find_decoder(enc->codec_id);
  1818. if (p)
  1819. profile = av_get_profile_name(p, enc->profile);
  1820. }
  1821. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1822. codec_name, enc->mb_decision ? " (hq)" : "");
  1823. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1824. if (profile)
  1825. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1826. if (enc->codec_tag) {
  1827. char tag_buf[32];
  1828. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1829. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1830. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1831. }
  1832. switch (enc->codec_type) {
  1833. case AVMEDIA_TYPE_VIDEO:
  1834. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1835. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1836. ", %s",
  1837. av_get_pix_fmt_name(enc->pix_fmt));
  1838. }
  1839. if (enc->width) {
  1840. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1841. ", %dx%d",
  1842. enc->width, enc->height);
  1843. if (enc->sample_aspect_ratio.num) {
  1844. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1845. enc->width * enc->sample_aspect_ratio.num,
  1846. enc->height * enc->sample_aspect_ratio.den,
  1847. 1024 * 1024);
  1848. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1849. " [SAR %d:%d DAR %d:%d]",
  1850. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1851. display_aspect_ratio.num, display_aspect_ratio.den);
  1852. }
  1853. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1854. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1855. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1856. ", %d/%d",
  1857. enc->time_base.num / g, enc->time_base.den / g);
  1858. }
  1859. }
  1860. if (encode) {
  1861. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1862. ", q=%d-%d", enc->qmin, enc->qmax);
  1863. }
  1864. break;
  1865. case AVMEDIA_TYPE_AUDIO:
  1866. if (enc->sample_rate) {
  1867. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1868. ", %d Hz", enc->sample_rate);
  1869. }
  1870. av_strlcat(buf, ", ", buf_size);
  1871. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1872. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1873. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1874. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1875. }
  1876. break;
  1877. default:
  1878. return;
  1879. }
  1880. if (encode) {
  1881. if (enc->flags & CODEC_FLAG_PASS1)
  1882. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1883. ", pass 1");
  1884. if (enc->flags & CODEC_FLAG_PASS2)
  1885. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1886. ", pass 2");
  1887. }
  1888. bitrate = get_bit_rate(enc);
  1889. if (bitrate != 0) {
  1890. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1891. ", %d kb/s", bitrate / 1000);
  1892. }
  1893. }
  1894. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1895. {
  1896. const AVProfile *p;
  1897. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1898. return NULL;
  1899. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1900. if (p->profile == profile)
  1901. return p->name;
  1902. return NULL;
  1903. }
  1904. unsigned avcodec_version(void)
  1905. {
  1906. // av_assert0(AV_CODEC_ID_V410==164);
  1907. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1908. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1909. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  1910. av_assert0(AV_CODEC_ID_SRT==94216);
  1911. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1912. return LIBAVCODEC_VERSION_INT;
  1913. }
  1914. const char *avcodec_configuration(void)
  1915. {
  1916. return FFMPEG_CONFIGURATION;
  1917. }
  1918. const char *avcodec_license(void)
  1919. {
  1920. #define LICENSE_PREFIX "libavcodec license: "
  1921. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1922. }
  1923. void avcodec_flush_buffers(AVCodecContext *avctx)
  1924. {
  1925. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1926. ff_thread_flush(avctx);
  1927. else if (avctx->codec->flush)
  1928. avctx->codec->flush(avctx);
  1929. avctx->pts_correction_last_pts =
  1930. avctx->pts_correction_last_dts = INT64_MIN;
  1931. }
  1932. static void video_free_buffers(AVCodecContext *s)
  1933. {
  1934. AVCodecInternal *avci = s->internal;
  1935. int i, j;
  1936. if (!avci->buffer)
  1937. return;
  1938. if (avci->buffer_count)
  1939. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1940. avci->buffer_count);
  1941. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1942. InternalBuffer *buf = &avci->buffer[i];
  1943. for (j = 0; j < 4; j++) {
  1944. av_freep(&buf->base[j]);
  1945. buf->data[j] = NULL;
  1946. }
  1947. }
  1948. av_freep(&avci->buffer);
  1949. avci->buffer_count = 0;
  1950. }
  1951. static void audio_free_buffers(AVCodecContext *avctx)
  1952. {
  1953. AVCodecInternal *avci = avctx->internal;
  1954. InternalBuffer *buf;
  1955. if (!avci->buffer)
  1956. return;
  1957. buf = avci->buffer;
  1958. if (buf->extended_data) {
  1959. av_free(buf->extended_data[0]);
  1960. if (buf->extended_data != buf->data)
  1961. av_freep(&buf->extended_data);
  1962. }
  1963. av_freep(&avci->buffer);
  1964. }
  1965. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1966. {
  1967. switch (avctx->codec_type) {
  1968. case AVMEDIA_TYPE_VIDEO:
  1969. video_free_buffers(avctx);
  1970. break;
  1971. case AVMEDIA_TYPE_AUDIO:
  1972. audio_free_buffers(avctx);
  1973. break;
  1974. default:
  1975. break;
  1976. }
  1977. }
  1978. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1979. {
  1980. switch (codec_id) {
  1981. case AV_CODEC_ID_ADPCM_CT:
  1982. case AV_CODEC_ID_ADPCM_IMA_APC:
  1983. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1984. case AV_CODEC_ID_ADPCM_IMA_WS:
  1985. case AV_CODEC_ID_ADPCM_G722:
  1986. case AV_CODEC_ID_ADPCM_YAMAHA:
  1987. return 4;
  1988. case AV_CODEC_ID_PCM_ALAW:
  1989. case AV_CODEC_ID_PCM_MULAW:
  1990. case AV_CODEC_ID_PCM_S8:
  1991. case AV_CODEC_ID_PCM_U8:
  1992. case AV_CODEC_ID_PCM_ZORK:
  1993. return 8;
  1994. case AV_CODEC_ID_PCM_S16BE:
  1995. case AV_CODEC_ID_PCM_S16LE:
  1996. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1997. case AV_CODEC_ID_PCM_U16BE:
  1998. case AV_CODEC_ID_PCM_U16LE:
  1999. return 16;
  2000. case AV_CODEC_ID_PCM_S24DAUD:
  2001. case AV_CODEC_ID_PCM_S24BE:
  2002. case AV_CODEC_ID_PCM_S24LE:
  2003. case AV_CODEC_ID_PCM_U24BE:
  2004. case AV_CODEC_ID_PCM_U24LE:
  2005. return 24;
  2006. case AV_CODEC_ID_PCM_S32BE:
  2007. case AV_CODEC_ID_PCM_S32LE:
  2008. case AV_CODEC_ID_PCM_U32BE:
  2009. case AV_CODEC_ID_PCM_U32LE:
  2010. case AV_CODEC_ID_PCM_F32BE:
  2011. case AV_CODEC_ID_PCM_F32LE:
  2012. return 32;
  2013. case AV_CODEC_ID_PCM_F64BE:
  2014. case AV_CODEC_ID_PCM_F64LE:
  2015. return 64;
  2016. default:
  2017. return 0;
  2018. }
  2019. }
  2020. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2021. {
  2022. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2023. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2024. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2025. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2026. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2027. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2028. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2029. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2030. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2031. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2032. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2033. };
  2034. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2035. return AV_CODEC_ID_NONE;
  2036. if (be < 0 || be > 1)
  2037. be = AV_NE(1, 0);
  2038. return map[fmt][be];
  2039. }
  2040. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2041. {
  2042. switch (codec_id) {
  2043. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2044. return 2;
  2045. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2046. return 3;
  2047. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2048. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2049. case AV_CODEC_ID_ADPCM_IMA_QT:
  2050. case AV_CODEC_ID_ADPCM_SWF:
  2051. case AV_CODEC_ID_ADPCM_MS:
  2052. return 4;
  2053. default:
  2054. return av_get_exact_bits_per_sample(codec_id);
  2055. }
  2056. }
  2057. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2058. {
  2059. int id, sr, ch, ba, tag, bps;
  2060. id = avctx->codec_id;
  2061. sr = avctx->sample_rate;
  2062. ch = avctx->channels;
  2063. ba = avctx->block_align;
  2064. tag = avctx->codec_tag;
  2065. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2066. /* codecs with an exact constant bits per sample */
  2067. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2068. return (frame_bytes * 8LL) / (bps * ch);
  2069. bps = avctx->bits_per_coded_sample;
  2070. /* codecs with a fixed packet duration */
  2071. switch (id) {
  2072. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2073. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2074. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2075. case AV_CODEC_ID_AMR_NB:
  2076. case AV_CODEC_ID_GSM:
  2077. case AV_CODEC_ID_QCELP:
  2078. case AV_CODEC_ID_RA_288: return 160;
  2079. case AV_CODEC_ID_IMC: return 256;
  2080. case AV_CODEC_ID_AMR_WB:
  2081. case AV_CODEC_ID_GSM_MS: return 320;
  2082. case AV_CODEC_ID_MP1: return 384;
  2083. case AV_CODEC_ID_ATRAC1: return 512;
  2084. case AV_CODEC_ID_ATRAC3: return 1024;
  2085. case AV_CODEC_ID_MP2:
  2086. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2087. case AV_CODEC_ID_AC3: return 1536;
  2088. }
  2089. if (sr > 0) {
  2090. /* calc from sample rate */
  2091. if (id == AV_CODEC_ID_TTA)
  2092. return 256 * sr / 245;
  2093. if (ch > 0) {
  2094. /* calc from sample rate and channels */
  2095. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2096. return (480 << (sr / 22050)) / ch;
  2097. }
  2098. }
  2099. if (ba > 0) {
  2100. /* calc from block_align */
  2101. if (id == AV_CODEC_ID_SIPR) {
  2102. switch (ba) {
  2103. case 20: return 160;
  2104. case 19: return 144;
  2105. case 29: return 288;
  2106. case 37: return 480;
  2107. }
  2108. } else if (id == AV_CODEC_ID_ILBC) {
  2109. switch (ba) {
  2110. case 38: return 160;
  2111. case 50: return 240;
  2112. }
  2113. }
  2114. }
  2115. if (frame_bytes > 0) {
  2116. /* calc from frame_bytes only */
  2117. if (id == AV_CODEC_ID_TRUESPEECH)
  2118. return 240 * (frame_bytes / 32);
  2119. if (id == AV_CODEC_ID_NELLYMOSER)
  2120. return 256 * (frame_bytes / 64);
  2121. if (id == AV_CODEC_ID_RA_144)
  2122. return 160 * (frame_bytes / 20);
  2123. if (bps > 0) {
  2124. /* calc from frame_bytes and bits_per_coded_sample */
  2125. if (id == AV_CODEC_ID_ADPCM_G726)
  2126. return frame_bytes * 8 / bps;
  2127. }
  2128. if (ch > 0) {
  2129. /* calc from frame_bytes and channels */
  2130. switch (id) {
  2131. case AV_CODEC_ID_ADPCM_4XM:
  2132. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2133. return (frame_bytes - 4 * ch) * 2 / ch;
  2134. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2135. return (frame_bytes - 4) * 2 / ch;
  2136. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2137. return (frame_bytes - 8) * 2 / ch;
  2138. case AV_CODEC_ID_ADPCM_XA:
  2139. return (frame_bytes / 128) * 224 / ch;
  2140. case AV_CODEC_ID_INTERPLAY_DPCM:
  2141. return (frame_bytes - 6 - ch) / ch;
  2142. case AV_CODEC_ID_ROQ_DPCM:
  2143. return (frame_bytes - 8) / ch;
  2144. case AV_CODEC_ID_XAN_DPCM:
  2145. return (frame_bytes - 2 * ch) / ch;
  2146. case AV_CODEC_ID_MACE3:
  2147. return 3 * frame_bytes / ch;
  2148. case AV_CODEC_ID_MACE6:
  2149. return 6 * frame_bytes / ch;
  2150. case AV_CODEC_ID_PCM_LXF:
  2151. return 2 * (frame_bytes / (5 * ch));
  2152. }
  2153. if (tag) {
  2154. /* calc from frame_bytes, channels, and codec_tag */
  2155. if (id == AV_CODEC_ID_SOL_DPCM) {
  2156. if (tag == 3)
  2157. return frame_bytes / ch;
  2158. else
  2159. return frame_bytes * 2 / ch;
  2160. }
  2161. }
  2162. if (ba > 0) {
  2163. /* calc from frame_bytes, channels, and block_align */
  2164. int blocks = frame_bytes / ba;
  2165. switch (avctx->codec_id) {
  2166. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2167. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2168. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2169. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2170. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2171. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2172. case AV_CODEC_ID_ADPCM_MS:
  2173. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2174. }
  2175. }
  2176. if (bps > 0) {
  2177. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2178. switch (avctx->codec_id) {
  2179. case AV_CODEC_ID_PCM_DVD:
  2180. if(bps<4)
  2181. return 0;
  2182. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2183. case AV_CODEC_ID_PCM_BLURAY:
  2184. if(bps<4)
  2185. return 0;
  2186. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2187. case AV_CODEC_ID_S302M:
  2188. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2189. }
  2190. }
  2191. }
  2192. }
  2193. return 0;
  2194. }
  2195. #if !HAVE_THREADS
  2196. int ff_thread_init(AVCodecContext *s)
  2197. {
  2198. return -1;
  2199. }
  2200. #endif
  2201. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2202. {
  2203. unsigned int n = 0;
  2204. while (v >= 0xff) {
  2205. *s++ = 0xff;
  2206. v -= 0xff;
  2207. n++;
  2208. }
  2209. *s = v;
  2210. n++;
  2211. return n;
  2212. }
  2213. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2214. {
  2215. int i;
  2216. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2217. return i;
  2218. }
  2219. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2220. {
  2221. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2222. "version to the newest one from Git. If the problem still "
  2223. "occurs, it means that your file has a feature which has not "
  2224. "been implemented.\n", feature);
  2225. if(want_sample)
  2226. av_log_ask_for_sample(avc, NULL);
  2227. }
  2228. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2229. {
  2230. va_list argument_list;
  2231. va_start(argument_list, msg);
  2232. if (msg)
  2233. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2234. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2235. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2236. "and contact the ffmpeg-devel mailing list.\n");
  2237. va_end(argument_list);
  2238. }
  2239. static AVHWAccel *first_hwaccel = NULL;
  2240. void av_register_hwaccel(AVHWAccel *hwaccel)
  2241. {
  2242. AVHWAccel **p = &first_hwaccel;
  2243. while (*p)
  2244. p = &(*p)->next;
  2245. *p = hwaccel;
  2246. hwaccel->next = NULL;
  2247. }
  2248. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2249. {
  2250. return hwaccel ? hwaccel->next : first_hwaccel;
  2251. }
  2252. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2253. {
  2254. AVHWAccel *hwaccel = NULL;
  2255. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2256. if (hwaccel->id == codec_id
  2257. && hwaccel->pix_fmt == pix_fmt)
  2258. return hwaccel;
  2259. return NULL;
  2260. }
  2261. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2262. {
  2263. if (ff_lockmgr_cb) {
  2264. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2265. return -1;
  2266. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2267. return -1;
  2268. }
  2269. ff_lockmgr_cb = cb;
  2270. if (ff_lockmgr_cb) {
  2271. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2272. return -1;
  2273. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2274. return -1;
  2275. }
  2276. return 0;
  2277. }
  2278. int avpriv_lock_avformat(void)
  2279. {
  2280. if (ff_lockmgr_cb) {
  2281. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2282. return -1;
  2283. }
  2284. return 0;
  2285. }
  2286. int avpriv_unlock_avformat(void)
  2287. {
  2288. if (ff_lockmgr_cb) {
  2289. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2290. return -1;
  2291. }
  2292. return 0;
  2293. }
  2294. unsigned int avpriv_toupper4(unsigned int x)
  2295. {
  2296. return toupper(x & 0xFF)
  2297. + (toupper((x >> 8) & 0xFF) << 8)
  2298. + (toupper((x >> 16) & 0xFF) << 16)
  2299. + (toupper((x >> 24) & 0xFF) << 24);
  2300. }
  2301. #if !HAVE_THREADS
  2302. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2303. {
  2304. f->owner = avctx;
  2305. ff_init_buffer_info(avctx, f);
  2306. return avctx->get_buffer(avctx, f);
  2307. }
  2308. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2309. {
  2310. f->owner->release_buffer(f->owner, f);
  2311. }
  2312. void ff_thread_finish_setup(AVCodecContext *avctx)
  2313. {
  2314. }
  2315. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2316. {
  2317. }
  2318. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2319. {
  2320. }
  2321. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2322. {
  2323. return 1;
  2324. }
  2325. #endif
  2326. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2327. {
  2328. AVCodec *c= avcodec_find_decoder(codec_id);
  2329. if(!c)
  2330. c= avcodec_find_encoder(codec_id);
  2331. if(c)
  2332. return c->type;
  2333. if (codec_id <= AV_CODEC_ID_NONE)
  2334. return AVMEDIA_TYPE_UNKNOWN;
  2335. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2336. return AVMEDIA_TYPE_VIDEO;
  2337. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2338. return AVMEDIA_TYPE_AUDIO;
  2339. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2340. return AVMEDIA_TYPE_SUBTITLE;
  2341. return AVMEDIA_TYPE_UNKNOWN;
  2342. }
  2343. int avcodec_is_open(AVCodecContext *s)
  2344. {
  2345. return !!s->internal;
  2346. }