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.

2658 lines
85KB

  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,
  768. "Codec %s is experimental but experimental codecs are not enabled: "
  769. "try the \"strict\" option with value \"experimental\"\n",
  770. avctx->codec->name);
  771. ret = -1;
  772. goto free_and_end;
  773. }
  774. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  775. if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
  776. if (avctx->coded_width && avctx->coded_height)
  777. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  778. else if (avctx->width && avctx->height)
  779. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  780. }
  781. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  782. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  783. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  784. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  785. avcodec_set_dimensions(avctx, 0, 0);
  786. }
  787. /* if the decoder init function was already called previously,
  788. * free the already allocated subtitle_header before overwriting it */
  789. if (av_codec_is_decoder(codec))
  790. av_freep(&avctx->subtitle_header);
  791. #define SANE_NB_CHANNELS 128U
  792. if (avctx->channels > SANE_NB_CHANNELS) {
  793. ret = AVERROR(EINVAL);
  794. goto free_and_end;
  795. }
  796. avctx->codec = codec;
  797. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  798. avctx->codec_id == AV_CODEC_ID_NONE) {
  799. avctx->codec_type = codec->type;
  800. avctx->codec_id = codec->id;
  801. }
  802. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  803. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  804. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  805. ret = AVERROR(EINVAL);
  806. goto free_and_end;
  807. }
  808. avctx->frame_number = 0;
  809. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  810. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  811. (!avctx->time_base.num || !avctx->time_base.den)) {
  812. avctx->time_base.num = 1;
  813. avctx->time_base.den = avctx->sample_rate;
  814. }
  815. if (!HAVE_THREADS)
  816. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  817. if (HAVE_THREADS) {
  818. entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  819. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  820. entangled_thread_counter++;
  821. if (ret < 0)
  822. goto free_and_end;
  823. }
  824. if (HAVE_THREADS && !avctx->thread_opaque
  825. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  826. ret = ff_thread_init(avctx);
  827. if (ret < 0) {
  828. goto free_and_end;
  829. }
  830. }
  831. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  832. avctx->thread_count = 1;
  833. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  834. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  835. avctx->codec->max_lowres);
  836. ret = AVERROR(EINVAL);
  837. goto free_and_end;
  838. }
  839. if (av_codec_is_encoder(avctx->codec)) {
  840. int i;
  841. if (avctx->codec->sample_fmts) {
  842. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  843. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  844. break;
  845. if (avctx->channels == 1 &&
  846. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  847. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  848. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  849. break;
  850. }
  851. }
  852. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  853. char buf[128];
  854. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  855. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  856. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  857. ret = AVERROR(EINVAL);
  858. goto free_and_end;
  859. }
  860. }
  861. if (avctx->codec->pix_fmts) {
  862. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  863. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  864. break;
  865. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  866. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  867. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  868. char buf[128];
  869. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  870. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  871. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  872. ret = AVERROR(EINVAL);
  873. goto free_and_end;
  874. }
  875. }
  876. if (avctx->codec->supported_samplerates) {
  877. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  878. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  879. break;
  880. if (avctx->codec->supported_samplerates[i] == 0) {
  881. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  882. avctx->sample_rate);
  883. ret = AVERROR(EINVAL);
  884. goto free_and_end;
  885. }
  886. }
  887. if (avctx->codec->channel_layouts) {
  888. if (!avctx->channel_layout) {
  889. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  890. } else {
  891. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  892. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  893. break;
  894. if (avctx->codec->channel_layouts[i] == 0) {
  895. char buf[512];
  896. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  897. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  898. ret = AVERROR(EINVAL);
  899. goto free_and_end;
  900. }
  901. }
  902. }
  903. if (avctx->channel_layout && avctx->channels) {
  904. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  905. if (channels != avctx->channels) {
  906. char buf[512];
  907. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  908. av_log(avctx, AV_LOG_ERROR,
  909. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  910. buf, channels, avctx->channels);
  911. ret = AVERROR(EINVAL);
  912. goto free_and_end;
  913. }
  914. } else if (avctx->channel_layout) {
  915. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  916. }
  917. }
  918. avctx->pts_correction_num_faulty_pts =
  919. avctx->pts_correction_num_faulty_dts = 0;
  920. avctx->pts_correction_last_pts =
  921. avctx->pts_correction_last_dts = INT64_MIN;
  922. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  923. || avctx->internal->frame_thread_encoder)) {
  924. ret = avctx->codec->init(avctx);
  925. if (ret < 0) {
  926. goto free_and_end;
  927. }
  928. }
  929. ret=0;
  930. if (av_codec_is_decoder(avctx->codec)) {
  931. if (!avctx->bit_rate)
  932. avctx->bit_rate = get_bit_rate(avctx);
  933. /* validate channel layout from the decoder */
  934. if (avctx->channel_layout) {
  935. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  936. if (!avctx->channels)
  937. avctx->channels = channels;
  938. else if (channels != avctx->channels) {
  939. char buf[512];
  940. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  941. av_log(avctx, AV_LOG_WARNING,
  942. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  943. "ignoring specified channel layout\n",
  944. buf, channels, avctx->channels);
  945. avctx->channel_layout = 0;
  946. }
  947. }
  948. }
  949. end:
  950. entangled_thread_counter--;
  951. /* Release any user-supplied mutex. */
  952. if (ff_lockmgr_cb) {
  953. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  954. }
  955. if (options) {
  956. av_dict_free(options);
  957. *options = tmp;
  958. }
  959. return ret;
  960. free_and_end:
  961. av_dict_free(&tmp);
  962. av_freep(&avctx->priv_data);
  963. av_freep(&avctx->internal);
  964. avctx->codec = NULL;
  965. goto end;
  966. }
  967. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  968. {
  969. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  970. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  971. return AVERROR(EINVAL);
  972. }
  973. if (avctx) {
  974. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  975. if (!avpkt->data || avpkt->size < size) {
  976. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  977. avpkt->data = avctx->internal->byte_buffer;
  978. avpkt->size = avctx->internal->byte_buffer_size;
  979. avpkt->destruct = NULL;
  980. }
  981. }
  982. if (avpkt->data) {
  983. void *destruct = avpkt->destruct;
  984. if (avpkt->size < size) {
  985. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  986. return AVERROR(EINVAL);
  987. }
  988. av_init_packet(avpkt);
  989. avpkt->destruct = destruct;
  990. avpkt->size = size;
  991. return 0;
  992. } else {
  993. int ret = av_new_packet(avpkt, size);
  994. if (ret < 0)
  995. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  996. return ret;
  997. }
  998. }
  999. int ff_alloc_packet(AVPacket *avpkt, int size)
  1000. {
  1001. return ff_alloc_packet2(NULL, avpkt, size);
  1002. }
  1003. /**
  1004. * Pad last frame with silence.
  1005. */
  1006. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1007. {
  1008. AVFrame *frame = NULL;
  1009. uint8_t *buf = NULL;
  1010. int ret;
  1011. if (!(frame = avcodec_alloc_frame()))
  1012. return AVERROR(ENOMEM);
  1013. *frame = *src;
  1014. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  1015. s->frame_size, s->sample_fmt, 0)) < 0)
  1016. goto fail;
  1017. if (!(buf = av_malloc(ret))) {
  1018. ret = AVERROR(ENOMEM);
  1019. goto fail;
  1020. }
  1021. frame->nb_samples = s->frame_size;
  1022. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  1023. buf, ret, 0)) < 0)
  1024. goto fail;
  1025. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1026. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1027. goto fail;
  1028. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1029. frame->nb_samples - src->nb_samples,
  1030. s->channels, s->sample_fmt)) < 0)
  1031. goto fail;
  1032. *dst = frame;
  1033. return 0;
  1034. fail:
  1035. if (frame->extended_data != frame->data)
  1036. av_freep(&frame->extended_data);
  1037. av_freep(&buf);
  1038. av_freep(&frame);
  1039. return ret;
  1040. }
  1041. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1042. AVPacket *avpkt,
  1043. const AVFrame *frame,
  1044. int *got_packet_ptr)
  1045. {
  1046. AVFrame tmp;
  1047. AVFrame *padded_frame = NULL;
  1048. int ret;
  1049. AVPacket user_pkt = *avpkt;
  1050. int needs_realloc = !user_pkt.data;
  1051. *got_packet_ptr = 0;
  1052. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1053. av_free_packet(avpkt);
  1054. av_init_packet(avpkt);
  1055. return 0;
  1056. }
  1057. /* ensure that extended_data is properly set */
  1058. if (frame && !frame->extended_data) {
  1059. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1060. avctx->channels > AV_NUM_DATA_POINTERS) {
  1061. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1062. "with more than %d channels, but extended_data is not set.\n",
  1063. AV_NUM_DATA_POINTERS);
  1064. return AVERROR(EINVAL);
  1065. }
  1066. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1067. tmp = *frame;
  1068. tmp.extended_data = tmp.data;
  1069. frame = &tmp;
  1070. }
  1071. /* check for valid frame size */
  1072. if (frame) {
  1073. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1074. if (frame->nb_samples > avctx->frame_size) {
  1075. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1076. return AVERROR(EINVAL);
  1077. }
  1078. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1079. if (frame->nb_samples < avctx->frame_size &&
  1080. !avctx->internal->last_audio_frame) {
  1081. ret = pad_last_frame(avctx, &padded_frame, frame);
  1082. if (ret < 0)
  1083. return ret;
  1084. frame = padded_frame;
  1085. avctx->internal->last_audio_frame = 1;
  1086. }
  1087. if (frame->nb_samples != avctx->frame_size) {
  1088. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1089. ret = AVERROR(EINVAL);
  1090. goto end;
  1091. }
  1092. }
  1093. }
  1094. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1095. if (!ret) {
  1096. if (*got_packet_ptr) {
  1097. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1098. if (avpkt->pts == AV_NOPTS_VALUE)
  1099. avpkt->pts = frame->pts;
  1100. if (!avpkt->duration)
  1101. avpkt->duration = ff_samples_to_time_base(avctx,
  1102. frame->nb_samples);
  1103. }
  1104. avpkt->dts = avpkt->pts;
  1105. } else {
  1106. avpkt->size = 0;
  1107. }
  1108. }
  1109. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1110. needs_realloc = 0;
  1111. if (user_pkt.data) {
  1112. if (user_pkt.size >= avpkt->size) {
  1113. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1114. } else {
  1115. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1116. avpkt->size = user_pkt.size;
  1117. ret = -1;
  1118. }
  1119. avpkt->data = user_pkt.data;
  1120. avpkt->destruct = user_pkt.destruct;
  1121. } else {
  1122. if (av_dup_packet(avpkt) < 0) {
  1123. ret = AVERROR(ENOMEM);
  1124. }
  1125. }
  1126. }
  1127. if (!ret) {
  1128. if (needs_realloc && avpkt->data) {
  1129. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1130. if (new_data)
  1131. avpkt->data = new_data;
  1132. }
  1133. avctx->frame_number++;
  1134. }
  1135. if (ret < 0 || !*got_packet_ptr) {
  1136. av_free_packet(avpkt);
  1137. av_init_packet(avpkt);
  1138. goto end;
  1139. }
  1140. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1141. * this needs to be moved to the encoders, but for now we can do it
  1142. * here to simplify things */
  1143. avpkt->flags |= AV_PKT_FLAG_KEY;
  1144. end:
  1145. if (padded_frame) {
  1146. av_freep(&padded_frame->data[0]);
  1147. if (padded_frame->extended_data != padded_frame->data)
  1148. av_freep(&padded_frame->extended_data);
  1149. av_freep(&padded_frame);
  1150. }
  1151. return ret;
  1152. }
  1153. #if FF_API_OLD_DECODE_AUDIO
  1154. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1155. uint8_t *buf, int buf_size,
  1156. const short *samples)
  1157. {
  1158. AVPacket pkt;
  1159. AVFrame frame0 = { 0 };
  1160. AVFrame *frame;
  1161. int ret, samples_size, got_packet;
  1162. av_init_packet(&pkt);
  1163. pkt.data = buf;
  1164. pkt.size = buf_size;
  1165. if (samples) {
  1166. frame = &frame0;
  1167. avcodec_get_frame_defaults(frame);
  1168. if (avctx->frame_size) {
  1169. frame->nb_samples = avctx->frame_size;
  1170. } else {
  1171. /* if frame_size is not set, the number of samples must be
  1172. * calculated from the buffer size */
  1173. int64_t nb_samples;
  1174. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1175. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1176. "support this codec\n");
  1177. return AVERROR(EINVAL);
  1178. }
  1179. nb_samples = (int64_t)buf_size * 8 /
  1180. (av_get_bits_per_sample(avctx->codec_id) *
  1181. avctx->channels);
  1182. if (nb_samples >= INT_MAX)
  1183. return AVERROR(EINVAL);
  1184. frame->nb_samples = nb_samples;
  1185. }
  1186. /* it is assumed that the samples buffer is large enough based on the
  1187. * relevant parameters */
  1188. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1189. frame->nb_samples,
  1190. avctx->sample_fmt, 1);
  1191. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1192. avctx->sample_fmt,
  1193. (const uint8_t *)samples,
  1194. samples_size, 1)))
  1195. return ret;
  1196. /* fabricate frame pts from sample count.
  1197. * this is needed because the avcodec_encode_audio() API does not have
  1198. * a way for the user to provide pts */
  1199. if (avctx->sample_rate && avctx->time_base.num)
  1200. frame->pts = ff_samples_to_time_base(avctx,
  1201. avctx->internal->sample_count);
  1202. else
  1203. frame->pts = AV_NOPTS_VALUE;
  1204. avctx->internal->sample_count += frame->nb_samples;
  1205. } else {
  1206. frame = NULL;
  1207. }
  1208. got_packet = 0;
  1209. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1210. if (!ret && got_packet && avctx->coded_frame) {
  1211. avctx->coded_frame->pts = pkt.pts;
  1212. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1213. }
  1214. /* free any side data since we cannot return it */
  1215. ff_packet_free_side_data(&pkt);
  1216. if (frame && frame->extended_data != frame->data)
  1217. av_freep(&frame->extended_data);
  1218. return ret ? ret : pkt.size;
  1219. }
  1220. #endif
  1221. #if FF_API_OLD_ENCODE_VIDEO
  1222. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1223. const AVFrame *pict)
  1224. {
  1225. AVPacket pkt;
  1226. int ret, got_packet = 0;
  1227. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1228. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1229. return -1;
  1230. }
  1231. av_init_packet(&pkt);
  1232. pkt.data = buf;
  1233. pkt.size = buf_size;
  1234. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1235. if (!ret && got_packet && avctx->coded_frame) {
  1236. avctx->coded_frame->pts = pkt.pts;
  1237. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1238. }
  1239. /* free any side data since we cannot return it */
  1240. if (pkt.side_data_elems > 0) {
  1241. int i;
  1242. for (i = 0; i < pkt.side_data_elems; i++)
  1243. av_free(pkt.side_data[i].data);
  1244. av_freep(&pkt.side_data);
  1245. pkt.side_data_elems = 0;
  1246. }
  1247. return ret ? ret : pkt.size;
  1248. }
  1249. #endif
  1250. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1251. AVPacket *avpkt,
  1252. const AVFrame *frame,
  1253. int *got_packet_ptr)
  1254. {
  1255. int ret;
  1256. AVPacket user_pkt = *avpkt;
  1257. int needs_realloc = !user_pkt.data;
  1258. *got_packet_ptr = 0;
  1259. if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1260. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1261. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1262. av_free_packet(avpkt);
  1263. av_init_packet(avpkt);
  1264. avpkt->size = 0;
  1265. return 0;
  1266. }
  1267. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1268. return AVERROR(EINVAL);
  1269. av_assert0(avctx->codec->encode2);
  1270. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1271. av_assert0(ret <= 0);
  1272. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1273. needs_realloc = 0;
  1274. if (user_pkt.data) {
  1275. if (user_pkt.size >= avpkt->size) {
  1276. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1277. } else {
  1278. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1279. avpkt->size = user_pkt.size;
  1280. ret = -1;
  1281. }
  1282. avpkt->data = user_pkt.data;
  1283. avpkt->destruct = user_pkt.destruct;
  1284. } else {
  1285. if (av_dup_packet(avpkt) < 0) {
  1286. ret = AVERROR(ENOMEM);
  1287. }
  1288. }
  1289. }
  1290. if (!ret) {
  1291. if (!*got_packet_ptr)
  1292. avpkt->size = 0;
  1293. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1294. avpkt->pts = avpkt->dts = frame->pts;
  1295. if (needs_realloc && avpkt->data &&
  1296. avpkt->destruct == av_destruct_packet) {
  1297. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1298. if (new_data)
  1299. avpkt->data = new_data;
  1300. }
  1301. avctx->frame_number++;
  1302. }
  1303. if (ret < 0 || !*got_packet_ptr)
  1304. av_free_packet(avpkt);
  1305. emms_c();
  1306. return ret;
  1307. }
  1308. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1309. const AVSubtitle *sub)
  1310. {
  1311. int ret;
  1312. if (sub->start_display_time) {
  1313. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1314. return -1;
  1315. }
  1316. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1317. avctx->frame_number++;
  1318. return ret;
  1319. }
  1320. /**
  1321. * Attempt to guess proper monotonic timestamps for decoded video frames
  1322. * which might have incorrect times. Input timestamps may wrap around, in
  1323. * which case the output will as well.
  1324. *
  1325. * @param pts the pts field of the decoded AVPacket, as passed through
  1326. * AVFrame.pkt_pts
  1327. * @param dts the dts field of the decoded AVPacket
  1328. * @return one of the input values, may be AV_NOPTS_VALUE
  1329. */
  1330. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1331. int64_t reordered_pts, int64_t dts)
  1332. {
  1333. int64_t pts = AV_NOPTS_VALUE;
  1334. if (dts != AV_NOPTS_VALUE) {
  1335. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1336. ctx->pts_correction_last_dts = dts;
  1337. }
  1338. if (reordered_pts != AV_NOPTS_VALUE) {
  1339. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1340. ctx->pts_correction_last_pts = reordered_pts;
  1341. }
  1342. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1343. && reordered_pts != AV_NOPTS_VALUE)
  1344. pts = reordered_pts;
  1345. else
  1346. pts = dts;
  1347. return pts;
  1348. }
  1349. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1350. {
  1351. int size = 0;
  1352. const uint8_t *data;
  1353. uint32_t flags;
  1354. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1355. return;
  1356. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1357. if (!data || size < 4)
  1358. return;
  1359. flags = bytestream_get_le32(&data);
  1360. size -= 4;
  1361. if (size < 4) /* Required for any of the changes */
  1362. return;
  1363. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1364. avctx->channels = bytestream_get_le32(&data);
  1365. size -= 4;
  1366. }
  1367. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1368. if (size < 8)
  1369. return;
  1370. avctx->channel_layout = bytestream_get_le64(&data);
  1371. size -= 8;
  1372. }
  1373. if (size < 4)
  1374. return;
  1375. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1376. avctx->sample_rate = bytestream_get_le32(&data);
  1377. size -= 4;
  1378. }
  1379. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1380. if (size < 8)
  1381. return;
  1382. avctx->width = bytestream_get_le32(&data);
  1383. avctx->height = bytestream_get_le32(&data);
  1384. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1385. size -= 8;
  1386. }
  1387. }
  1388. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1389. int *got_picture_ptr,
  1390. const AVPacket *avpkt)
  1391. {
  1392. int ret;
  1393. // copy to ensure we do not change avpkt
  1394. AVPacket tmp = *avpkt;
  1395. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1396. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1397. return AVERROR(EINVAL);
  1398. }
  1399. *got_picture_ptr = 0;
  1400. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1401. return AVERROR(EINVAL);
  1402. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1403. int did_split = av_packet_split_side_data(&tmp);
  1404. apply_param_change(avctx, &tmp);
  1405. avctx->pkt = &tmp;
  1406. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1407. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1408. &tmp);
  1409. else {
  1410. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1411. &tmp);
  1412. picture->pkt_dts = avpkt->dts;
  1413. if(!avctx->has_b_frames){
  1414. picture->pkt_pos = avpkt->pos;
  1415. }
  1416. //FIXME these should be under if(!avctx->has_b_frames)
  1417. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1418. if (!picture->width) picture->width = avctx->width;
  1419. if (!picture->height) picture->height = avctx->height;
  1420. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1421. }
  1422. emms_c(); //needed to avoid an emms_c() call before every return;
  1423. avctx->pkt = NULL;
  1424. if (did_split) {
  1425. ff_packet_free_side_data(&tmp);
  1426. if(ret == tmp.size)
  1427. ret = avpkt->size;
  1428. }
  1429. if (*got_picture_ptr){
  1430. avctx->frame_number++;
  1431. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1432. picture->pkt_pts,
  1433. picture->pkt_dts);
  1434. }
  1435. } else
  1436. ret = 0;
  1437. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1438. * make sure it's set correctly */
  1439. picture->extended_data = picture->data;
  1440. return ret;
  1441. }
  1442. #if FF_API_OLD_DECODE_AUDIO
  1443. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1444. int *frame_size_ptr,
  1445. AVPacket *avpkt)
  1446. {
  1447. AVFrame frame;
  1448. int ret, got_frame = 0;
  1449. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1450. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1451. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1452. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1453. "avcodec_decode_audio4()\n");
  1454. avctx->get_buffer = avcodec_default_get_buffer;
  1455. avctx->release_buffer = avcodec_default_release_buffer;
  1456. }
  1457. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1458. if (ret >= 0 && got_frame) {
  1459. int ch, plane_size;
  1460. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1461. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1462. frame.nb_samples,
  1463. avctx->sample_fmt, 1);
  1464. if (*frame_size_ptr < data_size) {
  1465. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1466. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1467. return AVERROR(EINVAL);
  1468. }
  1469. memcpy(samples, frame.extended_data[0], plane_size);
  1470. if (planar && avctx->channels > 1) {
  1471. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1472. for (ch = 1; ch < avctx->channels; ch++) {
  1473. memcpy(out, frame.extended_data[ch], plane_size);
  1474. out += plane_size;
  1475. }
  1476. }
  1477. *frame_size_ptr = data_size;
  1478. } else {
  1479. *frame_size_ptr = 0;
  1480. }
  1481. return ret;
  1482. }
  1483. #endif
  1484. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1485. AVFrame *frame,
  1486. int *got_frame_ptr,
  1487. const AVPacket *avpkt)
  1488. {
  1489. int planar, channels;
  1490. int ret = 0;
  1491. *got_frame_ptr = 0;
  1492. if (!avpkt->data && avpkt->size) {
  1493. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1494. return AVERROR(EINVAL);
  1495. }
  1496. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1497. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1498. return AVERROR(EINVAL);
  1499. }
  1500. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1501. uint8_t *side;
  1502. int side_size;
  1503. // copy to ensure we do not change avpkt
  1504. AVPacket tmp = *avpkt;
  1505. int did_split = av_packet_split_side_data(&tmp);
  1506. apply_param_change(avctx, &tmp);
  1507. avctx->pkt = &tmp;
  1508. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1509. if (ret >= 0 && *got_frame_ptr) {
  1510. avctx->frame_number++;
  1511. frame->pkt_dts = avpkt->dts;
  1512. frame->best_effort_timestamp = guess_correct_pts(avctx,
  1513. frame->pkt_pts,
  1514. frame->pkt_dts);
  1515. if (frame->format == AV_SAMPLE_FMT_NONE)
  1516. frame->format = avctx->sample_fmt;
  1517. if (!frame->channel_layout)
  1518. frame->channel_layout = avctx->channel_layout;
  1519. if (!frame->channels)
  1520. frame->channels = avctx->channels;
  1521. if (!frame->sample_rate)
  1522. frame->sample_rate = avctx->sample_rate;
  1523. }
  1524. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1525. if(side && side_size>=10) {
  1526. avctx->internal->skip_samples = AV_RL32(side);
  1527. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1528. avctx->internal->skip_samples);
  1529. }
  1530. if (avctx->internal->skip_samples) {
  1531. if(frame->nb_samples <= avctx->internal->skip_samples){
  1532. *got_frame_ptr = 0;
  1533. avctx->internal->skip_samples -= frame->nb_samples;
  1534. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1535. avctx->internal->skip_samples);
  1536. } else {
  1537. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1538. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1539. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1540. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1541. (AVRational){1, avctx->sample_rate},
  1542. avctx->pkt_timebase);
  1543. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1544. frame->pkt_pts += diff_ts;
  1545. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1546. frame->pkt_dts += diff_ts;
  1547. if (frame->pkt_duration >= diff_ts)
  1548. frame->pkt_duration -= diff_ts;
  1549. } else {
  1550. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1551. }
  1552. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1553. avctx->internal->skip_samples, frame->nb_samples);
  1554. frame->nb_samples -= avctx->internal->skip_samples;
  1555. avctx->internal->skip_samples = 0;
  1556. }
  1557. }
  1558. avctx->pkt = NULL;
  1559. if (did_split) {
  1560. ff_packet_free_side_data(&tmp);
  1561. if(ret == tmp.size)
  1562. ret = avpkt->size;
  1563. }
  1564. }
  1565. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1566. * make sure it's set correctly; assume decoders that actually use
  1567. * extended_data are doing it correctly */
  1568. if (*got_frame_ptr) {
  1569. planar = av_sample_fmt_is_planar(frame->format);
  1570. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1571. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1572. frame->extended_data = frame->data;
  1573. } else {
  1574. frame->extended_data = NULL;
  1575. }
  1576. return ret;
  1577. }
  1578. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1579. int *got_sub_ptr,
  1580. AVPacket *avpkt)
  1581. {
  1582. int ret;
  1583. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1584. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1585. return AVERROR(EINVAL);
  1586. }
  1587. avctx->pkt = avpkt;
  1588. *got_sub_ptr = 0;
  1589. avcodec_get_subtitle_defaults(sub);
  1590. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1591. sub->pts = av_rescale_q(avpkt->pts,
  1592. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1593. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1594. if (*got_sub_ptr)
  1595. avctx->frame_number++;
  1596. return ret;
  1597. }
  1598. void avsubtitle_free(AVSubtitle *sub)
  1599. {
  1600. int i;
  1601. for (i = 0; i < sub->num_rects; i++) {
  1602. av_freep(&sub->rects[i]->pict.data[0]);
  1603. av_freep(&sub->rects[i]->pict.data[1]);
  1604. av_freep(&sub->rects[i]->pict.data[2]);
  1605. av_freep(&sub->rects[i]->pict.data[3]);
  1606. av_freep(&sub->rects[i]->text);
  1607. av_freep(&sub->rects[i]->ass);
  1608. av_freep(&sub->rects[i]);
  1609. }
  1610. av_freep(&sub->rects);
  1611. memset(sub, 0, sizeof(AVSubtitle));
  1612. }
  1613. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  1614. {
  1615. int ret = 0;
  1616. entangled_thread_counter--;
  1617. /* Release any user-supplied mutex. */
  1618. if (ff_lockmgr_cb) {
  1619. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1620. }
  1621. ret = avcodec_close(avctx);
  1622. /* If there is a user-supplied mutex locking routine, call it. */
  1623. if (ff_lockmgr_cb) {
  1624. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1625. return -1;
  1626. }
  1627. entangled_thread_counter++;
  1628. return ret;
  1629. }
  1630. av_cold int avcodec_close(AVCodecContext *avctx)
  1631. {
  1632. /* If there is a user-supplied mutex locking routine, call it. */
  1633. if (ff_lockmgr_cb) {
  1634. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1635. return -1;
  1636. }
  1637. entangled_thread_counter++;
  1638. if (entangled_thread_counter != 1) {
  1639. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1640. entangled_thread_counter--;
  1641. return -1;
  1642. }
  1643. if (avcodec_is_open(avctx)) {
  1644. if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1645. entangled_thread_counter --;
  1646. ff_frame_thread_encoder_free(avctx);
  1647. entangled_thread_counter ++;
  1648. }
  1649. if (HAVE_THREADS && avctx->thread_opaque)
  1650. ff_thread_free(avctx);
  1651. if (avctx->codec && avctx->codec->close)
  1652. avctx->codec->close(avctx);
  1653. avcodec_default_free_buffers(avctx);
  1654. avctx->coded_frame = NULL;
  1655. avctx->internal->byte_buffer_size = 0;
  1656. av_freep(&avctx->internal->byte_buffer);
  1657. av_freep(&avctx->internal);
  1658. }
  1659. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1660. av_opt_free(avctx->priv_data);
  1661. av_opt_free(avctx);
  1662. av_freep(&avctx->priv_data);
  1663. if (av_codec_is_encoder(avctx->codec))
  1664. av_freep(&avctx->extradata);
  1665. avctx->codec = NULL;
  1666. avctx->active_thread_type = 0;
  1667. entangled_thread_counter--;
  1668. /* Release any user-supplied mutex. */
  1669. if (ff_lockmgr_cb) {
  1670. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1671. }
  1672. return 0;
  1673. }
  1674. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  1675. {
  1676. switch(id){
  1677. //This is for future deprecatec codec ids, its empty since
  1678. //last major bump but will fill up again over time, please don't remove it
  1679. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  1680. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  1681. default : return id;
  1682. }
  1683. }
  1684. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1685. {
  1686. AVCodec *p, *experimental = NULL;
  1687. p = first_avcodec;
  1688. id= remap_deprecated_codec_id(id);
  1689. while (p) {
  1690. if (av_codec_is_encoder(p) && p->id == id) {
  1691. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1692. experimental = p;
  1693. } else
  1694. return p;
  1695. }
  1696. p = p->next;
  1697. }
  1698. return experimental;
  1699. }
  1700. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1701. {
  1702. AVCodec *p;
  1703. if (!name)
  1704. return NULL;
  1705. p = first_avcodec;
  1706. while (p) {
  1707. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1708. return p;
  1709. p = p->next;
  1710. }
  1711. return NULL;
  1712. }
  1713. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1714. {
  1715. AVCodec *p, *experimental=NULL;
  1716. p = first_avcodec;
  1717. id= remap_deprecated_codec_id(id);
  1718. while (p) {
  1719. if (av_codec_is_decoder(p) && p->id == id) {
  1720. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1721. experimental = p;
  1722. } else
  1723. return p;
  1724. }
  1725. p = p->next;
  1726. }
  1727. return experimental;
  1728. }
  1729. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1730. {
  1731. AVCodec *p;
  1732. if (!name)
  1733. return NULL;
  1734. p = first_avcodec;
  1735. while (p) {
  1736. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1737. return p;
  1738. p = p->next;
  1739. }
  1740. return NULL;
  1741. }
  1742. const char *avcodec_get_name(enum AVCodecID id)
  1743. {
  1744. const AVCodecDescriptor *cd;
  1745. AVCodec *codec;
  1746. if (id == AV_CODEC_ID_NONE)
  1747. return "none";
  1748. cd = avcodec_descriptor_get(id);
  1749. if (cd)
  1750. return cd->name;
  1751. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1752. codec = avcodec_find_decoder(id);
  1753. if (codec)
  1754. return codec->name;
  1755. codec = avcodec_find_encoder(id);
  1756. if (codec)
  1757. return codec->name;
  1758. return "unknown_codec";
  1759. }
  1760. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1761. {
  1762. int i, len, ret = 0;
  1763. #define IS_PRINT(x) \
  1764. (((x) >= '0' && (x) <= '9') || \
  1765. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1766. ((x) == '.' || (x) == ' ' || (x) == '-'))
  1767. for (i = 0; i < 4; i++) {
  1768. len = snprintf(buf, buf_size,
  1769. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1770. buf += len;
  1771. buf_size = buf_size > len ? buf_size - len : 0;
  1772. ret += len;
  1773. codec_tag >>= 8;
  1774. }
  1775. return ret;
  1776. }
  1777. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1778. {
  1779. const char *codec_type;
  1780. const char *codec_name;
  1781. const char *profile = NULL;
  1782. const AVCodec *p;
  1783. int bitrate;
  1784. AVRational display_aspect_ratio;
  1785. if (!buf || buf_size <= 0)
  1786. return;
  1787. codec_type = av_get_media_type_string(enc->codec_type);
  1788. codec_name = avcodec_get_name(enc->codec_id);
  1789. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1790. if (enc->codec)
  1791. p = enc->codec;
  1792. else
  1793. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1794. avcodec_find_decoder(enc->codec_id);
  1795. if (p)
  1796. profile = av_get_profile_name(p, enc->profile);
  1797. }
  1798. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1799. codec_name, enc->mb_decision ? " (hq)" : "");
  1800. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1801. if (profile)
  1802. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1803. if (enc->codec_tag) {
  1804. char tag_buf[32];
  1805. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1806. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1807. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1808. }
  1809. switch (enc->codec_type) {
  1810. case AVMEDIA_TYPE_VIDEO:
  1811. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1812. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1813. ", %s",
  1814. av_get_pix_fmt_name(enc->pix_fmt));
  1815. }
  1816. if (enc->width) {
  1817. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1818. ", %dx%d",
  1819. enc->width, enc->height);
  1820. if (enc->sample_aspect_ratio.num) {
  1821. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1822. enc->width * enc->sample_aspect_ratio.num,
  1823. enc->height * enc->sample_aspect_ratio.den,
  1824. 1024 * 1024);
  1825. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1826. " [SAR %d:%d DAR %d:%d]",
  1827. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1828. display_aspect_ratio.num, display_aspect_ratio.den);
  1829. }
  1830. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1831. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1832. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1833. ", %d/%d",
  1834. enc->time_base.num / g, enc->time_base.den / g);
  1835. }
  1836. }
  1837. if (encode) {
  1838. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1839. ", q=%d-%d", enc->qmin, enc->qmax);
  1840. }
  1841. break;
  1842. case AVMEDIA_TYPE_AUDIO:
  1843. if (enc->sample_rate) {
  1844. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1845. ", %d Hz", enc->sample_rate);
  1846. }
  1847. av_strlcat(buf, ", ", buf_size);
  1848. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1849. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1850. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1851. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1852. }
  1853. break;
  1854. default:
  1855. return;
  1856. }
  1857. if (encode) {
  1858. if (enc->flags & CODEC_FLAG_PASS1)
  1859. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1860. ", pass 1");
  1861. if (enc->flags & CODEC_FLAG_PASS2)
  1862. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1863. ", pass 2");
  1864. }
  1865. bitrate = get_bit_rate(enc);
  1866. if (bitrate != 0) {
  1867. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1868. ", %d kb/s", bitrate / 1000);
  1869. }
  1870. }
  1871. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1872. {
  1873. const AVProfile *p;
  1874. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1875. return NULL;
  1876. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1877. if (p->profile == profile)
  1878. return p->name;
  1879. return NULL;
  1880. }
  1881. unsigned avcodec_version(void)
  1882. {
  1883. // av_assert0(AV_CODEC_ID_V410==164);
  1884. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1885. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1886. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  1887. av_assert0(AV_CODEC_ID_SRT==94216);
  1888. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1889. return LIBAVCODEC_VERSION_INT;
  1890. }
  1891. const char *avcodec_configuration(void)
  1892. {
  1893. return FFMPEG_CONFIGURATION;
  1894. }
  1895. const char *avcodec_license(void)
  1896. {
  1897. #define LICENSE_PREFIX "libavcodec license: "
  1898. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1899. }
  1900. void avcodec_flush_buffers(AVCodecContext *avctx)
  1901. {
  1902. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1903. ff_thread_flush(avctx);
  1904. else if (avctx->codec->flush)
  1905. avctx->codec->flush(avctx);
  1906. avctx->pts_correction_last_pts =
  1907. avctx->pts_correction_last_dts = INT64_MIN;
  1908. }
  1909. static void video_free_buffers(AVCodecContext *s)
  1910. {
  1911. AVCodecInternal *avci = s->internal;
  1912. int i, j;
  1913. if (!avci->buffer)
  1914. return;
  1915. if (avci->buffer_count)
  1916. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1917. avci->buffer_count);
  1918. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1919. InternalBuffer *buf = &avci->buffer[i];
  1920. for (j = 0; j < 4; j++) {
  1921. av_freep(&buf->base[j]);
  1922. buf->data[j] = NULL;
  1923. }
  1924. }
  1925. av_freep(&avci->buffer);
  1926. avci->buffer_count = 0;
  1927. }
  1928. static void audio_free_buffers(AVCodecContext *avctx)
  1929. {
  1930. AVCodecInternal *avci = avctx->internal;
  1931. InternalBuffer *buf;
  1932. if (!avci->buffer)
  1933. return;
  1934. buf = avci->buffer;
  1935. if (buf->extended_data) {
  1936. av_free(buf->extended_data[0]);
  1937. if (buf->extended_data != buf->data)
  1938. av_freep(&buf->extended_data);
  1939. }
  1940. av_freep(&avci->buffer);
  1941. }
  1942. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1943. {
  1944. switch (avctx->codec_type) {
  1945. case AVMEDIA_TYPE_VIDEO:
  1946. video_free_buffers(avctx);
  1947. break;
  1948. case AVMEDIA_TYPE_AUDIO:
  1949. audio_free_buffers(avctx);
  1950. break;
  1951. default:
  1952. break;
  1953. }
  1954. }
  1955. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1956. {
  1957. switch (codec_id) {
  1958. case AV_CODEC_ID_ADPCM_CT:
  1959. case AV_CODEC_ID_ADPCM_IMA_APC:
  1960. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1961. case AV_CODEC_ID_ADPCM_IMA_WS:
  1962. case AV_CODEC_ID_ADPCM_G722:
  1963. case AV_CODEC_ID_ADPCM_YAMAHA:
  1964. return 4;
  1965. case AV_CODEC_ID_PCM_ALAW:
  1966. case AV_CODEC_ID_PCM_MULAW:
  1967. case AV_CODEC_ID_PCM_S8:
  1968. case AV_CODEC_ID_PCM_U8:
  1969. case AV_CODEC_ID_PCM_ZORK:
  1970. return 8;
  1971. case AV_CODEC_ID_PCM_S16BE:
  1972. case AV_CODEC_ID_PCM_S16LE:
  1973. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1974. case AV_CODEC_ID_PCM_U16BE:
  1975. case AV_CODEC_ID_PCM_U16LE:
  1976. return 16;
  1977. case AV_CODEC_ID_PCM_S24DAUD:
  1978. case AV_CODEC_ID_PCM_S24BE:
  1979. case AV_CODEC_ID_PCM_S24LE:
  1980. case AV_CODEC_ID_PCM_U24BE:
  1981. case AV_CODEC_ID_PCM_U24LE:
  1982. return 24;
  1983. case AV_CODEC_ID_PCM_S32BE:
  1984. case AV_CODEC_ID_PCM_S32LE:
  1985. case AV_CODEC_ID_PCM_U32BE:
  1986. case AV_CODEC_ID_PCM_U32LE:
  1987. case AV_CODEC_ID_PCM_F32BE:
  1988. case AV_CODEC_ID_PCM_F32LE:
  1989. return 32;
  1990. case AV_CODEC_ID_PCM_F64BE:
  1991. case AV_CODEC_ID_PCM_F64LE:
  1992. return 64;
  1993. default:
  1994. return 0;
  1995. }
  1996. }
  1997. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1998. {
  1999. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2000. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2001. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2002. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2003. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2004. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2005. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2006. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2007. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2008. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2009. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2010. };
  2011. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2012. return AV_CODEC_ID_NONE;
  2013. if (be < 0 || be > 1)
  2014. be = AV_NE(1, 0);
  2015. return map[fmt][be];
  2016. }
  2017. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2018. {
  2019. switch (codec_id) {
  2020. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2021. return 2;
  2022. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2023. return 3;
  2024. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2025. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2026. case AV_CODEC_ID_ADPCM_IMA_QT:
  2027. case AV_CODEC_ID_ADPCM_SWF:
  2028. case AV_CODEC_ID_ADPCM_MS:
  2029. return 4;
  2030. default:
  2031. return av_get_exact_bits_per_sample(codec_id);
  2032. }
  2033. }
  2034. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2035. {
  2036. int id, sr, ch, ba, tag, bps;
  2037. id = avctx->codec_id;
  2038. sr = avctx->sample_rate;
  2039. ch = avctx->channels;
  2040. ba = avctx->block_align;
  2041. tag = avctx->codec_tag;
  2042. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2043. /* codecs with an exact constant bits per sample */
  2044. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2045. return (frame_bytes * 8LL) / (bps * ch);
  2046. bps = avctx->bits_per_coded_sample;
  2047. /* codecs with a fixed packet duration */
  2048. switch (id) {
  2049. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2050. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2051. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2052. case AV_CODEC_ID_AMR_NB:
  2053. case AV_CODEC_ID_GSM:
  2054. case AV_CODEC_ID_QCELP:
  2055. case AV_CODEC_ID_RA_288: return 160;
  2056. case AV_CODEC_ID_IMC: return 256;
  2057. case AV_CODEC_ID_AMR_WB:
  2058. case AV_CODEC_ID_GSM_MS: return 320;
  2059. case AV_CODEC_ID_MP1: return 384;
  2060. case AV_CODEC_ID_ATRAC1: return 512;
  2061. case AV_CODEC_ID_ATRAC3: return 1024;
  2062. case AV_CODEC_ID_MP2:
  2063. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2064. case AV_CODEC_ID_AC3: return 1536;
  2065. }
  2066. if (sr > 0) {
  2067. /* calc from sample rate */
  2068. if (id == AV_CODEC_ID_TTA)
  2069. return 256 * sr / 245;
  2070. if (ch > 0) {
  2071. /* calc from sample rate and channels */
  2072. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2073. return (480 << (sr / 22050)) / ch;
  2074. }
  2075. }
  2076. if (ba > 0) {
  2077. /* calc from block_align */
  2078. if (id == AV_CODEC_ID_SIPR) {
  2079. switch (ba) {
  2080. case 20: return 160;
  2081. case 19: return 144;
  2082. case 29: return 288;
  2083. case 37: return 480;
  2084. }
  2085. } else if (id == AV_CODEC_ID_ILBC) {
  2086. switch (ba) {
  2087. case 38: return 160;
  2088. case 50: return 240;
  2089. }
  2090. }
  2091. }
  2092. if (frame_bytes > 0) {
  2093. /* calc from frame_bytes only */
  2094. if (id == AV_CODEC_ID_TRUESPEECH)
  2095. return 240 * (frame_bytes / 32);
  2096. if (id == AV_CODEC_ID_NELLYMOSER)
  2097. return 256 * (frame_bytes / 64);
  2098. if (id == AV_CODEC_ID_RA_144)
  2099. return 160 * (frame_bytes / 20);
  2100. if (bps > 0) {
  2101. /* calc from frame_bytes and bits_per_coded_sample */
  2102. if (id == AV_CODEC_ID_ADPCM_G726)
  2103. return frame_bytes * 8 / bps;
  2104. }
  2105. if (ch > 0) {
  2106. /* calc from frame_bytes and channels */
  2107. switch (id) {
  2108. case AV_CODEC_ID_ADPCM_4XM:
  2109. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2110. return (frame_bytes - 4 * ch) * 2 / ch;
  2111. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2112. return (frame_bytes - 4) * 2 / ch;
  2113. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2114. return (frame_bytes - 8) * 2 / ch;
  2115. case AV_CODEC_ID_ADPCM_XA:
  2116. return (frame_bytes / 128) * 224 / ch;
  2117. case AV_CODEC_ID_INTERPLAY_DPCM:
  2118. return (frame_bytes - 6 - ch) / ch;
  2119. case AV_CODEC_ID_ROQ_DPCM:
  2120. return (frame_bytes - 8) / ch;
  2121. case AV_CODEC_ID_XAN_DPCM:
  2122. return (frame_bytes - 2 * ch) / ch;
  2123. case AV_CODEC_ID_MACE3:
  2124. return 3 * frame_bytes / ch;
  2125. case AV_CODEC_ID_MACE6:
  2126. return 6 * frame_bytes / ch;
  2127. case AV_CODEC_ID_PCM_LXF:
  2128. return 2 * (frame_bytes / (5 * ch));
  2129. }
  2130. if (tag) {
  2131. /* calc from frame_bytes, channels, and codec_tag */
  2132. if (id == AV_CODEC_ID_SOL_DPCM) {
  2133. if (tag == 3)
  2134. return frame_bytes / ch;
  2135. else
  2136. return frame_bytes * 2 / ch;
  2137. }
  2138. }
  2139. if (ba > 0) {
  2140. /* calc from frame_bytes, channels, and block_align */
  2141. int blocks = frame_bytes / ba;
  2142. switch (avctx->codec_id) {
  2143. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2144. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2145. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2146. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2147. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2148. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2149. case AV_CODEC_ID_ADPCM_MS:
  2150. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2151. }
  2152. }
  2153. if (bps > 0) {
  2154. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2155. switch (avctx->codec_id) {
  2156. case AV_CODEC_ID_PCM_DVD:
  2157. if(bps<4)
  2158. return 0;
  2159. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2160. case AV_CODEC_ID_PCM_BLURAY:
  2161. if(bps<4)
  2162. return 0;
  2163. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2164. case AV_CODEC_ID_S302M:
  2165. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2166. }
  2167. }
  2168. }
  2169. }
  2170. return 0;
  2171. }
  2172. #if !HAVE_THREADS
  2173. int ff_thread_init(AVCodecContext *s)
  2174. {
  2175. return -1;
  2176. }
  2177. #endif
  2178. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2179. {
  2180. unsigned int n = 0;
  2181. while (v >= 0xff) {
  2182. *s++ = 0xff;
  2183. v -= 0xff;
  2184. n++;
  2185. }
  2186. *s = v;
  2187. n++;
  2188. return n;
  2189. }
  2190. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2191. {
  2192. int i;
  2193. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2194. return i;
  2195. }
  2196. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2197. {
  2198. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2199. "version to the newest one from Git. If the problem still "
  2200. "occurs, it means that your file has a feature which has not "
  2201. "been implemented.\n", feature);
  2202. if(want_sample)
  2203. av_log_ask_for_sample(avc, NULL);
  2204. }
  2205. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2206. {
  2207. va_list argument_list;
  2208. va_start(argument_list, msg);
  2209. if (msg)
  2210. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2211. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2212. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2213. "and contact the ffmpeg-devel mailing list.\n");
  2214. va_end(argument_list);
  2215. }
  2216. static AVHWAccel *first_hwaccel = NULL;
  2217. void av_register_hwaccel(AVHWAccel *hwaccel)
  2218. {
  2219. AVHWAccel **p = &first_hwaccel;
  2220. while (*p)
  2221. p = &(*p)->next;
  2222. *p = hwaccel;
  2223. hwaccel->next = NULL;
  2224. }
  2225. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2226. {
  2227. return hwaccel ? hwaccel->next : first_hwaccel;
  2228. }
  2229. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2230. {
  2231. AVHWAccel *hwaccel = NULL;
  2232. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2233. if (hwaccel->id == codec_id
  2234. && hwaccel->pix_fmt == pix_fmt)
  2235. return hwaccel;
  2236. return NULL;
  2237. }
  2238. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2239. {
  2240. if (ff_lockmgr_cb) {
  2241. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2242. return -1;
  2243. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2244. return -1;
  2245. }
  2246. ff_lockmgr_cb = cb;
  2247. if (ff_lockmgr_cb) {
  2248. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2249. return -1;
  2250. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2251. return -1;
  2252. }
  2253. return 0;
  2254. }
  2255. int avpriv_lock_avformat(void)
  2256. {
  2257. if (ff_lockmgr_cb) {
  2258. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2259. return -1;
  2260. }
  2261. return 0;
  2262. }
  2263. int avpriv_unlock_avformat(void)
  2264. {
  2265. if (ff_lockmgr_cb) {
  2266. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2267. return -1;
  2268. }
  2269. return 0;
  2270. }
  2271. unsigned int avpriv_toupper4(unsigned int x)
  2272. {
  2273. return toupper(x & 0xFF)
  2274. + (toupper((x >> 8) & 0xFF) << 8)
  2275. + (toupper((x >> 16) & 0xFF) << 16)
  2276. + (toupper((x >> 24) & 0xFF) << 24);
  2277. }
  2278. #if !HAVE_THREADS
  2279. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2280. {
  2281. f->owner = avctx;
  2282. ff_init_buffer_info(avctx, f);
  2283. return avctx->get_buffer(avctx, f);
  2284. }
  2285. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2286. {
  2287. f->owner->release_buffer(f->owner, f);
  2288. }
  2289. void ff_thread_finish_setup(AVCodecContext *avctx)
  2290. {
  2291. }
  2292. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2293. {
  2294. }
  2295. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2296. {
  2297. }
  2298. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2299. {
  2300. return 1;
  2301. }
  2302. #endif
  2303. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2304. {
  2305. AVCodec *c= avcodec_find_decoder(codec_id);
  2306. if(!c)
  2307. c= avcodec_find_encoder(codec_id);
  2308. if(c)
  2309. return c->type;
  2310. if (codec_id <= AV_CODEC_ID_NONE)
  2311. return AVMEDIA_TYPE_UNKNOWN;
  2312. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2313. return AVMEDIA_TYPE_VIDEO;
  2314. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2315. return AVMEDIA_TYPE_AUDIO;
  2316. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2317. return AVMEDIA_TYPE_SUBTITLE;
  2318. return AVMEDIA_TYPE_UNKNOWN;
  2319. }
  2320. int avcodec_is_open(AVCodecContext *s)
  2321. {
  2322. return !!s->internal;
  2323. }