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.

2637 lines
84KB

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