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.

2165 lines
65KB

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