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.

2174 lines
66KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "avcodec.h"
  36. #include "dsputil.h"
  37. #include "libavutil/opt.h"
  38. #include "thread.h"
  39. #include "audioconvert.h"
  40. #include "internal.h"
  41. #include "bytestream.h"
  42. #include <stdlib.h>
  43. #include <stdarg.h>
  44. #include <limits.h>
  45. #include <float.h>
  46. static int volatile entangled_thread_counter = 0;
  47. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  48. static void *codec_mutex;
  49. static void *avformat_mutex;
  50. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  51. {
  52. if (min_size < *size)
  53. return ptr;
  54. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  55. ptr = av_realloc(ptr, min_size);
  56. /* we could set this to the unmodified min_size but this is safer
  57. * if the user lost the ptr and uses NULL now
  58. */
  59. if (!ptr)
  60. min_size = 0;
  61. *size = min_size;
  62. return ptr;
  63. }
  64. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  65. {
  66. void **p = ptr;
  67. if (min_size < *size)
  68. return;
  69. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  70. av_free(*p);
  71. *p = av_malloc(min_size);
  72. if (!*p)
  73. min_size = 0;
  74. *size = min_size;
  75. }
  76. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  77. {
  78. void **p = ptr;
  79. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  80. av_freep(p);
  81. *size = 0;
  82. return;
  83. }
  84. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  85. if (*size)
  86. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  87. }
  88. /* encoder management */
  89. static AVCodec *first_avcodec = NULL;
  90. AVCodec *av_codec_next(const AVCodec *c)
  91. {
  92. if (c)
  93. return c->next;
  94. else
  95. return first_avcodec;
  96. }
  97. static void avcodec_init(void)
  98. {
  99. static int initialized = 0;
  100. if (initialized != 0)
  101. return;
  102. initialized = 1;
  103. ff_dsputil_static_init();
  104. }
  105. int av_codec_is_encoder(const AVCodec *codec)
  106. {
  107. return codec && (codec->encode_sub || codec->encode2);
  108. }
  109. int av_codec_is_decoder(const AVCodec *codec)
  110. {
  111. return codec && codec->decode;
  112. }
  113. void avcodec_register(AVCodec *codec)
  114. {
  115. AVCodec **p;
  116. avcodec_init();
  117. p = &first_avcodec;
  118. while (*p != NULL)
  119. p = &(*p)->next;
  120. *p = codec;
  121. codec->next = NULL;
  122. if (codec->init_static_data)
  123. codec->init_static_data(codec);
  124. }
  125. unsigned avcodec_get_edge_width(void)
  126. {
  127. return EDGE_WIDTH;
  128. }
  129. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  130. {
  131. s->coded_width = width;
  132. s->coded_height = height;
  133. s->width = width;
  134. s->height = height;
  135. }
  136. #define INTERNAL_BUFFER_SIZE (32 + 1)
  137. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  138. int linesize_align[AV_NUM_DATA_POINTERS])
  139. {
  140. int i;
  141. int w_align = 1;
  142. int h_align = 1;
  143. switch (s->pix_fmt) {
  144. case AV_PIX_FMT_YUV420P:
  145. case AV_PIX_FMT_YUYV422:
  146. case AV_PIX_FMT_UYVY422:
  147. case AV_PIX_FMT_YUV422P:
  148. case AV_PIX_FMT_YUV440P:
  149. case AV_PIX_FMT_YUV444P:
  150. case AV_PIX_FMT_GBRP:
  151. case AV_PIX_FMT_GRAY8:
  152. case AV_PIX_FMT_GRAY16BE:
  153. case AV_PIX_FMT_GRAY16LE:
  154. case AV_PIX_FMT_YUVJ420P:
  155. case AV_PIX_FMT_YUVJ422P:
  156. case AV_PIX_FMT_YUVJ440P:
  157. case AV_PIX_FMT_YUVJ444P:
  158. case AV_PIX_FMT_YUVA420P:
  159. case AV_PIX_FMT_YUV420P9LE:
  160. case AV_PIX_FMT_YUV420P9BE:
  161. case AV_PIX_FMT_YUV420P10LE:
  162. case AV_PIX_FMT_YUV420P10BE:
  163. case AV_PIX_FMT_YUV422P9LE:
  164. case AV_PIX_FMT_YUV422P9BE:
  165. case AV_PIX_FMT_YUV422P10LE:
  166. case AV_PIX_FMT_YUV422P10BE:
  167. case AV_PIX_FMT_YUV444P9LE:
  168. case AV_PIX_FMT_YUV444P9BE:
  169. case AV_PIX_FMT_YUV444P10LE:
  170. case AV_PIX_FMT_YUV444P10BE:
  171. case AV_PIX_FMT_GBRP9LE:
  172. case AV_PIX_FMT_GBRP9BE:
  173. case AV_PIX_FMT_GBRP10LE:
  174. case AV_PIX_FMT_GBRP10BE:
  175. w_align = 16; //FIXME assume 16 pixel per macroblock
  176. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  177. break;
  178. case AV_PIX_FMT_YUV411P:
  179. case AV_PIX_FMT_UYYVYY411:
  180. w_align = 32;
  181. h_align = 8;
  182. break;
  183. case AV_PIX_FMT_YUV410P:
  184. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  185. w_align = 64;
  186. h_align = 64;
  187. }
  188. case AV_PIX_FMT_RGB555:
  189. if (s->codec_id == AV_CODEC_ID_RPZA) {
  190. w_align = 4;
  191. h_align = 4;
  192. }
  193. case AV_PIX_FMT_PAL8:
  194. case AV_PIX_FMT_BGR8:
  195. case AV_PIX_FMT_RGB8:
  196. if (s->codec_id == AV_CODEC_ID_SMC) {
  197. w_align = 4;
  198. h_align = 4;
  199. }
  200. break;
  201. case AV_PIX_FMT_BGR24:
  202. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  203. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  204. w_align = 4;
  205. h_align = 4;
  206. }
  207. break;
  208. default:
  209. w_align = 1;
  210. h_align = 1;
  211. break;
  212. }
  213. *width = FFALIGN(*width, w_align);
  214. *height = FFALIGN(*height, h_align);
  215. if (s->codec_id == AV_CODEC_ID_H264)
  216. // some of the optimized chroma MC reads one line too much
  217. *height += 2;
  218. for (i = 0; i < 4; i++)
  219. linesize_align[i] = STRIDE_ALIGN;
  220. }
  221. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  222. {
  223. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  224. int chroma_shift = desc->log2_chroma_w;
  225. int linesize_align[AV_NUM_DATA_POINTERS];
  226. int align;
  227. avcodec_align_dimensions2(s, width, height, linesize_align);
  228. align = FFMAX(linesize_align[0], linesize_align[3]);
  229. linesize_align[1] <<= chroma_shift;
  230. linesize_align[2] <<= chroma_shift;
  231. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  232. *width = FFALIGN(*width, align);
  233. }
  234. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  235. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  236. int buf_size, int align)
  237. {
  238. int ch, planar, needed_size, ret = 0;
  239. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  240. frame->nb_samples, sample_fmt,
  241. align);
  242. if (buf_size < needed_size)
  243. return AVERROR(EINVAL);
  244. planar = av_sample_fmt_is_planar(sample_fmt);
  245. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  246. if (!(frame->extended_data = av_mallocz(nb_channels *
  247. sizeof(*frame->extended_data))))
  248. return AVERROR(ENOMEM);
  249. } else {
  250. frame->extended_data = frame->data;
  251. }
  252. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  253. buf, nb_channels, frame->nb_samples,
  254. sample_fmt, align)) < 0) {
  255. if (frame->extended_data != frame->data)
  256. av_free(frame->extended_data);
  257. return ret;
  258. }
  259. if (frame->extended_data != frame->data) {
  260. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  261. frame->data[ch] = frame->extended_data[ch];
  262. }
  263. return ret;
  264. }
  265. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  266. {
  267. AVCodecInternal *avci = avctx->internal;
  268. InternalBuffer *buf;
  269. int buf_size, ret;
  270. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  271. frame->nb_samples, avctx->sample_fmt,
  272. 0);
  273. if (buf_size < 0)
  274. return AVERROR(EINVAL);
  275. /* allocate InternalBuffer if needed */
  276. if (!avci->buffer) {
  277. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  278. if (!avci->buffer)
  279. return AVERROR(ENOMEM);
  280. }
  281. buf = avci->buffer;
  282. /* if there is a previously-used internal buffer, check its size and
  283. * channel count to see if we can reuse it */
  284. if (buf->extended_data) {
  285. /* if current buffer is too small, free it */
  286. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  287. av_free(buf->extended_data[0]);
  288. if (buf->extended_data != buf->data)
  289. av_free(buf->extended_data);
  290. buf->extended_data = NULL;
  291. buf->data[0] = NULL;
  292. }
  293. /* if number of channels has changed, reset and/or free extended data
  294. * pointers but leave data buffer in buf->data[0] for reuse */
  295. if (buf->nb_channels != avctx->channels) {
  296. if (buf->extended_data != buf->data)
  297. av_free(buf->extended_data);
  298. buf->extended_data = NULL;
  299. }
  300. }
  301. /* if there is no previous buffer or the previous buffer cannot be used
  302. * as-is, allocate a new buffer and/or rearrange the channel pointers */
  303. if (!buf->extended_data) {
  304. if (!buf->data[0]) {
  305. if (!(buf->data[0] = av_mallocz(buf_size)))
  306. return AVERROR(ENOMEM);
  307. buf->audio_data_size = buf_size;
  308. }
  309. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  310. avctx->sample_fmt, buf->data[0],
  311. buf->audio_data_size, 0)))
  312. return ret;
  313. if (frame->extended_data == frame->data)
  314. buf->extended_data = buf->data;
  315. else
  316. buf->extended_data = frame->extended_data;
  317. memcpy(buf->data, frame->data, sizeof(frame->data));
  318. buf->linesize[0] = frame->linesize[0];
  319. buf->nb_channels = avctx->channels;
  320. } else {
  321. /* copy InternalBuffer info to the AVFrame */
  322. frame->extended_data = buf->extended_data;
  323. frame->linesize[0] = buf->linesize[0];
  324. memcpy(frame->data, buf->data, sizeof(frame->data));
  325. }
  326. frame->type = FF_BUFFER_TYPE_INTERNAL;
  327. if (avctx->pkt)
  328. frame->pkt_pts = avctx->pkt->pts;
  329. else
  330. frame->pkt_pts = AV_NOPTS_VALUE;
  331. frame->reordered_opaque = avctx->reordered_opaque;
  332. frame->sample_rate = avctx->sample_rate;
  333. frame->format = avctx->sample_fmt;
  334. frame->channel_layout = avctx->channel_layout;
  335. if (avctx->debug & FF_DEBUG_BUFFERS)
  336. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  337. "internal audio buffer used\n", frame);
  338. return 0;
  339. }
  340. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  341. {
  342. int i;
  343. int w = s->width;
  344. int h = s->height;
  345. InternalBuffer *buf;
  346. AVCodecInternal *avci = s->internal;
  347. if (pic->data[0] != NULL) {
  348. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  349. return -1;
  350. }
  351. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  352. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  353. return -1;
  354. }
  355. if (av_image_check_size(w, h, 0, s))
  356. return -1;
  357. if (!avci->buffer) {
  358. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  359. sizeof(InternalBuffer));
  360. }
  361. buf = &avci->buffer[avci->buffer_count];
  362. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  363. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  364. av_freep(&buf->base[i]);
  365. buf->data[i] = NULL;
  366. }
  367. }
  368. if (!buf->base[0]) {
  369. int h_chroma_shift, v_chroma_shift;
  370. int size[4] = { 0 };
  371. int tmpsize;
  372. int unaligned;
  373. AVPicture picture;
  374. int stride_align[AV_NUM_DATA_POINTERS];
  375. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  376. const int pixel_size = desc->comp[0].step_minus1 + 1;
  377. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  378. avcodec_align_dimensions2(s, &w, &h, stride_align);
  379. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  380. w += EDGE_WIDTH * 2;
  381. h += EDGE_WIDTH * 2;
  382. }
  383. do {
  384. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  385. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  386. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  387. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  388. w += w & ~(w - 1);
  389. unaligned = 0;
  390. for (i = 0; i < 4; i++)
  391. unaligned |= picture.linesize[i] % stride_align[i];
  392. } while (unaligned);
  393. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  394. if (tmpsize < 0)
  395. return -1;
  396. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  397. size[i] = picture.data[i + 1] - picture.data[i];
  398. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  399. memset(buf->base, 0, sizeof(buf->base));
  400. memset(buf->data, 0, sizeof(buf->data));
  401. for (i = 0; i < 4 && size[i]; i++) {
  402. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  403. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  404. buf->linesize[i] = picture.linesize[i];
  405. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  406. if (buf->base[i] == NULL)
  407. return -1;
  408. memset(buf->base[i], 128, size[i]);
  409. // no edge if EDGE EMU or not planar YUV
  410. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  411. buf->data[i] = buf->base[i];
  412. else
  413. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  414. }
  415. for (; i < AV_NUM_DATA_POINTERS; i++) {
  416. buf->base[i] = buf->data[i] = NULL;
  417. buf->linesize[i] = 0;
  418. }
  419. if (size[1] && !size[2])
  420. ff_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  421. buf->width = s->width;
  422. buf->height = s->height;
  423. buf->pix_fmt = s->pix_fmt;
  424. }
  425. pic->type = FF_BUFFER_TYPE_INTERNAL;
  426. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  427. pic->base[i] = buf->base[i];
  428. pic->data[i] = buf->data[i];
  429. pic->linesize[i] = buf->linesize[i];
  430. }
  431. pic->extended_data = pic->data;
  432. avci->buffer_count++;
  433. pic->width = buf->width;
  434. pic->height = buf->height;
  435. pic->format = buf->pix_fmt;
  436. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  437. if (s->pkt)
  438. pic->pkt_pts = s->pkt->pts;
  439. else
  440. pic->pkt_pts = AV_NOPTS_VALUE;
  441. pic->reordered_opaque = s->reordered_opaque;
  442. if (s->debug & FF_DEBUG_BUFFERS)
  443. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  444. "buffers used\n", pic, avci->buffer_count);
  445. return 0;
  446. }
  447. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  448. {
  449. switch (avctx->codec_type) {
  450. case AVMEDIA_TYPE_VIDEO:
  451. return video_get_buffer(avctx, frame);
  452. case AVMEDIA_TYPE_AUDIO:
  453. return audio_get_buffer(avctx, frame);
  454. default:
  455. return -1;
  456. }
  457. }
  458. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  459. {
  460. int i;
  461. InternalBuffer *buf, *last;
  462. AVCodecInternal *avci = s->internal;
  463. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  464. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  465. assert(avci->buffer_count);
  466. if (avci->buffer) {
  467. buf = NULL; /* avoids warning */
  468. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  469. buf = &avci->buffer[i];
  470. if (buf->data[0] == pic->data[0])
  471. break;
  472. }
  473. assert(i < avci->buffer_count);
  474. avci->buffer_count--;
  475. last = &avci->buffer[avci->buffer_count];
  476. if (buf != last)
  477. FFSWAP(InternalBuffer, *buf, *last);
  478. }
  479. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  480. pic->data[i] = NULL;
  481. // pic->base[i]=NULL;
  482. if (s->debug & FF_DEBUG_BUFFERS)
  483. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  484. "buffers used\n", pic, avci->buffer_count);
  485. }
  486. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  487. {
  488. AVFrame temp_pic;
  489. int i;
  490. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  491. /* If no picture return a new buffer */
  492. if (pic->data[0] == NULL) {
  493. /* We will copy from buffer, so must be readable */
  494. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  495. return s->get_buffer(s, pic);
  496. }
  497. assert(s->pix_fmt == pic->format);
  498. /* If internal buffer type return the same buffer */
  499. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  500. if (s->pkt)
  501. pic->pkt_pts = s->pkt->pts;
  502. else
  503. pic->pkt_pts = AV_NOPTS_VALUE;
  504. pic->reordered_opaque = s->reordered_opaque;
  505. return 0;
  506. }
  507. /*
  508. * Not internal type and reget_buffer not overridden, emulate cr buffer
  509. */
  510. temp_pic = *pic;
  511. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  512. pic->data[i] = pic->base[i] = NULL;
  513. pic->opaque = NULL;
  514. /* Allocate new frame */
  515. if (s->get_buffer(s, pic))
  516. return -1;
  517. /* Copy image data from old buffer to new buffer */
  518. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  519. s->height);
  520. s->release_buffer(s, &temp_pic); // Release old frame
  521. return 0;
  522. }
  523. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  524. {
  525. int i;
  526. for (i = 0; i < count; i++) {
  527. int r = func(c, (char *)arg + i * size);
  528. if (ret)
  529. ret[i] = r;
  530. }
  531. return 0;
  532. }
  533. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  534. {
  535. int i;
  536. for (i = 0; i < count; i++) {
  537. int r = func(c, arg, i, 0);
  538. if (ret)
  539. ret[i] = r;
  540. }
  541. return 0;
  542. }
  543. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  544. {
  545. while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  546. ++fmt;
  547. return fmt[0];
  548. }
  549. void avcodec_get_frame_defaults(AVFrame *frame)
  550. {
  551. if (frame->extended_data != frame->data)
  552. av_freep(&frame->extended_data);
  553. memset(frame, 0, sizeof(AVFrame));
  554. frame->pts = AV_NOPTS_VALUE;
  555. frame->key_frame = 1;
  556. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  557. frame->format = -1; /* unknown */
  558. frame->extended_data = frame->data;
  559. }
  560. AVFrame *avcodec_alloc_frame(void)
  561. {
  562. AVFrame *frame = av_mallocz(sizeof(AVFrame));
  563. if (frame == NULL)
  564. return NULL;
  565. avcodec_get_frame_defaults(frame);
  566. return frame;
  567. }
  568. void avcodec_free_frame(AVFrame **frame)
  569. {
  570. AVFrame *f;
  571. if (!frame || !*frame)
  572. return;
  573. f = *frame;
  574. if (f->extended_data != f->data)
  575. av_freep(&f->extended_data);
  576. av_freep(frame);
  577. }
  578. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  579. {
  580. int ret = 0;
  581. AVDictionary *tmp = NULL;
  582. if (avcodec_is_open(avctx))
  583. return 0;
  584. if ((!codec && !avctx->codec)) {
  585. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  586. return AVERROR(EINVAL);
  587. }
  588. if ((codec && avctx->codec && codec != avctx->codec)) {
  589. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  590. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  591. return AVERROR(EINVAL);
  592. }
  593. if (!codec)
  594. codec = avctx->codec;
  595. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  596. return AVERROR(EINVAL);
  597. if (options)
  598. av_dict_copy(&tmp, *options, 0);
  599. /* If there is a user-supplied mutex locking routine, call it. */
  600. if (ff_lockmgr_cb) {
  601. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  602. return -1;
  603. }
  604. entangled_thread_counter++;
  605. if (entangled_thread_counter != 1) {
  606. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  607. ret = -1;
  608. goto end;
  609. }
  610. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  611. if (!avctx->internal) {
  612. ret = AVERROR(ENOMEM);
  613. goto end;
  614. }
  615. if (codec->priv_data_size > 0) {
  616. if (!avctx->priv_data) {
  617. avctx->priv_data = av_mallocz(codec->priv_data_size);
  618. if (!avctx->priv_data) {
  619. ret = AVERROR(ENOMEM);
  620. goto end;
  621. }
  622. if (codec->priv_class) {
  623. *(const AVClass **)avctx->priv_data = codec->priv_class;
  624. av_opt_set_defaults(avctx->priv_data);
  625. }
  626. }
  627. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  628. goto free_and_end;
  629. } else {
  630. avctx->priv_data = NULL;
  631. }
  632. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  633. goto free_and_end;
  634. if (avctx->coded_width && avctx->coded_height)
  635. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  636. else if (avctx->width && avctx->height)
  637. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  638. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  639. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  640. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  641. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  642. avcodec_set_dimensions(avctx, 0, 0);
  643. }
  644. /* if the decoder init function was already called previously,
  645. * free the already allocated subtitle_header before overwriting it */
  646. if (av_codec_is_decoder(codec))
  647. av_freep(&avctx->subtitle_header);
  648. #define SANE_NB_CHANNELS 128U
  649. if (avctx->channels > SANE_NB_CHANNELS) {
  650. ret = AVERROR(EINVAL);
  651. goto free_and_end;
  652. }
  653. avctx->codec = codec;
  654. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  655. avctx->codec_id == AV_CODEC_ID_NONE) {
  656. avctx->codec_type = codec->type;
  657. avctx->codec_id = codec->id;
  658. }
  659. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  660. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  661. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  662. ret = AVERROR(EINVAL);
  663. goto free_and_end;
  664. }
  665. avctx->frame_number = 0;
  666. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  667. (!avctx->time_base.num || !avctx->time_base.den)) {
  668. avctx->time_base.num = 1;
  669. avctx->time_base.den = avctx->sample_rate;
  670. }
  671. if (HAVE_THREADS && !avctx->thread_opaque) {
  672. ret = ff_thread_init(avctx);
  673. if (ret < 0) {
  674. goto free_and_end;
  675. }
  676. }
  677. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  678. avctx->thread_count = 1;
  679. if (av_codec_is_encoder(avctx->codec)) {
  680. int i;
  681. if (avctx->codec->sample_fmts) {
  682. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  683. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  684. break;
  685. if (avctx->channels == 1 &&
  686. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  687. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  688. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  689. break;
  690. }
  691. }
  692. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  693. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  694. ret = AVERROR(EINVAL);
  695. goto free_and_end;
  696. }
  697. }
  698. if (avctx->codec->pix_fmts) {
  699. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  700. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  701. break;
  702. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  703. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  704. ret = AVERROR(EINVAL);
  705. goto free_and_end;
  706. }
  707. }
  708. if (avctx->codec->supported_samplerates) {
  709. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  710. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  711. break;
  712. if (avctx->codec->supported_samplerates[i] == 0) {
  713. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  714. ret = AVERROR(EINVAL);
  715. goto free_and_end;
  716. }
  717. }
  718. if (avctx->codec->channel_layouts) {
  719. if (!avctx->channel_layout) {
  720. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  721. } else {
  722. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  723. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  724. break;
  725. if (avctx->codec->channel_layouts[i] == 0) {
  726. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  727. ret = AVERROR(EINVAL);
  728. goto free_and_end;
  729. }
  730. }
  731. }
  732. if (avctx->channel_layout && avctx->channels) {
  733. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  734. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  735. ret = AVERROR(EINVAL);
  736. goto free_and_end;
  737. }
  738. } else if (avctx->channel_layout) {
  739. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  740. }
  741. }
  742. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  743. ret = avctx->codec->init(avctx);
  744. if (ret < 0) {
  745. goto free_and_end;
  746. }
  747. }
  748. if (av_codec_is_decoder(avctx->codec)) {
  749. /* validate channel layout from the decoder */
  750. if (avctx->channel_layout) {
  751. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  752. if (!avctx->channels)
  753. avctx->channels = channels;
  754. else if (channels != avctx->channels) {
  755. av_log(avctx, AV_LOG_WARNING,
  756. "channel layout does not match number of channels\n");
  757. avctx->channel_layout = 0;
  758. }
  759. }
  760. }
  761. end:
  762. entangled_thread_counter--;
  763. /* Release any user-supplied mutex. */
  764. if (ff_lockmgr_cb) {
  765. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  766. }
  767. if (options) {
  768. av_dict_free(options);
  769. *options = tmp;
  770. }
  771. return ret;
  772. free_and_end:
  773. av_dict_free(&tmp);
  774. av_freep(&avctx->priv_data);
  775. av_freep(&avctx->internal);
  776. avctx->codec = NULL;
  777. goto end;
  778. }
  779. int ff_alloc_packet(AVPacket *avpkt, int size)
  780. {
  781. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  782. return AVERROR(EINVAL);
  783. if (avpkt->data) {
  784. void *destruct = avpkt->destruct;
  785. if (avpkt->size < size)
  786. return AVERROR(EINVAL);
  787. av_init_packet(avpkt);
  788. avpkt->destruct = destruct;
  789. avpkt->size = size;
  790. return 0;
  791. } else {
  792. return av_new_packet(avpkt, size);
  793. }
  794. }
  795. /**
  796. * Pad last frame with silence.
  797. */
  798. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  799. {
  800. AVFrame *frame = NULL;
  801. uint8_t *buf = NULL;
  802. int ret;
  803. if (!(frame = avcodec_alloc_frame()))
  804. return AVERROR(ENOMEM);
  805. *frame = *src;
  806. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  807. s->frame_size, s->sample_fmt, 0)) < 0)
  808. goto fail;
  809. if (!(buf = av_malloc(ret))) {
  810. ret = AVERROR(ENOMEM);
  811. goto fail;
  812. }
  813. frame->nb_samples = s->frame_size;
  814. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  815. buf, ret, 0)) < 0)
  816. goto fail;
  817. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  818. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  819. goto fail;
  820. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  821. frame->nb_samples - src->nb_samples,
  822. s->channels, s->sample_fmt)) < 0)
  823. goto fail;
  824. *dst = frame;
  825. return 0;
  826. fail:
  827. if (frame->extended_data != frame->data)
  828. av_freep(&frame->extended_data);
  829. av_freep(&buf);
  830. av_freep(&frame);
  831. return ret;
  832. }
  833. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  834. AVPacket *avpkt,
  835. const AVFrame *frame,
  836. int *got_packet_ptr)
  837. {
  838. AVFrame tmp;
  839. AVFrame *padded_frame = NULL;
  840. int ret;
  841. int user_packet = !!avpkt->data;
  842. *got_packet_ptr = 0;
  843. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  844. av_free_packet(avpkt);
  845. av_init_packet(avpkt);
  846. return 0;
  847. }
  848. /* ensure that extended_data is properly set */
  849. if (frame && !frame->extended_data) {
  850. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  851. avctx->channels > AV_NUM_DATA_POINTERS) {
  852. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  853. "with more than %d channels, but extended_data is not set.\n",
  854. AV_NUM_DATA_POINTERS);
  855. return AVERROR(EINVAL);
  856. }
  857. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  858. tmp = *frame;
  859. tmp.extended_data = tmp.data;
  860. frame = &tmp;
  861. }
  862. /* check for valid frame size */
  863. if (frame) {
  864. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  865. if (frame->nb_samples > avctx->frame_size)
  866. return AVERROR(EINVAL);
  867. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  868. if (frame->nb_samples < avctx->frame_size &&
  869. !avctx->internal->last_audio_frame) {
  870. ret = pad_last_frame(avctx, &padded_frame, frame);
  871. if (ret < 0)
  872. return ret;
  873. frame = padded_frame;
  874. avctx->internal->last_audio_frame = 1;
  875. }
  876. if (frame->nb_samples != avctx->frame_size) {
  877. ret = AVERROR(EINVAL);
  878. goto end;
  879. }
  880. }
  881. }
  882. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  883. if (!ret) {
  884. if (*got_packet_ptr) {
  885. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  886. if (avpkt->pts == AV_NOPTS_VALUE)
  887. avpkt->pts = frame->pts;
  888. if (!avpkt->duration)
  889. avpkt->duration = ff_samples_to_time_base(avctx,
  890. frame->nb_samples);
  891. }
  892. avpkt->dts = avpkt->pts;
  893. } else {
  894. avpkt->size = 0;
  895. }
  896. if (!user_packet && avpkt->size) {
  897. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  898. if (new_data)
  899. avpkt->data = new_data;
  900. }
  901. avctx->frame_number++;
  902. }
  903. if (ret < 0 || !*got_packet_ptr) {
  904. av_free_packet(avpkt);
  905. av_init_packet(avpkt);
  906. goto end;
  907. }
  908. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  909. * this needs to be moved to the encoders, but for now we can do it
  910. * here to simplify things */
  911. avpkt->flags |= AV_PKT_FLAG_KEY;
  912. end:
  913. if (padded_frame) {
  914. av_freep(&padded_frame->data[0]);
  915. if (padded_frame->extended_data != padded_frame->data)
  916. av_freep(&padded_frame->extended_data);
  917. av_freep(&padded_frame);
  918. }
  919. return ret;
  920. }
  921. #if FF_API_OLD_DECODE_AUDIO
  922. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  923. uint8_t *buf, int buf_size,
  924. const short *samples)
  925. {
  926. AVPacket pkt;
  927. AVFrame frame0;
  928. AVFrame *frame;
  929. int ret, samples_size, got_packet;
  930. av_init_packet(&pkt);
  931. pkt.data = buf;
  932. pkt.size = buf_size;
  933. if (samples) {
  934. frame = &frame0;
  935. avcodec_get_frame_defaults(frame);
  936. if (avctx->frame_size) {
  937. frame->nb_samples = avctx->frame_size;
  938. } else {
  939. /* if frame_size is not set, the number of samples must be
  940. * calculated from the buffer size */
  941. int64_t nb_samples;
  942. if (!av_get_bits_per_sample(avctx->codec_id)) {
  943. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  944. "support this codec\n");
  945. return AVERROR(EINVAL);
  946. }
  947. nb_samples = (int64_t)buf_size * 8 /
  948. (av_get_bits_per_sample(avctx->codec_id) *
  949. avctx->channels);
  950. if (nb_samples >= INT_MAX)
  951. return AVERROR(EINVAL);
  952. frame->nb_samples = nb_samples;
  953. }
  954. /* it is assumed that the samples buffer is large enough based on the
  955. * relevant parameters */
  956. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  957. frame->nb_samples,
  958. avctx->sample_fmt, 1);
  959. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  960. avctx->sample_fmt,
  961. (const uint8_t *)samples,
  962. samples_size, 1)))
  963. return ret;
  964. /* fabricate frame pts from sample count.
  965. * this is needed because the avcodec_encode_audio() API does not have
  966. * a way for the user to provide pts */
  967. frame->pts = ff_samples_to_time_base(avctx,
  968. avctx->internal->sample_count);
  969. avctx->internal->sample_count += frame->nb_samples;
  970. } else {
  971. frame = NULL;
  972. }
  973. got_packet = 0;
  974. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  975. if (!ret && got_packet && avctx->coded_frame) {
  976. avctx->coded_frame->pts = pkt.pts;
  977. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  978. }
  979. /* free any side data since we cannot return it */
  980. if (pkt.side_data_elems > 0) {
  981. int i;
  982. for (i = 0; i < pkt.side_data_elems; i++)
  983. av_free(pkt.side_data[i].data);
  984. av_freep(&pkt.side_data);
  985. pkt.side_data_elems = 0;
  986. }
  987. if (frame && frame->extended_data != frame->data)
  988. av_free(frame->extended_data);
  989. return ret ? ret : pkt.size;
  990. }
  991. #endif
  992. #if FF_API_OLD_ENCODE_VIDEO
  993. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  994. const AVFrame *pict)
  995. {
  996. AVPacket pkt;
  997. int ret, got_packet = 0;
  998. if (buf_size < FF_MIN_BUFFER_SIZE) {
  999. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1000. return -1;
  1001. }
  1002. av_init_packet(&pkt);
  1003. pkt.data = buf;
  1004. pkt.size = buf_size;
  1005. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1006. if (!ret && got_packet && avctx->coded_frame) {
  1007. avctx->coded_frame->pts = pkt.pts;
  1008. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1009. }
  1010. /* free any side data since we cannot return it */
  1011. if (pkt.side_data_elems > 0) {
  1012. int i;
  1013. for (i = 0; i < pkt.side_data_elems; i++)
  1014. av_free(pkt.side_data[i].data);
  1015. av_freep(&pkt.side_data);
  1016. pkt.side_data_elems = 0;
  1017. }
  1018. return ret ? ret : pkt.size;
  1019. }
  1020. #endif
  1021. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1022. AVPacket *avpkt,
  1023. const AVFrame *frame,
  1024. int *got_packet_ptr)
  1025. {
  1026. int ret;
  1027. int user_packet = !!avpkt->data;
  1028. *got_packet_ptr = 0;
  1029. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1030. av_free_packet(avpkt);
  1031. av_init_packet(avpkt);
  1032. avpkt->size = 0;
  1033. return 0;
  1034. }
  1035. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1036. return AVERROR(EINVAL);
  1037. av_assert0(avctx->codec->encode2);
  1038. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1039. if (!ret) {
  1040. if (!*got_packet_ptr)
  1041. avpkt->size = 0;
  1042. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1043. avpkt->pts = avpkt->dts = frame->pts;
  1044. if (!user_packet && avpkt->size) {
  1045. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  1046. if (new_data)
  1047. avpkt->data = new_data;
  1048. }
  1049. avctx->frame_number++;
  1050. }
  1051. if (ret < 0 || !*got_packet_ptr)
  1052. av_free_packet(avpkt);
  1053. emms_c();
  1054. return ret;
  1055. }
  1056. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1057. const AVSubtitle *sub)
  1058. {
  1059. int ret;
  1060. if (sub->start_display_time) {
  1061. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1062. return -1;
  1063. }
  1064. if (sub->num_rects == 0 || !sub->rects)
  1065. return -1;
  1066. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1067. avctx->frame_number++;
  1068. return ret;
  1069. }
  1070. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1071. {
  1072. int size = 0;
  1073. const uint8_t *data;
  1074. uint32_t flags;
  1075. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1076. return;
  1077. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1078. if (!data || size < 4)
  1079. return;
  1080. flags = bytestream_get_le32(&data);
  1081. size -= 4;
  1082. if (size < 4) /* Required for any of the changes */
  1083. return;
  1084. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1085. avctx->channels = bytestream_get_le32(&data);
  1086. size -= 4;
  1087. }
  1088. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1089. if (size < 8)
  1090. return;
  1091. avctx->channel_layout = bytestream_get_le64(&data);
  1092. size -= 8;
  1093. }
  1094. if (size < 4)
  1095. return;
  1096. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1097. avctx->sample_rate = bytestream_get_le32(&data);
  1098. size -= 4;
  1099. }
  1100. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1101. if (size < 8)
  1102. return;
  1103. avctx->width = bytestream_get_le32(&data);
  1104. avctx->height = bytestream_get_le32(&data);
  1105. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1106. size -= 8;
  1107. }
  1108. }
  1109. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1110. int *got_picture_ptr,
  1111. AVPacket *avpkt)
  1112. {
  1113. int ret;
  1114. *got_picture_ptr = 0;
  1115. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1116. return -1;
  1117. avctx->pkt = avpkt;
  1118. apply_param_change(avctx, avpkt);
  1119. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1120. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1121. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1122. avpkt);
  1123. else {
  1124. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1125. avpkt);
  1126. picture->pkt_dts = avpkt->dts;
  1127. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1128. picture->width = avctx->width;
  1129. picture->height = avctx->height;
  1130. picture->format = avctx->pix_fmt;
  1131. }
  1132. emms_c(); //needed to avoid an emms_c() call before every return;
  1133. if (*got_picture_ptr)
  1134. avctx->frame_number++;
  1135. } else
  1136. ret = 0;
  1137. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1138. * make sure it's set correctly */
  1139. picture->extended_data = picture->data;
  1140. return ret;
  1141. }
  1142. #if FF_API_OLD_DECODE_AUDIO
  1143. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1144. int *frame_size_ptr,
  1145. AVPacket *avpkt)
  1146. {
  1147. AVFrame frame;
  1148. int ret, got_frame = 0;
  1149. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1150. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1151. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1152. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1153. "avcodec_decode_audio4()\n");
  1154. avctx->get_buffer = avcodec_default_get_buffer;
  1155. }
  1156. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1157. if (ret >= 0 && got_frame) {
  1158. int ch, plane_size;
  1159. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1160. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1161. frame.nb_samples,
  1162. avctx->sample_fmt, 1);
  1163. if (*frame_size_ptr < data_size) {
  1164. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1165. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1166. return AVERROR(EINVAL);
  1167. }
  1168. memcpy(samples, frame.extended_data[0], plane_size);
  1169. if (planar && avctx->channels > 1) {
  1170. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1171. for (ch = 1; ch < avctx->channels; ch++) {
  1172. memcpy(out, frame.extended_data[ch], plane_size);
  1173. out += plane_size;
  1174. }
  1175. }
  1176. *frame_size_ptr = data_size;
  1177. } else {
  1178. *frame_size_ptr = 0;
  1179. }
  1180. return ret;
  1181. }
  1182. #endif
  1183. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1184. AVFrame *frame,
  1185. int *got_frame_ptr,
  1186. AVPacket *avpkt)
  1187. {
  1188. int planar, channels;
  1189. int ret = 0;
  1190. *got_frame_ptr = 0;
  1191. avctx->pkt = avpkt;
  1192. if (!avpkt->data && avpkt->size) {
  1193. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1194. return AVERROR(EINVAL);
  1195. }
  1196. apply_param_change(avctx, avpkt);
  1197. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1198. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1199. if (ret >= 0 && *got_frame_ptr) {
  1200. avctx->frame_number++;
  1201. frame->pkt_dts = avpkt->dts;
  1202. if (frame->format == AV_SAMPLE_FMT_NONE)
  1203. frame->format = avctx->sample_fmt;
  1204. }
  1205. }
  1206. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1207. * make sure it's set correctly; assume decoders that actually use
  1208. * extended_data are doing it correctly */
  1209. planar = av_sample_fmt_is_planar(frame->format);
  1210. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1211. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1212. frame->extended_data = frame->data;
  1213. return ret;
  1214. }
  1215. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1216. int *got_sub_ptr,
  1217. AVPacket *avpkt)
  1218. {
  1219. int ret;
  1220. avctx->pkt = avpkt;
  1221. *got_sub_ptr = 0;
  1222. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1223. if (*got_sub_ptr)
  1224. avctx->frame_number++;
  1225. return ret;
  1226. }
  1227. void avsubtitle_free(AVSubtitle *sub)
  1228. {
  1229. int i;
  1230. for (i = 0; i < sub->num_rects; i++) {
  1231. av_freep(&sub->rects[i]->pict.data[0]);
  1232. av_freep(&sub->rects[i]->pict.data[1]);
  1233. av_freep(&sub->rects[i]->pict.data[2]);
  1234. av_freep(&sub->rects[i]->pict.data[3]);
  1235. av_freep(&sub->rects[i]->text);
  1236. av_freep(&sub->rects[i]->ass);
  1237. av_freep(&sub->rects[i]);
  1238. }
  1239. av_freep(&sub->rects);
  1240. memset(sub, 0, sizeof(AVSubtitle));
  1241. }
  1242. av_cold int avcodec_close(AVCodecContext *avctx)
  1243. {
  1244. /* If there is a user-supplied mutex locking routine, call it. */
  1245. if (ff_lockmgr_cb) {
  1246. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1247. return -1;
  1248. }
  1249. entangled_thread_counter++;
  1250. if (entangled_thread_counter != 1) {
  1251. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1252. entangled_thread_counter--;
  1253. return -1;
  1254. }
  1255. if (avcodec_is_open(avctx)) {
  1256. if (HAVE_THREADS && avctx->thread_opaque)
  1257. ff_thread_free(avctx);
  1258. if (avctx->codec && avctx->codec->close)
  1259. avctx->codec->close(avctx);
  1260. avcodec_default_free_buffers(avctx);
  1261. avctx->coded_frame = NULL;
  1262. av_freep(&avctx->internal);
  1263. }
  1264. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1265. av_opt_free(avctx->priv_data);
  1266. av_opt_free(avctx);
  1267. av_freep(&avctx->priv_data);
  1268. if (av_codec_is_encoder(avctx->codec))
  1269. av_freep(&avctx->extradata);
  1270. avctx->codec = NULL;
  1271. avctx->active_thread_type = 0;
  1272. entangled_thread_counter--;
  1273. /* Release any user-supplied mutex. */
  1274. if (ff_lockmgr_cb) {
  1275. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1276. }
  1277. return 0;
  1278. }
  1279. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1280. {
  1281. AVCodec *p, *experimental = NULL;
  1282. p = first_avcodec;
  1283. while (p) {
  1284. if (av_codec_is_encoder(p) && p->id == id) {
  1285. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1286. experimental = p;
  1287. } else
  1288. return p;
  1289. }
  1290. p = p->next;
  1291. }
  1292. return experimental;
  1293. }
  1294. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1295. {
  1296. AVCodec *p;
  1297. if (!name)
  1298. return NULL;
  1299. p = first_avcodec;
  1300. while (p) {
  1301. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1302. return p;
  1303. p = p->next;
  1304. }
  1305. return NULL;
  1306. }
  1307. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1308. {
  1309. AVCodec *p;
  1310. p = first_avcodec;
  1311. while (p) {
  1312. if (av_codec_is_decoder(p) && p->id == id)
  1313. return p;
  1314. p = p->next;
  1315. }
  1316. return NULL;
  1317. }
  1318. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1319. {
  1320. AVCodec *p;
  1321. if (!name)
  1322. return NULL;
  1323. p = first_avcodec;
  1324. while (p) {
  1325. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1326. return p;
  1327. p = p->next;
  1328. }
  1329. return NULL;
  1330. }
  1331. static int get_bit_rate(AVCodecContext *ctx)
  1332. {
  1333. int bit_rate;
  1334. int bits_per_sample;
  1335. switch (ctx->codec_type) {
  1336. case AVMEDIA_TYPE_VIDEO:
  1337. case AVMEDIA_TYPE_DATA:
  1338. case AVMEDIA_TYPE_SUBTITLE:
  1339. case AVMEDIA_TYPE_ATTACHMENT:
  1340. bit_rate = ctx->bit_rate;
  1341. break;
  1342. case AVMEDIA_TYPE_AUDIO:
  1343. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1344. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1345. break;
  1346. default:
  1347. bit_rate = 0;
  1348. break;
  1349. }
  1350. return bit_rate;
  1351. }
  1352. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1353. {
  1354. int i, len, ret = 0;
  1355. for (i = 0; i < 4; i++) {
  1356. len = snprintf(buf, buf_size,
  1357. isprint(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1358. buf += len;
  1359. buf_size = buf_size > len ? buf_size - len : 0;
  1360. ret += len;
  1361. codec_tag >>= 8;
  1362. }
  1363. return ret;
  1364. }
  1365. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1366. {
  1367. const char *codec_name;
  1368. const char *profile = NULL;
  1369. const AVCodec *p;
  1370. char buf1[32];
  1371. int bitrate;
  1372. AVRational display_aspect_ratio;
  1373. if (enc->codec)
  1374. p = enc->codec;
  1375. else if (encode)
  1376. p = avcodec_find_encoder(enc->codec_id);
  1377. else
  1378. p = avcodec_find_decoder(enc->codec_id);
  1379. if (p) {
  1380. codec_name = p->name;
  1381. profile = av_get_profile_name(p, enc->profile);
  1382. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1383. /* fake mpeg2 transport stream codec (currently not
  1384. * registered) */
  1385. codec_name = "mpeg2ts";
  1386. } else if (enc->codec_name[0] != '\0') {
  1387. codec_name = enc->codec_name;
  1388. } else {
  1389. /* output avi tags */
  1390. char tag_buf[32];
  1391. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1392. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1393. codec_name = buf1;
  1394. }
  1395. switch (enc->codec_type) {
  1396. case AVMEDIA_TYPE_VIDEO:
  1397. snprintf(buf, buf_size,
  1398. "Video: %s%s",
  1399. codec_name, enc->mb_decision ? " (hq)" : "");
  1400. if (profile)
  1401. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1402. " (%s)", profile);
  1403. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1404. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1405. ", %s",
  1406. av_get_pix_fmt_name(enc->pix_fmt));
  1407. }
  1408. if (enc->width) {
  1409. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1410. ", %dx%d",
  1411. enc->width, enc->height);
  1412. if (enc->sample_aspect_ratio.num) {
  1413. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1414. enc->width * enc->sample_aspect_ratio.num,
  1415. enc->height * enc->sample_aspect_ratio.den,
  1416. 1024 * 1024);
  1417. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1418. " [PAR %d:%d DAR %d:%d]",
  1419. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1420. display_aspect_ratio.num, display_aspect_ratio.den);
  1421. }
  1422. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1423. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1424. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1425. ", %d/%d",
  1426. enc->time_base.num / g, enc->time_base.den / g);
  1427. }
  1428. }
  1429. if (encode) {
  1430. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1431. ", q=%d-%d", enc->qmin, enc->qmax);
  1432. }
  1433. break;
  1434. case AVMEDIA_TYPE_AUDIO:
  1435. snprintf(buf, buf_size,
  1436. "Audio: %s",
  1437. codec_name);
  1438. if (profile)
  1439. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1440. " (%s)", profile);
  1441. if (enc->sample_rate) {
  1442. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1443. ", %d Hz", enc->sample_rate);
  1444. }
  1445. av_strlcat(buf, ", ", buf_size);
  1446. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1447. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1448. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1449. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1450. }
  1451. break;
  1452. case AVMEDIA_TYPE_DATA:
  1453. snprintf(buf, buf_size, "Data: %s", codec_name);
  1454. break;
  1455. case AVMEDIA_TYPE_SUBTITLE:
  1456. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1457. break;
  1458. case AVMEDIA_TYPE_ATTACHMENT:
  1459. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1460. break;
  1461. default:
  1462. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1463. return;
  1464. }
  1465. if (encode) {
  1466. if (enc->flags & CODEC_FLAG_PASS1)
  1467. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1468. ", pass 1");
  1469. if (enc->flags & CODEC_FLAG_PASS2)
  1470. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1471. ", pass 2");
  1472. }
  1473. bitrate = get_bit_rate(enc);
  1474. if (bitrate != 0) {
  1475. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1476. ", %d kb/s", bitrate / 1000);
  1477. }
  1478. }
  1479. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1480. {
  1481. const AVProfile *p;
  1482. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1483. return NULL;
  1484. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1485. if (p->profile == profile)
  1486. return p->name;
  1487. return NULL;
  1488. }
  1489. unsigned avcodec_version(void)
  1490. {
  1491. return LIBAVCODEC_VERSION_INT;
  1492. }
  1493. const char *avcodec_configuration(void)
  1494. {
  1495. return LIBAV_CONFIGURATION;
  1496. }
  1497. const char *avcodec_license(void)
  1498. {
  1499. #define LICENSE_PREFIX "libavcodec license: "
  1500. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1501. }
  1502. void avcodec_flush_buffers(AVCodecContext *avctx)
  1503. {
  1504. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1505. ff_thread_flush(avctx);
  1506. else if (avctx->codec->flush)
  1507. avctx->codec->flush(avctx);
  1508. }
  1509. static void video_free_buffers(AVCodecContext *s)
  1510. {
  1511. AVCodecInternal *avci = s->internal;
  1512. int i, j;
  1513. if (!avci->buffer)
  1514. return;
  1515. if (avci->buffer_count)
  1516. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1517. avci->buffer_count);
  1518. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1519. InternalBuffer *buf = &avci->buffer[i];
  1520. for (j = 0; j < 4; j++) {
  1521. av_freep(&buf->base[j]);
  1522. buf->data[j] = NULL;
  1523. }
  1524. }
  1525. av_freep(&avci->buffer);
  1526. avci->buffer_count = 0;
  1527. }
  1528. static void audio_free_buffers(AVCodecContext *avctx)
  1529. {
  1530. AVCodecInternal *avci = avctx->internal;
  1531. InternalBuffer *buf;
  1532. if (!avci->buffer)
  1533. return;
  1534. buf = avci->buffer;
  1535. if (buf->extended_data) {
  1536. av_free(buf->extended_data[0]);
  1537. if (buf->extended_data != buf->data)
  1538. av_free(buf->extended_data);
  1539. }
  1540. av_freep(&avci->buffer);
  1541. }
  1542. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1543. {
  1544. switch (avctx->codec_type) {
  1545. case AVMEDIA_TYPE_VIDEO:
  1546. video_free_buffers(avctx);
  1547. break;
  1548. case AVMEDIA_TYPE_AUDIO:
  1549. audio_free_buffers(avctx);
  1550. break;
  1551. default:
  1552. break;
  1553. }
  1554. }
  1555. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1556. {
  1557. switch (codec_id) {
  1558. case AV_CODEC_ID_ADPCM_CT:
  1559. case AV_CODEC_ID_ADPCM_IMA_APC:
  1560. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1561. case AV_CODEC_ID_ADPCM_IMA_WS:
  1562. case AV_CODEC_ID_ADPCM_G722:
  1563. case AV_CODEC_ID_ADPCM_YAMAHA:
  1564. return 4;
  1565. case AV_CODEC_ID_PCM_ALAW:
  1566. case AV_CODEC_ID_PCM_MULAW:
  1567. case AV_CODEC_ID_PCM_S8:
  1568. case AV_CODEC_ID_PCM_U8:
  1569. case AV_CODEC_ID_PCM_ZORK:
  1570. return 8;
  1571. case AV_CODEC_ID_PCM_S16BE:
  1572. case AV_CODEC_ID_PCM_S16LE:
  1573. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1574. case AV_CODEC_ID_PCM_U16BE:
  1575. case AV_CODEC_ID_PCM_U16LE:
  1576. return 16;
  1577. case AV_CODEC_ID_PCM_S24DAUD:
  1578. case AV_CODEC_ID_PCM_S24BE:
  1579. case AV_CODEC_ID_PCM_S24LE:
  1580. case AV_CODEC_ID_PCM_U24BE:
  1581. case AV_CODEC_ID_PCM_U24LE:
  1582. return 24;
  1583. case AV_CODEC_ID_PCM_S32BE:
  1584. case AV_CODEC_ID_PCM_S32LE:
  1585. case AV_CODEC_ID_PCM_U32BE:
  1586. case AV_CODEC_ID_PCM_U32LE:
  1587. case AV_CODEC_ID_PCM_F32BE:
  1588. case AV_CODEC_ID_PCM_F32LE:
  1589. return 32;
  1590. case AV_CODEC_ID_PCM_F64BE:
  1591. case AV_CODEC_ID_PCM_F64LE:
  1592. return 64;
  1593. default:
  1594. return 0;
  1595. }
  1596. }
  1597. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1598. {
  1599. switch (codec_id) {
  1600. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1601. return 2;
  1602. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1603. return 3;
  1604. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1605. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1606. case AV_CODEC_ID_ADPCM_IMA_QT:
  1607. case AV_CODEC_ID_ADPCM_SWF:
  1608. case AV_CODEC_ID_ADPCM_MS:
  1609. return 4;
  1610. default:
  1611. return av_get_exact_bits_per_sample(codec_id);
  1612. }
  1613. }
  1614. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1615. {
  1616. int id, sr, ch, ba, tag, bps;
  1617. id = avctx->codec_id;
  1618. sr = avctx->sample_rate;
  1619. ch = avctx->channels;
  1620. ba = avctx->block_align;
  1621. tag = avctx->codec_tag;
  1622. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1623. /* codecs with an exact constant bits per sample */
  1624. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1625. return (frame_bytes * 8) / (bps * ch);
  1626. bps = avctx->bits_per_coded_sample;
  1627. /* codecs with a fixed packet duration */
  1628. switch (id) {
  1629. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1630. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1631. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1632. case AV_CODEC_ID_AMR_NB:
  1633. case AV_CODEC_ID_GSM:
  1634. case AV_CODEC_ID_QCELP:
  1635. case AV_CODEC_ID_RA_144:
  1636. case AV_CODEC_ID_RA_288: return 160;
  1637. case AV_CODEC_ID_IMC: return 256;
  1638. case AV_CODEC_ID_AMR_WB:
  1639. case AV_CODEC_ID_GSM_MS: return 320;
  1640. case AV_CODEC_ID_MP1: return 384;
  1641. case AV_CODEC_ID_ATRAC1: return 512;
  1642. case AV_CODEC_ID_ATRAC3: return 1024;
  1643. case AV_CODEC_ID_MP2:
  1644. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1645. case AV_CODEC_ID_AC3: return 1536;
  1646. }
  1647. if (sr > 0) {
  1648. /* calc from sample rate */
  1649. if (id == AV_CODEC_ID_TTA)
  1650. return 256 * sr / 245;
  1651. if (ch > 0) {
  1652. /* calc from sample rate and channels */
  1653. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1654. return (480 << (sr / 22050)) / ch;
  1655. }
  1656. }
  1657. if (ba > 0) {
  1658. /* calc from block_align */
  1659. if (id == AV_CODEC_ID_SIPR) {
  1660. switch (ba) {
  1661. case 20: return 160;
  1662. case 19: return 144;
  1663. case 29: return 288;
  1664. case 37: return 480;
  1665. }
  1666. } else if (id == AV_CODEC_ID_ILBC) {
  1667. switch (ba) {
  1668. case 38: return 160;
  1669. case 50: return 240;
  1670. }
  1671. }
  1672. }
  1673. if (frame_bytes > 0) {
  1674. /* calc from frame_bytes only */
  1675. if (id == AV_CODEC_ID_TRUESPEECH)
  1676. return 240 * (frame_bytes / 32);
  1677. if (id == AV_CODEC_ID_NELLYMOSER)
  1678. return 256 * (frame_bytes / 64);
  1679. if (bps > 0) {
  1680. /* calc from frame_bytes and bits_per_coded_sample */
  1681. if (id == AV_CODEC_ID_ADPCM_G726)
  1682. return frame_bytes * 8 / bps;
  1683. }
  1684. if (ch > 0) {
  1685. /* calc from frame_bytes and channels */
  1686. switch (id) {
  1687. case AV_CODEC_ID_ADPCM_4XM:
  1688. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1689. return (frame_bytes - 4 * ch) * 2 / ch;
  1690. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1691. return (frame_bytes - 4) * 2 / ch;
  1692. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1693. return (frame_bytes - 8) * 2 / ch;
  1694. case AV_CODEC_ID_ADPCM_XA:
  1695. return (frame_bytes / 128) * 224 / ch;
  1696. case AV_CODEC_ID_INTERPLAY_DPCM:
  1697. return (frame_bytes - 6 - ch) / ch;
  1698. case AV_CODEC_ID_ROQ_DPCM:
  1699. return (frame_bytes - 8) / ch;
  1700. case AV_CODEC_ID_XAN_DPCM:
  1701. return (frame_bytes - 2 * ch) / ch;
  1702. case AV_CODEC_ID_MACE3:
  1703. return 3 * frame_bytes / ch;
  1704. case AV_CODEC_ID_MACE6:
  1705. return 6 * frame_bytes / ch;
  1706. case AV_CODEC_ID_PCM_LXF:
  1707. return 2 * (frame_bytes / (5 * ch));
  1708. }
  1709. if (tag) {
  1710. /* calc from frame_bytes, channels, and codec_tag */
  1711. if (id == AV_CODEC_ID_SOL_DPCM) {
  1712. if (tag == 3)
  1713. return frame_bytes / ch;
  1714. else
  1715. return frame_bytes * 2 / ch;
  1716. }
  1717. }
  1718. if (ba > 0) {
  1719. /* calc from frame_bytes, channels, and block_align */
  1720. int blocks = frame_bytes / ba;
  1721. switch (avctx->codec_id) {
  1722. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1723. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1724. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1725. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1726. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1727. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1728. case AV_CODEC_ID_ADPCM_MS:
  1729. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1730. }
  1731. }
  1732. if (bps > 0) {
  1733. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1734. switch (avctx->codec_id) {
  1735. case AV_CODEC_ID_PCM_DVD:
  1736. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1737. case AV_CODEC_ID_PCM_BLURAY:
  1738. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1739. case AV_CODEC_ID_S302M:
  1740. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1741. }
  1742. }
  1743. }
  1744. }
  1745. return 0;
  1746. }
  1747. #if !HAVE_THREADS
  1748. int ff_thread_init(AVCodecContext *s)
  1749. {
  1750. return -1;
  1751. }
  1752. #endif
  1753. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1754. {
  1755. unsigned int n = 0;
  1756. while (v >= 0xff) {
  1757. *s++ = 0xff;
  1758. v -= 0xff;
  1759. n++;
  1760. }
  1761. *s = v;
  1762. n++;
  1763. return n;
  1764. }
  1765. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1766. {
  1767. int i;
  1768. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1769. return i;
  1770. }
  1771. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1772. {
  1773. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  1774. "version to the newest one from Git. If the problem still "
  1775. "occurs, it means that your file has a feature which has not "
  1776. "been implemented.\n", feature);
  1777. if(want_sample)
  1778. av_log_ask_for_sample(avc, NULL);
  1779. }
  1780. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1781. {
  1782. va_list argument_list;
  1783. va_start(argument_list, msg);
  1784. if (msg)
  1785. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1786. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1787. "of this file to ftp://upload.libav.org/incoming/ "
  1788. "and contact the libav-devel mailing list.\n");
  1789. va_end(argument_list);
  1790. }
  1791. static AVHWAccel *first_hwaccel = NULL;
  1792. void av_register_hwaccel(AVHWAccel *hwaccel)
  1793. {
  1794. AVHWAccel **p = &first_hwaccel;
  1795. while (*p)
  1796. p = &(*p)->next;
  1797. *p = hwaccel;
  1798. hwaccel->next = NULL;
  1799. }
  1800. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1801. {
  1802. return hwaccel ? hwaccel->next : first_hwaccel;
  1803. }
  1804. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  1805. {
  1806. AVHWAccel *hwaccel = NULL;
  1807. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1808. if (hwaccel->id == codec_id
  1809. && hwaccel->pix_fmt == pix_fmt)
  1810. return hwaccel;
  1811. return NULL;
  1812. }
  1813. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1814. {
  1815. if (ff_lockmgr_cb) {
  1816. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1817. return -1;
  1818. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1819. return -1;
  1820. }
  1821. ff_lockmgr_cb = cb;
  1822. if (ff_lockmgr_cb) {
  1823. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1824. return -1;
  1825. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1826. return -1;
  1827. }
  1828. return 0;
  1829. }
  1830. int avpriv_lock_avformat(void)
  1831. {
  1832. if (ff_lockmgr_cb) {
  1833. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1834. return -1;
  1835. }
  1836. return 0;
  1837. }
  1838. int avpriv_unlock_avformat(void)
  1839. {
  1840. if (ff_lockmgr_cb) {
  1841. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1842. return -1;
  1843. }
  1844. return 0;
  1845. }
  1846. unsigned int avpriv_toupper4(unsigned int x)
  1847. {
  1848. return toupper(x & 0xFF)
  1849. + (toupper((x >> 8) & 0xFF) << 8)
  1850. + (toupper((x >> 16) & 0xFF) << 16)
  1851. + (toupper((x >> 24) & 0xFF) << 24);
  1852. }
  1853. #if !HAVE_THREADS
  1854. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1855. {
  1856. f->owner = avctx;
  1857. return avctx->get_buffer(avctx, f);
  1858. }
  1859. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1860. {
  1861. f->owner->release_buffer(f->owner, f);
  1862. }
  1863. void ff_thread_finish_setup(AVCodecContext *avctx)
  1864. {
  1865. }
  1866. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1867. {
  1868. }
  1869. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1870. {
  1871. }
  1872. #endif
  1873. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  1874. {
  1875. if (codec_id <= AV_CODEC_ID_NONE)
  1876. return AVMEDIA_TYPE_UNKNOWN;
  1877. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  1878. return AVMEDIA_TYPE_VIDEO;
  1879. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  1880. return AVMEDIA_TYPE_AUDIO;
  1881. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  1882. return AVMEDIA_TYPE_SUBTITLE;
  1883. return AVMEDIA_TYPE_UNKNOWN;
  1884. }
  1885. int avcodec_is_open(AVCodecContext *s)
  1886. {
  1887. return !!s->internal;
  1888. }