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.

2355 lines
73KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/avassert.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "imgconvert.h"
  40. #include "thread.h"
  41. #include "audioconvert.h"
  42. #include "internal.h"
  43. #include "bytestream.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. static int volatile entangled_thread_counter=0;
  49. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  50. static void *codec_mutex;
  51. static void *avformat_mutex;
  52. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  53. {
  54. if(min_size < *size)
  55. return ptr;
  56. min_size= FFMAX(17*min_size/16 + 32, min_size);
  57. ptr= av_realloc(ptr, min_size);
  58. if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
  59. min_size= 0;
  60. *size= min_size;
  61. return ptr;
  62. }
  63. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  64. {
  65. void **p = ptr;
  66. if (min_size < *size)
  67. return 0;
  68. min_size= FFMAX(17*min_size/16 + 32, min_size);
  69. av_free(*p);
  70. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  71. if (!*p) min_size = 0;
  72. *size= min_size;
  73. return 1;
  74. }
  75. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  76. {
  77. ff_fast_malloc(ptr, size, min_size, 0);
  78. }
  79. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  80. {
  81. uint8_t **p = ptr;
  82. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  83. av_freep(p);
  84. *size = 0;
  85. return;
  86. }
  87. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  88. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  89. }
  90. /* encoder management */
  91. static AVCodec *first_avcodec = NULL;
  92. AVCodec *av_codec_next(AVCodec *c){
  93. if(c) return c->next;
  94. else return first_avcodec;
  95. }
  96. static void avcodec_init(void)
  97. {
  98. static int initialized = 0;
  99. if (initialized != 0)
  100. return;
  101. initialized = 1;
  102. ff_dsputil_static_init();
  103. }
  104. int av_codec_is_encoder(AVCodec *codec)
  105. {
  106. return codec && (codec->encode || codec->encode2);
  107. }
  108. int av_codec_is_decoder(AVCodec *codec)
  109. {
  110. return codec && codec->decode;
  111. }
  112. void avcodec_register(AVCodec *codec)
  113. {
  114. AVCodec **p;
  115. avcodec_init();
  116. p = &first_avcodec;
  117. while (*p != NULL) p = &(*p)->next;
  118. *p = codec;
  119. codec->next = NULL;
  120. if (codec->init_static_data)
  121. codec->init_static_data(codec);
  122. }
  123. unsigned avcodec_get_edge_width(void)
  124. {
  125. return EDGE_WIDTH;
  126. }
  127. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  128. s->coded_width = width;
  129. s->coded_height= height;
  130. s->width = -((-width )>>s->lowres);
  131. s->height= -((-height)>>s->lowres);
  132. }
  133. #define INTERNAL_BUFFER_SIZE (32+1)
  134. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  135. int linesize_align[AV_NUM_DATA_POINTERS])
  136. {
  137. int i;
  138. int w_align= 1;
  139. int h_align= 1;
  140. switch(s->pix_fmt){
  141. case PIX_FMT_YUV420P:
  142. case PIX_FMT_YUYV422:
  143. case PIX_FMT_UYVY422:
  144. case PIX_FMT_YUV422P:
  145. case PIX_FMT_YUV440P:
  146. case PIX_FMT_YUV444P:
  147. case PIX_FMT_GBRP:
  148. case PIX_FMT_GRAY8:
  149. case PIX_FMT_GRAY16BE:
  150. case PIX_FMT_GRAY16LE:
  151. case PIX_FMT_YUVJ420P:
  152. case PIX_FMT_YUVJ422P:
  153. case PIX_FMT_YUVJ440P:
  154. case PIX_FMT_YUVJ444P:
  155. case PIX_FMT_YUVA420P:
  156. case PIX_FMT_YUVA444P:
  157. case PIX_FMT_YUV420P9LE:
  158. case PIX_FMT_YUV420P9BE:
  159. case PIX_FMT_YUV420P10LE:
  160. case PIX_FMT_YUV420P10BE:
  161. case PIX_FMT_YUV422P9LE:
  162. case PIX_FMT_YUV422P9BE:
  163. case PIX_FMT_YUV422P10LE:
  164. case PIX_FMT_YUV422P10BE:
  165. case PIX_FMT_YUV444P9LE:
  166. case PIX_FMT_YUV444P9BE:
  167. case PIX_FMT_YUV444P10LE:
  168. case PIX_FMT_YUV444P10BE:
  169. case PIX_FMT_GBRP9LE:
  170. case PIX_FMT_GBRP9BE:
  171. case PIX_FMT_GBRP10LE:
  172. case PIX_FMT_GBRP10BE:
  173. w_align = 16; //FIXME assume 16 pixel per macroblock
  174. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  175. break;
  176. case PIX_FMT_YUV411P:
  177. case PIX_FMT_UYYVYY411:
  178. w_align=32;
  179. h_align=8;
  180. break;
  181. case PIX_FMT_YUV410P:
  182. if(s->codec_id == CODEC_ID_SVQ1){
  183. w_align=64;
  184. h_align=64;
  185. }
  186. case PIX_FMT_RGB555:
  187. if(s->codec_id == CODEC_ID_RPZA){
  188. w_align=4;
  189. h_align=4;
  190. }
  191. case PIX_FMT_PAL8:
  192. case PIX_FMT_BGR8:
  193. case PIX_FMT_RGB8:
  194. if(s->codec_id == CODEC_ID_SMC){
  195. w_align=4;
  196. h_align=4;
  197. }
  198. break;
  199. case PIX_FMT_BGR24:
  200. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  201. w_align=4;
  202. h_align=4;
  203. }
  204. break;
  205. default:
  206. w_align= 1;
  207. h_align= 1;
  208. break;
  209. }
  210. if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
  211. w_align= FFMAX(w_align, 8);
  212. }
  213. *width = FFALIGN(*width , w_align);
  214. *height= FFALIGN(*height, h_align);
  215. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  216. *height+=2; // some of the optimized chroma MC reads one line too much
  217. // which is also done in mpeg decoders with lowres > 0
  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. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  223. int linesize_align[AV_NUM_DATA_POINTERS];
  224. int align;
  225. avcodec_align_dimensions2(s, width, height, linesize_align);
  226. align = FFMAX(linesize_align[0], linesize_align[3]);
  227. linesize_align[1] <<= chroma_shift;
  228. linesize_align[2] <<= chroma_shift;
  229. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  230. *width=FFALIGN(*width, align);
  231. }
  232. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  233. {
  234. if (s->pkt) {
  235. pic->pkt_pts = s->pkt->pts;
  236. pic->pkt_pos = s->pkt->pos;
  237. } else {
  238. pic->pkt_pts = AV_NOPTS_VALUE;
  239. pic->pkt_pos = -1;
  240. }
  241. pic->reordered_opaque= s->reordered_opaque;
  242. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  243. pic->width = s->width;
  244. pic->height = s->height;
  245. pic->format = s->pix_fmt;
  246. }
  247. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  248. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  249. int buf_size, int align)
  250. {
  251. int ch, planar, needed_size, ret = 0;
  252. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  253. frame->nb_samples, sample_fmt,
  254. align);
  255. if (buf_size < needed_size)
  256. return AVERROR(EINVAL);
  257. planar = av_sample_fmt_is_planar(sample_fmt);
  258. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  259. if (!(frame->extended_data = av_mallocz(nb_channels *
  260. sizeof(*frame->extended_data))))
  261. return AVERROR(ENOMEM);
  262. } else {
  263. frame->extended_data = frame->data;
  264. }
  265. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  266. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  267. sample_fmt, align)) < 0) {
  268. if (frame->extended_data != frame->data)
  269. av_freep(&frame->extended_data);
  270. return ret;
  271. }
  272. if (frame->extended_data != frame->data) {
  273. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  274. frame->data[ch] = frame->extended_data[ch];
  275. }
  276. return ret;
  277. }
  278. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  279. {
  280. AVCodecInternal *avci = avctx->internal;
  281. InternalBuffer *buf;
  282. int buf_size, ret;
  283. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  284. frame->nb_samples, avctx->sample_fmt,
  285. 0);
  286. if (buf_size < 0)
  287. return AVERROR(EINVAL);
  288. /* allocate InternalBuffer if needed */
  289. if (!avci->buffer) {
  290. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  291. if (!avci->buffer)
  292. return AVERROR(ENOMEM);
  293. }
  294. buf = avci->buffer;
  295. /* if there is a previously-used internal buffer, check its size and
  296. channel count to see if we can reuse it */
  297. if (buf->extended_data) {
  298. /* if current buffer is too small, free it */
  299. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  300. av_free(buf->extended_data[0]);
  301. if (buf->extended_data != buf->data)
  302. av_freep(&buf->extended_data);
  303. buf->extended_data = NULL;
  304. buf->data[0] = NULL;
  305. }
  306. /* if number of channels has changed, reset and/or free extended data
  307. pointers but leave data buffer in buf->data[0] for reuse */
  308. if (buf->nb_channels != avctx->channels) {
  309. if (buf->extended_data != buf->data)
  310. av_free(buf->extended_data);
  311. buf->extended_data = NULL;
  312. }
  313. }
  314. /* if there is no previous buffer or the previous buffer cannot be used
  315. as-is, allocate a new buffer and/or rearrange the channel pointers */
  316. if (!buf->extended_data) {
  317. if (!buf->data[0]) {
  318. if (!(buf->data[0] = av_mallocz(buf_size)))
  319. return AVERROR(ENOMEM);
  320. buf->audio_data_size = buf_size;
  321. }
  322. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  323. avctx->sample_fmt, buf->data[0],
  324. buf->audio_data_size, 0)))
  325. return ret;
  326. if (frame->extended_data == frame->data)
  327. buf->extended_data = buf->data;
  328. else
  329. buf->extended_data = frame->extended_data;
  330. memcpy(buf->data, frame->data, sizeof(frame->data));
  331. buf->linesize[0] = frame->linesize[0];
  332. buf->nb_channels = avctx->channels;
  333. } else {
  334. /* copy InternalBuffer info to the AVFrame */
  335. frame->extended_data = buf->extended_data;
  336. frame->linesize[0] = buf->linesize[0];
  337. memcpy(frame->data, buf->data, sizeof(frame->data));
  338. }
  339. frame->type = FF_BUFFER_TYPE_INTERNAL;
  340. if (avctx->pkt) {
  341. frame->pkt_pts = avctx->pkt->pts;
  342. frame->pkt_pos = avctx->pkt->pos;
  343. } else {
  344. frame->pkt_pts = AV_NOPTS_VALUE;
  345. frame->pkt_pos = -1;
  346. }
  347. frame->reordered_opaque = avctx->reordered_opaque;
  348. if (avctx->debug & FF_DEBUG_BUFFERS)
  349. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  350. "internal audio buffer used\n", frame);
  351. return 0;
  352. }
  353. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  354. {
  355. int i;
  356. int w= s->width;
  357. int h= s->height;
  358. InternalBuffer *buf;
  359. AVCodecInternal *avci = s->internal;
  360. if(pic->data[0]!=NULL) {
  361. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  362. return -1;
  363. }
  364. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  365. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  366. return -1;
  367. }
  368. if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0)
  369. return -1;
  370. if (!avci->buffer) {
  371. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  372. sizeof(InternalBuffer));
  373. }
  374. buf = &avci->buffer[avci->buffer_count];
  375. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  376. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  377. av_freep(&buf->base[i]);
  378. buf->data[i]= NULL;
  379. }
  380. }
  381. if (!buf->base[0]) {
  382. int h_chroma_shift, v_chroma_shift;
  383. int size[4] = {0};
  384. int tmpsize;
  385. int unaligned;
  386. AVPicture picture;
  387. int stride_align[AV_NUM_DATA_POINTERS];
  388. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  389. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  390. avcodec_align_dimensions2(s, &w, &h, stride_align);
  391. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  392. w+= EDGE_WIDTH*2;
  393. h+= EDGE_WIDTH*2;
  394. }
  395. do {
  396. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  397. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  398. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  399. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  400. w += w & ~(w-1);
  401. unaligned = 0;
  402. for (i=0; i<4; i++){
  403. unaligned |= picture.linesize[i] % stride_align[i];
  404. }
  405. } while (unaligned);
  406. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  407. if (tmpsize < 0)
  408. return -1;
  409. for (i=0; i<3 && picture.data[i+1]; i++)
  410. size[i] = picture.data[i+1] - picture.data[i];
  411. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  412. memset(buf->base, 0, sizeof(buf->base));
  413. memset(buf->data, 0, sizeof(buf->data));
  414. for(i=0; i<4 && size[i]; i++){
  415. const int h_shift= i==0 ? 0 : h_chroma_shift;
  416. const int v_shift= i==0 ? 0 : v_chroma_shift;
  417. buf->linesize[i]= picture.linesize[i];
  418. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  419. if(buf->base[i]==NULL) return -1;
  420. memset(buf->base[i], 128, size[i]);
  421. // no edge if EDGE EMU or not planar YUV
  422. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  423. buf->data[i] = buf->base[i];
  424. else
  425. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  426. }
  427. for (; i < AV_NUM_DATA_POINTERS; i++) {
  428. buf->base[i] = buf->data[i] = NULL;
  429. buf->linesize[i] = 0;
  430. }
  431. if(size[1] && !size[2])
  432. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  433. buf->width = s->width;
  434. buf->height = s->height;
  435. buf->pix_fmt= s->pix_fmt;
  436. }
  437. pic->type= FF_BUFFER_TYPE_INTERNAL;
  438. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  439. pic->base[i]= buf->base[i];
  440. pic->data[i]= buf->data[i];
  441. pic->linesize[i]= buf->linesize[i];
  442. }
  443. pic->extended_data = pic->data;
  444. avci->buffer_count++;
  445. pic->width = buf->width;
  446. pic->height = buf->height;
  447. pic->format = buf->pix_fmt;
  448. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  449. if (s->pkt) {
  450. pic->pkt_pts = s->pkt->pts;
  451. pic->pkt_pos = s->pkt->pos;
  452. } else {
  453. pic->pkt_pts = AV_NOPTS_VALUE;
  454. pic->pkt_pos = -1;
  455. }
  456. pic->reordered_opaque= s->reordered_opaque;
  457. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  458. pic->width = s->width;
  459. pic->height = s->height;
  460. pic->format = s->pix_fmt;
  461. if(s->debug&FF_DEBUG_BUFFERS)
  462. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  463. "buffers used\n", pic, avci->buffer_count);
  464. return 0;
  465. }
  466. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  467. {
  468. switch (avctx->codec_type) {
  469. case AVMEDIA_TYPE_VIDEO:
  470. return video_get_buffer(avctx, frame);
  471. case AVMEDIA_TYPE_AUDIO:
  472. return audio_get_buffer(avctx, frame);
  473. default:
  474. return -1;
  475. }
  476. }
  477. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  478. int i;
  479. InternalBuffer *buf, *last;
  480. AVCodecInternal *avci = s->internal;
  481. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  482. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  483. assert(avci->buffer_count);
  484. if (avci->buffer) {
  485. buf = NULL; /* avoids warning */
  486. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  487. buf = &avci->buffer[i];
  488. if (buf->data[0] == pic->data[0])
  489. break;
  490. }
  491. assert(i < avci->buffer_count);
  492. avci->buffer_count--;
  493. last = &avci->buffer[avci->buffer_count];
  494. if (buf != last)
  495. FFSWAP(InternalBuffer, *buf, *last);
  496. }
  497. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  498. pic->data[i]=NULL;
  499. // pic->base[i]=NULL;
  500. }
  501. //printf("R%X\n", pic->opaque);
  502. if(s->debug&FF_DEBUG_BUFFERS)
  503. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  504. "buffers used\n", pic, avci->buffer_count);
  505. }
  506. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  507. AVFrame temp_pic;
  508. int i;
  509. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  510. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  511. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  512. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  513. s->release_buffer(s, pic);
  514. }
  515. ff_init_buffer_info(s, pic);
  516. /* If no picture return a new buffer */
  517. if(pic->data[0] == NULL) {
  518. /* We will copy from buffer, so must be readable */
  519. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  520. return s->get_buffer(s, pic);
  521. }
  522. assert(s->pix_fmt == pic->format);
  523. /* If internal buffer type return the same buffer */
  524. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  525. return 0;
  526. }
  527. /*
  528. * Not internal type and reget_buffer not overridden, emulate cr buffer
  529. */
  530. temp_pic = *pic;
  531. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  532. pic->data[i] = pic->base[i] = NULL;
  533. pic->opaque = NULL;
  534. /* Allocate new frame */
  535. if (s->get_buffer(s, pic))
  536. return -1;
  537. /* Copy image data from old buffer to new buffer */
  538. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  539. s->height);
  540. s->release_buffer(s, &temp_pic); // Release old frame
  541. return 0;
  542. }
  543. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  544. int i;
  545. for(i=0; i<count; i++){
  546. int r= func(c, (char*)arg + i*size);
  547. if(ret) ret[i]= r;
  548. }
  549. return 0;
  550. }
  551. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  552. int i;
  553. for(i=0; i<count; i++){
  554. int r= func(c, arg, i, 0);
  555. if(ret) ret[i]= r;
  556. }
  557. return 0;
  558. }
  559. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  560. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  561. ++fmt;
  562. return fmt[0];
  563. }
  564. void avcodec_get_frame_defaults(AVFrame *pic){
  565. memset(pic, 0, sizeof(AVFrame));
  566. pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  567. pic->pkt_pos = -1;
  568. pic->key_frame= 1;
  569. pic->sample_aspect_ratio = (AVRational){0, 1};
  570. pic->format = -1; /* unknown */
  571. }
  572. AVFrame *avcodec_alloc_frame(void){
  573. AVFrame *pic= av_malloc(sizeof(AVFrame));
  574. if(pic==NULL) return NULL;
  575. avcodec_get_frame_defaults(pic);
  576. return pic;
  577. }
  578. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  579. {
  580. memset(sub, 0, sizeof(*sub));
  581. sub->pts = AV_NOPTS_VALUE;
  582. }
  583. static int get_bit_rate(AVCodecContext *ctx)
  584. {
  585. int bit_rate;
  586. int bits_per_sample;
  587. switch(ctx->codec_type) {
  588. case AVMEDIA_TYPE_VIDEO:
  589. case AVMEDIA_TYPE_DATA:
  590. case AVMEDIA_TYPE_SUBTITLE:
  591. case AVMEDIA_TYPE_ATTACHMENT:
  592. bit_rate = ctx->bit_rate;
  593. break;
  594. case AVMEDIA_TYPE_AUDIO:
  595. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  596. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  597. break;
  598. default:
  599. bit_rate = 0;
  600. break;
  601. }
  602. return bit_rate;
  603. }
  604. #if FF_API_AVCODEC_OPEN
  605. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  606. {
  607. return avcodec_open2(avctx, codec, NULL);
  608. }
  609. #endif
  610. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  611. {
  612. int ret = 0;
  613. AVDictionary *tmp = NULL;
  614. if (avcodec_is_open(avctx))
  615. return 0;
  616. if ((!codec && !avctx->codec)) {
  617. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  618. return AVERROR(EINVAL);
  619. }
  620. if ((codec && avctx->codec && codec != avctx->codec)) {
  621. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  622. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  623. return AVERROR(EINVAL);
  624. }
  625. if (!codec)
  626. codec = avctx->codec;
  627. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  628. return AVERROR(EINVAL);
  629. if (options)
  630. av_dict_copy(&tmp, *options, 0);
  631. /* If there is a user-supplied mutex locking routine, call it. */
  632. if (ff_lockmgr_cb) {
  633. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  634. return -1;
  635. }
  636. entangled_thread_counter++;
  637. if(entangled_thread_counter != 1){
  638. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  639. ret = -1;
  640. goto end;
  641. }
  642. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  643. if (!avctx->internal) {
  644. ret = AVERROR(ENOMEM);
  645. goto end;
  646. }
  647. if (codec->priv_data_size > 0) {
  648. if(!avctx->priv_data){
  649. avctx->priv_data = av_mallocz(codec->priv_data_size);
  650. if (!avctx->priv_data) {
  651. ret = AVERROR(ENOMEM);
  652. goto end;
  653. }
  654. if (codec->priv_class) {
  655. *(const AVClass**)avctx->priv_data= codec->priv_class;
  656. av_opt_set_defaults(avctx->priv_data);
  657. }
  658. }
  659. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  660. goto free_and_end;
  661. } else {
  662. avctx->priv_data = NULL;
  663. }
  664. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  665. goto free_and_end;
  666. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  667. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  668. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
  669. ret = -1;
  670. goto free_and_end;
  671. }
  672. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  673. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  674. if(avctx->coded_width && avctx->coded_height)
  675. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  676. else if(avctx->width && avctx->height)
  677. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  678. }
  679. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  680. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  681. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  682. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  683. avcodec_set_dimensions(avctx, 0, 0);
  684. }
  685. /* if the decoder init function was already called previously,
  686. free the already allocated subtitle_header before overwriting it */
  687. if (av_codec_is_decoder(codec))
  688. av_freep(&avctx->subtitle_header);
  689. #define SANE_NB_CHANNELS 128U
  690. if (avctx->channels > SANE_NB_CHANNELS) {
  691. ret = AVERROR(EINVAL);
  692. goto free_and_end;
  693. }
  694. avctx->codec = codec;
  695. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  696. avctx->codec_id == CODEC_ID_NONE) {
  697. avctx->codec_type = codec->type;
  698. avctx->codec_id = codec->id;
  699. }
  700. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  701. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  702. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  703. ret = AVERROR(EINVAL);
  704. goto free_and_end;
  705. }
  706. avctx->frame_number = 0;
  707. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  708. (!avctx->time_base.num || !avctx->time_base.den)) {
  709. avctx->time_base.num = 1;
  710. avctx->time_base.den = avctx->sample_rate;
  711. }
  712. if (!HAVE_THREADS)
  713. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  714. if (HAVE_THREADS && !avctx->thread_opaque) {
  715. ret = ff_thread_init(avctx);
  716. if (ret < 0) {
  717. goto free_and_end;
  718. }
  719. }
  720. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  721. avctx->thread_count = 1;
  722. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  723. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  724. avctx->codec->max_lowres);
  725. ret = AVERROR(EINVAL);
  726. goto free_and_end;
  727. }
  728. if (av_codec_is_encoder(avctx->codec)) {
  729. int i;
  730. if (avctx->codec->sample_fmts) {
  731. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  732. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  733. break;
  734. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  735. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  736. ret = AVERROR(EINVAL);
  737. goto free_and_end;
  738. }
  739. }
  740. if (avctx->codec->pix_fmts) {
  741. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  742. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  743. break;
  744. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
  745. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  746. ret = AVERROR(EINVAL);
  747. goto free_and_end;
  748. }
  749. }
  750. if (avctx->codec->supported_samplerates) {
  751. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  752. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  753. break;
  754. if (avctx->codec->supported_samplerates[i] == 0) {
  755. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  756. ret = AVERROR(EINVAL);
  757. goto free_and_end;
  758. }
  759. }
  760. if (avctx->codec->channel_layouts) {
  761. if (!avctx->channel_layout) {
  762. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  763. } else {
  764. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  765. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  766. break;
  767. if (avctx->codec->channel_layouts[i] == 0) {
  768. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  769. ret = AVERROR(EINVAL);
  770. goto free_and_end;
  771. }
  772. }
  773. }
  774. if (avctx->channel_layout && avctx->channels) {
  775. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  776. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  777. ret = AVERROR(EINVAL);
  778. goto free_and_end;
  779. }
  780. } else if (avctx->channel_layout) {
  781. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  782. }
  783. }
  784. avctx->pts_correction_num_faulty_pts =
  785. avctx->pts_correction_num_faulty_dts = 0;
  786. avctx->pts_correction_last_pts =
  787. avctx->pts_correction_last_dts = INT64_MIN;
  788. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  789. ret = avctx->codec->init(avctx);
  790. if (ret < 0) {
  791. goto free_and_end;
  792. }
  793. }
  794. ret=0;
  795. if (av_codec_is_decoder(avctx->codec)) {
  796. if (!avctx->bit_rate)
  797. avctx->bit_rate = get_bit_rate(avctx);
  798. /* validate channel layout from the decoder */
  799. if (avctx->channel_layout &&
  800. av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  801. av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
  802. avctx->channel_layout = 0;
  803. }
  804. }
  805. end:
  806. entangled_thread_counter--;
  807. /* Release any user-supplied mutex. */
  808. if (ff_lockmgr_cb) {
  809. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  810. }
  811. if (options) {
  812. av_dict_free(options);
  813. *options = tmp;
  814. }
  815. return ret;
  816. free_and_end:
  817. av_dict_free(&tmp);
  818. av_freep(&avctx->priv_data);
  819. av_freep(&avctx->internal);
  820. avctx->codec= NULL;
  821. goto end;
  822. }
  823. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  824. {
  825. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  826. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  827. return AVERROR(EINVAL);
  828. }
  829. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  830. if (!avpkt->data || avpkt->size < size) {
  831. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  832. avpkt->data = avctx->internal->byte_buffer;
  833. avpkt->size = avctx->internal->byte_buffer_size;
  834. avpkt->destruct = NULL;
  835. }
  836. if (avpkt->data) {
  837. void *destruct = avpkt->destruct;
  838. if (avpkt->size < size) {
  839. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  840. return AVERROR(EINVAL);
  841. }
  842. av_init_packet(avpkt);
  843. avpkt->destruct = destruct;
  844. avpkt->size = size;
  845. return 0;
  846. } else {
  847. int ret = av_new_packet(avpkt, size);
  848. if (ret < 0)
  849. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  850. return ret;
  851. }
  852. }
  853. int ff_alloc_packet(AVPacket *avpkt, int size)
  854. {
  855. return ff_alloc_packet2(NULL, avpkt, size);
  856. }
  857. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  858. AVPacket *avpkt,
  859. const AVFrame *frame,
  860. int *got_packet_ptr)
  861. {
  862. int ret;
  863. AVPacket user_pkt = *avpkt;
  864. int nb_samples;
  865. int needs_realloc = !user_pkt.data;
  866. *got_packet_ptr = 0;
  867. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  868. av_free_packet(avpkt);
  869. av_init_packet(avpkt);
  870. avpkt->size = 0;
  871. return 0;
  872. }
  873. /* check for valid frame size */
  874. if (frame) {
  875. nb_samples = frame->nb_samples;
  876. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  877. if (nb_samples > avctx->frame_size)
  878. return AVERROR(EINVAL);
  879. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  880. if (nb_samples != avctx->frame_size)
  881. return AVERROR(EINVAL);
  882. }
  883. } else {
  884. nb_samples = avctx->frame_size;
  885. }
  886. if (avctx->codec->encode2) {
  887. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  888. if (!ret && *got_packet_ptr) {
  889. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  890. if (avpkt->pts == AV_NOPTS_VALUE)
  891. avpkt->pts = frame->pts;
  892. if (!avpkt->duration)
  893. avpkt->duration = ff_samples_to_time_base(avctx,
  894. frame->nb_samples);
  895. }
  896. avpkt->dts = avpkt->pts;
  897. } else {
  898. avpkt->size = 0;
  899. }
  900. } else {
  901. /* for compatibility with encoders not supporting encode2(), we need to
  902. allocate a packet buffer if the user has not provided one or check
  903. the size otherwise */
  904. int fs_tmp = 0;
  905. int buf_size = avpkt->size;
  906. if (!user_pkt.data) {
  907. if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
  908. av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
  909. if (!frame)
  910. return AVERROR(EINVAL);
  911. buf_size = nb_samples * avctx->channels *
  912. av_get_bits_per_sample(avctx->codec_id) / 8;
  913. } else {
  914. /* this is a guess as to the required size.
  915. if an encoder needs more than this, it should probably
  916. implement encode2() */
  917. buf_size = 2 * avctx->frame_size * avctx->channels *
  918. av_get_bytes_per_sample(avctx->sample_fmt);
  919. buf_size += 2*FF_MIN_BUFFER_SIZE;
  920. }
  921. }
  922. if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size)))
  923. return ret;
  924. /* Encoders using AVCodec.encode() that support
  925. CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
  926. the smaller size when encoding the last frame.
  927. This code can be removed once all encoders supporting
  928. CODEC_CAP_SMALL_LAST_FRAME use encode2() */
  929. if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
  930. nb_samples < avctx->frame_size) {
  931. fs_tmp = avctx->frame_size;
  932. avctx->frame_size = nb_samples;
  933. }
  934. /* encode the frame */
  935. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
  936. frame ? frame->data[0] : NULL);
  937. if (ret >= 0) {
  938. if (!ret) {
  939. /* no output. if the packet data was allocated by libavcodec,
  940. free it */
  941. if (!user_pkt.data && avpkt->data != avctx->internal->byte_buffer)
  942. av_freep(&avpkt->data);
  943. } else {
  944. if (avctx->coded_frame)
  945. avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
  946. /* Set duration for final small packet. This can be removed
  947. once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
  948. encode2() */
  949. if (fs_tmp) {
  950. avpkt->duration = ff_samples_to_time_base(avctx,
  951. avctx->frame_size);
  952. }
  953. }
  954. avpkt->size = ret;
  955. *got_packet_ptr = (ret > 0);
  956. ret = 0;
  957. }
  958. if (fs_tmp)
  959. avctx->frame_size = fs_tmp;
  960. }
  961. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  962. needs_realloc = 0;
  963. if (user_pkt.data) {
  964. if (user_pkt.size >= avpkt->size) {
  965. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  966. } else {
  967. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  968. avpkt->size = user_pkt.size;
  969. ret = -1;
  970. }
  971. avpkt->data = user_pkt.data;
  972. avpkt->destruct = user_pkt.destruct;
  973. } else {
  974. if (av_dup_packet(avpkt) < 0) {
  975. ret = AVERROR(ENOMEM);
  976. }
  977. }
  978. }
  979. if (!ret) {
  980. if (needs_realloc && avpkt->data) {
  981. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  982. if (new_data)
  983. avpkt->data = new_data;
  984. }
  985. avctx->frame_number++;
  986. }
  987. if (ret < 0 || !*got_packet_ptr)
  988. av_free_packet(avpkt);
  989. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  990. this needs to be moved to the encoders, but for now we can do it
  991. here to simplify things */
  992. avpkt->flags |= AV_PKT_FLAG_KEY;
  993. return ret;
  994. }
  995. #if FF_API_OLD_DECODE_AUDIO
  996. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  997. uint8_t *buf, int buf_size,
  998. const short *samples)
  999. {
  1000. AVPacket pkt;
  1001. AVFrame frame0;
  1002. AVFrame *frame;
  1003. int ret, samples_size, got_packet;
  1004. av_init_packet(&pkt);
  1005. pkt.data = buf;
  1006. pkt.size = buf_size;
  1007. if (samples) {
  1008. frame = &frame0;
  1009. avcodec_get_frame_defaults(frame);
  1010. if (avctx->frame_size) {
  1011. frame->nb_samples = avctx->frame_size;
  1012. } else {
  1013. /* if frame_size is not set, the number of samples must be
  1014. calculated from the buffer size */
  1015. int64_t nb_samples;
  1016. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1017. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1018. "support this codec\n");
  1019. return AVERROR(EINVAL);
  1020. }
  1021. nb_samples = (int64_t)buf_size * 8 /
  1022. (av_get_bits_per_sample(avctx->codec_id) *
  1023. avctx->channels);
  1024. if (nb_samples >= INT_MAX)
  1025. return AVERROR(EINVAL);
  1026. frame->nb_samples = nb_samples;
  1027. }
  1028. /* it is assumed that the samples buffer is large enough based on the
  1029. relevant parameters */
  1030. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1031. frame->nb_samples,
  1032. avctx->sample_fmt, 1);
  1033. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1034. avctx->sample_fmt,
  1035. (const uint8_t *)samples, samples_size, 1)))
  1036. return ret;
  1037. /* fabricate frame pts from sample count.
  1038. this is needed because the avcodec_encode_audio() API does not have
  1039. a way for the user to provide pts */
  1040. if(avctx->sample_rate && avctx->time_base.num)
  1041. frame->pts = ff_samples_to_time_base(avctx,
  1042. avctx->internal->sample_count);
  1043. else
  1044. frame->pts = AV_NOPTS_VALUE;
  1045. avctx->internal->sample_count += frame->nb_samples;
  1046. } else {
  1047. frame = NULL;
  1048. }
  1049. got_packet = 0;
  1050. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1051. if (!ret && got_packet && avctx->coded_frame) {
  1052. avctx->coded_frame->pts = pkt.pts;
  1053. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1054. }
  1055. /* free any side data since we cannot return it */
  1056. ff_packet_free_side_data(&pkt);
  1057. if (frame && frame->extended_data != frame->data)
  1058. av_freep(&frame->extended_data);
  1059. return ret ? ret : pkt.size;
  1060. }
  1061. #endif
  1062. #if FF_API_OLD_ENCODE_VIDEO
  1063. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1064. const AVFrame *pict)
  1065. {
  1066. AVPacket pkt;
  1067. int ret, got_packet = 0;
  1068. if(buf_size < FF_MIN_BUFFER_SIZE){
  1069. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1070. return -1;
  1071. }
  1072. av_init_packet(&pkt);
  1073. pkt.data = buf;
  1074. pkt.size = buf_size;
  1075. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1076. if (!ret && got_packet && avctx->coded_frame) {
  1077. avctx->coded_frame->pts = pkt.pts;
  1078. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1079. }
  1080. /* free any side data since we cannot return it */
  1081. if (pkt.side_data_elems > 0) {
  1082. int i;
  1083. for (i = 0; i < pkt.side_data_elems; i++)
  1084. av_free(pkt.side_data[i].data);
  1085. av_freep(&pkt.side_data);
  1086. pkt.side_data_elems = 0;
  1087. }
  1088. return ret ? ret : pkt.size;
  1089. }
  1090. #endif
  1091. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1092. AVPacket *avpkt,
  1093. const AVFrame *frame,
  1094. int *got_packet_ptr)
  1095. {
  1096. int ret;
  1097. AVPacket user_pkt = *avpkt;
  1098. int needs_realloc = !user_pkt.data;
  1099. *got_packet_ptr = 0;
  1100. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1101. av_free_packet(avpkt);
  1102. av_init_packet(avpkt);
  1103. avpkt->size = 0;
  1104. return 0;
  1105. }
  1106. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1107. return AVERROR(EINVAL);
  1108. av_assert0(avctx->codec->encode2);
  1109. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1110. av_assert0(ret <= 0);
  1111. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1112. needs_realloc = 0;
  1113. if (user_pkt.data) {
  1114. if (user_pkt.size >= avpkt->size) {
  1115. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1116. } else {
  1117. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1118. avpkt->size = user_pkt.size;
  1119. ret = -1;
  1120. }
  1121. avpkt->data = user_pkt.data;
  1122. avpkt->destruct = user_pkt.destruct;
  1123. } else {
  1124. if (av_dup_packet(avpkt) < 0) {
  1125. ret = AVERROR(ENOMEM);
  1126. }
  1127. }
  1128. }
  1129. if (!ret) {
  1130. if (!*got_packet_ptr)
  1131. avpkt->size = 0;
  1132. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1133. avpkt->pts = avpkt->dts = frame->pts;
  1134. if (needs_realloc && avpkt->data &&
  1135. avpkt->destruct == av_destruct_packet) {
  1136. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1137. if (new_data)
  1138. avpkt->data = new_data;
  1139. }
  1140. avctx->frame_number++;
  1141. }
  1142. if (ret < 0 || !*got_packet_ptr)
  1143. av_free_packet(avpkt);
  1144. emms_c();
  1145. return ret;
  1146. }
  1147. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1148. const AVSubtitle *sub)
  1149. {
  1150. int ret;
  1151. if(sub->start_display_time) {
  1152. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1153. return -1;
  1154. }
  1155. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
  1156. avctx->frame_number++;
  1157. return ret;
  1158. }
  1159. /**
  1160. * Attempt to guess proper monotonic timestamps for decoded video frames
  1161. * which might have incorrect times. Input timestamps may wrap around, in
  1162. * which case the output will as well.
  1163. *
  1164. * @param pts the pts field of the decoded AVPacket, as passed through
  1165. * AVFrame.pkt_pts
  1166. * @param dts the dts field of the decoded AVPacket
  1167. * @return one of the input values, may be AV_NOPTS_VALUE
  1168. */
  1169. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1170. int64_t reordered_pts, int64_t dts)
  1171. {
  1172. int64_t pts = AV_NOPTS_VALUE;
  1173. if (dts != AV_NOPTS_VALUE) {
  1174. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1175. ctx->pts_correction_last_dts = dts;
  1176. }
  1177. if (reordered_pts != AV_NOPTS_VALUE) {
  1178. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1179. ctx->pts_correction_last_pts = reordered_pts;
  1180. }
  1181. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1182. && reordered_pts != AV_NOPTS_VALUE)
  1183. pts = reordered_pts;
  1184. else
  1185. pts = dts;
  1186. return pts;
  1187. }
  1188. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1189. {
  1190. int size = 0;
  1191. const uint8_t *data;
  1192. uint32_t flags;
  1193. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1194. return;
  1195. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1196. if (!data || size < 4)
  1197. return;
  1198. flags = bytestream_get_le32(&data);
  1199. size -= 4;
  1200. if (size < 4) /* Required for any of the changes */
  1201. return;
  1202. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1203. avctx->channels = bytestream_get_le32(&data);
  1204. size -= 4;
  1205. }
  1206. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1207. if (size < 8)
  1208. return;
  1209. avctx->channel_layout = bytestream_get_le64(&data);
  1210. size -= 8;
  1211. }
  1212. if (size < 4)
  1213. return;
  1214. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1215. avctx->sample_rate = bytestream_get_le32(&data);
  1216. size -= 4;
  1217. }
  1218. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1219. if (size < 8)
  1220. return;
  1221. avctx->width = bytestream_get_le32(&data);
  1222. avctx->height = bytestream_get_le32(&data);
  1223. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1224. size -= 8;
  1225. }
  1226. }
  1227. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1228. int *got_picture_ptr,
  1229. const AVPacket *avpkt)
  1230. {
  1231. int ret;
  1232. // copy to ensure we do not change avpkt
  1233. AVPacket tmp = *avpkt;
  1234. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1235. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1236. return AVERROR(EINVAL);
  1237. }
  1238. *got_picture_ptr= 0;
  1239. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1240. return -1;
  1241. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1242. int did_split = av_packet_split_side_data(&tmp);
  1243. apply_param_change(avctx, &tmp);
  1244. avctx->pkt = &tmp;
  1245. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1246. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1247. &tmp);
  1248. else {
  1249. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1250. &tmp);
  1251. picture->pkt_dts= avpkt->dts;
  1252. if(!avctx->has_b_frames){
  1253. picture->pkt_pos= avpkt->pos;
  1254. }
  1255. //FIXME these should be under if(!avctx->has_b_frames)
  1256. if (!picture->sample_aspect_ratio.num)
  1257. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1258. if (!picture->width)
  1259. picture->width = avctx->width;
  1260. if (!picture->height)
  1261. picture->height = avctx->height;
  1262. if (picture->format == PIX_FMT_NONE)
  1263. picture->format = avctx->pix_fmt;
  1264. }
  1265. emms_c(); //needed to avoid an emms_c() call before every return;
  1266. avctx->pkt = NULL;
  1267. if (did_split)
  1268. ff_packet_free_side_data(&tmp);
  1269. if (*got_picture_ptr){
  1270. avctx->frame_number++;
  1271. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1272. picture->pkt_pts,
  1273. picture->pkt_dts);
  1274. }
  1275. }else
  1276. ret= 0;
  1277. return ret;
  1278. }
  1279. #if FF_API_OLD_DECODE_AUDIO
  1280. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1281. int *frame_size_ptr,
  1282. AVPacket *avpkt)
  1283. {
  1284. AVFrame frame;
  1285. int ret, got_frame = 0;
  1286. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1287. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1288. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1289. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1290. "avcodec_decode_audio4()\n");
  1291. avctx->get_buffer = avcodec_default_get_buffer;
  1292. avctx->release_buffer = avcodec_default_release_buffer;
  1293. }
  1294. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1295. if (ret >= 0 && got_frame) {
  1296. int ch, plane_size;
  1297. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1298. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1299. frame.nb_samples,
  1300. avctx->sample_fmt, 1);
  1301. if (*frame_size_ptr < data_size) {
  1302. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1303. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1304. return AVERROR(EINVAL);
  1305. }
  1306. memcpy(samples, frame.extended_data[0], plane_size);
  1307. if (planar && avctx->channels > 1) {
  1308. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1309. for (ch = 1; ch < avctx->channels; ch++) {
  1310. memcpy(out, frame.extended_data[ch], plane_size);
  1311. out += plane_size;
  1312. }
  1313. }
  1314. *frame_size_ptr = data_size;
  1315. } else {
  1316. *frame_size_ptr = 0;
  1317. }
  1318. return ret;
  1319. }
  1320. #endif
  1321. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1322. AVFrame *frame,
  1323. int *got_frame_ptr,
  1324. const AVPacket *avpkt)
  1325. {
  1326. int ret = 0;
  1327. *got_frame_ptr = 0;
  1328. if (!avpkt->data && avpkt->size) {
  1329. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1330. return AVERROR(EINVAL);
  1331. }
  1332. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1333. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1334. return AVERROR(EINVAL);
  1335. }
  1336. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1337. // copy to ensure we do not change avpkt
  1338. AVPacket tmp = *avpkt;
  1339. int did_split = av_packet_split_side_data(&tmp);
  1340. apply_param_change(avctx, &tmp);
  1341. avctx->pkt = &tmp;
  1342. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1343. if (ret >= 0 && *got_frame_ptr) {
  1344. avctx->frame_number++;
  1345. frame->pkt_dts = avpkt->dts;
  1346. if (frame->format == AV_SAMPLE_FMT_NONE)
  1347. frame->format = avctx->sample_fmt;
  1348. if (!frame->channel_layout)
  1349. frame->channel_layout = avctx->channel_layout;
  1350. }
  1351. avctx->pkt = NULL;
  1352. if (did_split)
  1353. ff_packet_free_side_data(&tmp);
  1354. }
  1355. return ret;
  1356. }
  1357. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1358. int *got_sub_ptr,
  1359. AVPacket *avpkt)
  1360. {
  1361. int ret;
  1362. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1363. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1364. return AVERROR(EINVAL);
  1365. }
  1366. avctx->pkt = avpkt;
  1367. *got_sub_ptr = 0;
  1368. avcodec_get_subtitle_defaults(sub);
  1369. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1370. if (*got_sub_ptr)
  1371. avctx->frame_number++;
  1372. return ret;
  1373. }
  1374. void avsubtitle_free(AVSubtitle *sub)
  1375. {
  1376. int i;
  1377. for (i = 0; i < sub->num_rects; i++)
  1378. {
  1379. av_freep(&sub->rects[i]->pict.data[0]);
  1380. av_freep(&sub->rects[i]->pict.data[1]);
  1381. av_freep(&sub->rects[i]->pict.data[2]);
  1382. av_freep(&sub->rects[i]->pict.data[3]);
  1383. av_freep(&sub->rects[i]->text);
  1384. av_freep(&sub->rects[i]->ass);
  1385. av_freep(&sub->rects[i]);
  1386. }
  1387. av_freep(&sub->rects);
  1388. memset(sub, 0, sizeof(AVSubtitle));
  1389. }
  1390. av_cold int avcodec_close(AVCodecContext *avctx)
  1391. {
  1392. /* If there is a user-supplied mutex locking routine, call it. */
  1393. if (ff_lockmgr_cb) {
  1394. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1395. return -1;
  1396. }
  1397. entangled_thread_counter++;
  1398. if(entangled_thread_counter != 1){
  1399. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1400. entangled_thread_counter--;
  1401. return -1;
  1402. }
  1403. if (avcodec_is_open(avctx)) {
  1404. if (HAVE_THREADS && avctx->thread_opaque)
  1405. ff_thread_free(avctx);
  1406. if (avctx->codec && avctx->codec->close)
  1407. avctx->codec->close(avctx);
  1408. avcodec_default_free_buffers(avctx);
  1409. avctx->coded_frame = NULL;
  1410. avctx->internal->byte_buffer_size = 0;
  1411. av_freep(&avctx->internal->byte_buffer);
  1412. av_freep(&avctx->internal);
  1413. }
  1414. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1415. av_opt_free(avctx->priv_data);
  1416. av_opt_free(avctx);
  1417. av_freep(&avctx->priv_data);
  1418. if (av_codec_is_encoder(avctx->codec))
  1419. av_freep(&avctx->extradata);
  1420. avctx->codec = NULL;
  1421. avctx->active_thread_type = 0;
  1422. entangled_thread_counter--;
  1423. /* Release any user-supplied mutex. */
  1424. if (ff_lockmgr_cb) {
  1425. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1426. }
  1427. return 0;
  1428. }
  1429. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1430. {
  1431. switch(id){
  1432. //This is for future deprecatec codec ids, its empty since
  1433. //last major bump but will fill up again over time, please dont remove it
  1434. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1435. default : return id;
  1436. }
  1437. }
  1438. AVCodec *avcodec_find_encoder(enum CodecID id)
  1439. {
  1440. AVCodec *p, *experimental=NULL;
  1441. p = first_avcodec;
  1442. id= remap_deprecated_codec_id(id);
  1443. while (p) {
  1444. if (av_codec_is_encoder(p) && p->id == id) {
  1445. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1446. experimental = p;
  1447. } else
  1448. return p;
  1449. }
  1450. p = p->next;
  1451. }
  1452. return experimental;
  1453. }
  1454. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1455. {
  1456. AVCodec *p;
  1457. if (!name)
  1458. return NULL;
  1459. p = first_avcodec;
  1460. while (p) {
  1461. if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1462. return p;
  1463. p = p->next;
  1464. }
  1465. return NULL;
  1466. }
  1467. AVCodec *avcodec_find_decoder(enum CodecID id)
  1468. {
  1469. AVCodec *p, *experimental=NULL;
  1470. p = first_avcodec;
  1471. id= remap_deprecated_codec_id(id);
  1472. while (p) {
  1473. if (av_codec_is_decoder(p) && p->id == id) {
  1474. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1475. experimental = p;
  1476. } else
  1477. return p;
  1478. }
  1479. p = p->next;
  1480. }
  1481. return experimental;
  1482. }
  1483. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1484. {
  1485. AVCodec *p;
  1486. if (!name)
  1487. return NULL;
  1488. p = first_avcodec;
  1489. while (p) {
  1490. if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1491. return p;
  1492. p = p->next;
  1493. }
  1494. return NULL;
  1495. }
  1496. const char *avcodec_get_name(enum CodecID id)
  1497. {
  1498. AVCodec *codec;
  1499. #if !CONFIG_SMALL
  1500. switch (id) {
  1501. #include "libavcodec/codec_names.h"
  1502. }
  1503. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1504. #endif
  1505. codec = avcodec_find_decoder(id);
  1506. if (codec)
  1507. return codec->name;
  1508. codec = avcodec_find_encoder(id);
  1509. if (codec)
  1510. return codec->name;
  1511. return "unknown_codec";
  1512. }
  1513. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1514. {
  1515. int i, len, ret = 0;
  1516. for (i = 0; i < 4; i++) {
  1517. len = snprintf(buf, buf_size,
  1518. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1519. buf += len;
  1520. buf_size = buf_size > len ? buf_size - len : 0;
  1521. ret += len;
  1522. codec_tag>>=8;
  1523. }
  1524. return ret;
  1525. }
  1526. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1527. {
  1528. const char *codec_type;
  1529. const char *codec_name;
  1530. const char *profile = NULL;
  1531. AVCodec *p;
  1532. int bitrate;
  1533. AVRational display_aspect_ratio;
  1534. if (!buf || buf_size <= 0)
  1535. return;
  1536. codec_type = av_get_media_type_string(enc->codec_type);
  1537. codec_name = avcodec_get_name(enc->codec_id);
  1538. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1539. if (enc->codec)
  1540. p = enc->codec;
  1541. else
  1542. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1543. avcodec_find_decoder(enc->codec_id);
  1544. if (p)
  1545. profile = av_get_profile_name(p, enc->profile);
  1546. }
  1547. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1548. codec_name, enc->mb_decision ? " (hq)" : "");
  1549. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1550. if (profile)
  1551. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1552. if (enc->codec_tag) {
  1553. char tag_buf[32];
  1554. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1555. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1556. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1557. }
  1558. switch(enc->codec_type) {
  1559. case AVMEDIA_TYPE_VIDEO:
  1560. if (enc->pix_fmt != PIX_FMT_NONE) {
  1561. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1562. ", %s",
  1563. av_get_pix_fmt_name(enc->pix_fmt));
  1564. }
  1565. if (enc->width) {
  1566. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1567. ", %dx%d",
  1568. enc->width, enc->height);
  1569. if (enc->sample_aspect_ratio.num) {
  1570. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1571. enc->width*enc->sample_aspect_ratio.num,
  1572. enc->height*enc->sample_aspect_ratio.den,
  1573. 1024*1024);
  1574. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1575. " [SAR %d:%d DAR %d:%d]",
  1576. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1577. display_aspect_ratio.num, display_aspect_ratio.den);
  1578. }
  1579. if(av_log_get_level() >= AV_LOG_DEBUG){
  1580. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1581. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1582. ", %d/%d",
  1583. enc->time_base.num/g, enc->time_base.den/g);
  1584. }
  1585. }
  1586. if (encode) {
  1587. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1588. ", q=%d-%d", enc->qmin, enc->qmax);
  1589. }
  1590. break;
  1591. case AVMEDIA_TYPE_AUDIO:
  1592. if (enc->sample_rate) {
  1593. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1594. ", %d Hz", enc->sample_rate);
  1595. }
  1596. av_strlcat(buf, ", ", buf_size);
  1597. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1598. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1599. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1600. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1601. }
  1602. break;
  1603. default:
  1604. return;
  1605. }
  1606. if (encode) {
  1607. if (enc->flags & CODEC_FLAG_PASS1)
  1608. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1609. ", pass 1");
  1610. if (enc->flags & CODEC_FLAG_PASS2)
  1611. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1612. ", pass 2");
  1613. }
  1614. bitrate = get_bit_rate(enc);
  1615. if (bitrate != 0) {
  1616. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1617. ", %d kb/s", bitrate / 1000);
  1618. }
  1619. }
  1620. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1621. {
  1622. const AVProfile *p;
  1623. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1624. return NULL;
  1625. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1626. if (p->profile == profile)
  1627. return p->name;
  1628. return NULL;
  1629. }
  1630. unsigned avcodec_version( void )
  1631. {
  1632. // av_assert0(CODEC_ID_V410==164);
  1633. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1634. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1635. // av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1636. av_assert0(CODEC_ID_SRT==94216);
  1637. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1638. return LIBAVCODEC_VERSION_INT;
  1639. }
  1640. const char *avcodec_configuration(void)
  1641. {
  1642. return FFMPEG_CONFIGURATION;
  1643. }
  1644. const char *avcodec_license(void)
  1645. {
  1646. #define LICENSE_PREFIX "libavcodec license: "
  1647. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1648. }
  1649. void avcodec_flush_buffers(AVCodecContext *avctx)
  1650. {
  1651. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1652. ff_thread_flush(avctx);
  1653. else if(avctx->codec->flush)
  1654. avctx->codec->flush(avctx);
  1655. avctx->pts_correction_last_pts =
  1656. avctx->pts_correction_last_dts = INT64_MIN;
  1657. }
  1658. static void video_free_buffers(AVCodecContext *s)
  1659. {
  1660. AVCodecInternal *avci = s->internal;
  1661. int i, j;
  1662. if (!avci->buffer)
  1663. return;
  1664. if (avci->buffer_count)
  1665. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1666. avci->buffer_count);
  1667. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1668. InternalBuffer *buf = &avci->buffer[i];
  1669. for(j=0; j<4; j++){
  1670. av_freep(&buf->base[j]);
  1671. buf->data[j]= NULL;
  1672. }
  1673. }
  1674. av_freep(&avci->buffer);
  1675. avci->buffer_count=0;
  1676. }
  1677. static void audio_free_buffers(AVCodecContext *avctx)
  1678. {
  1679. AVCodecInternal *avci = avctx->internal;
  1680. InternalBuffer *buf;
  1681. if (!avci->buffer)
  1682. return;
  1683. buf = avci->buffer;
  1684. if (buf->extended_data) {
  1685. av_free(buf->extended_data[0]);
  1686. if (buf->extended_data != buf->data)
  1687. av_freep(&buf->extended_data);
  1688. }
  1689. av_freep(&avci->buffer);
  1690. }
  1691. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1692. {
  1693. switch (avctx->codec_type) {
  1694. case AVMEDIA_TYPE_VIDEO:
  1695. video_free_buffers(avctx);
  1696. break;
  1697. case AVMEDIA_TYPE_AUDIO:
  1698. audio_free_buffers(avctx);
  1699. break;
  1700. default:
  1701. break;
  1702. }
  1703. }
  1704. int av_get_exact_bits_per_sample(enum CodecID codec_id)
  1705. {
  1706. switch(codec_id){
  1707. case CODEC_ID_ADPCM_CT:
  1708. case CODEC_ID_ADPCM_IMA_APC:
  1709. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1710. case CODEC_ID_ADPCM_IMA_WS:
  1711. case CODEC_ID_ADPCM_G722:
  1712. case CODEC_ID_ADPCM_YAMAHA:
  1713. return 4;
  1714. case CODEC_ID_PCM_ALAW:
  1715. case CODEC_ID_PCM_MULAW:
  1716. case CODEC_ID_PCM_S8:
  1717. case CODEC_ID_PCM_U8:
  1718. case CODEC_ID_PCM_ZORK:
  1719. return 8;
  1720. case CODEC_ID_PCM_S16BE:
  1721. case CODEC_ID_PCM_S16LE:
  1722. case CODEC_ID_PCM_S16LE_PLANAR:
  1723. case CODEC_ID_PCM_U16BE:
  1724. case CODEC_ID_PCM_U16LE:
  1725. return 16;
  1726. case CODEC_ID_PCM_S24DAUD:
  1727. case CODEC_ID_PCM_S24BE:
  1728. case CODEC_ID_PCM_S24LE:
  1729. case CODEC_ID_PCM_U24BE:
  1730. case CODEC_ID_PCM_U24LE:
  1731. return 24;
  1732. case CODEC_ID_PCM_S32BE:
  1733. case CODEC_ID_PCM_S32LE:
  1734. case CODEC_ID_PCM_U32BE:
  1735. case CODEC_ID_PCM_U32LE:
  1736. case CODEC_ID_PCM_F32BE:
  1737. case CODEC_ID_PCM_F32LE:
  1738. return 32;
  1739. case CODEC_ID_PCM_F64BE:
  1740. case CODEC_ID_PCM_F64LE:
  1741. return 64;
  1742. default:
  1743. return 0;
  1744. }
  1745. }
  1746. enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1747. {
  1748. static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
  1749. [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1750. [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1751. [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1752. [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1753. [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1754. [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1755. [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1756. [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1757. [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1758. [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1759. };
  1760. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1761. return CODEC_ID_NONE;
  1762. if (be < 0 || be > 1)
  1763. be = AV_NE(1, 0);
  1764. return map[fmt][be];
  1765. }
  1766. int av_get_bits_per_sample(enum CodecID codec_id)
  1767. {
  1768. switch (codec_id) {
  1769. case CODEC_ID_ADPCM_SBPRO_2:
  1770. return 2;
  1771. case CODEC_ID_ADPCM_SBPRO_3:
  1772. return 3;
  1773. case CODEC_ID_ADPCM_SBPRO_4:
  1774. case CODEC_ID_ADPCM_IMA_WAV:
  1775. case CODEC_ID_ADPCM_IMA_QT:
  1776. case CODEC_ID_ADPCM_SWF:
  1777. case CODEC_ID_ADPCM_MS:
  1778. return 4;
  1779. default:
  1780. return av_get_exact_bits_per_sample(codec_id);
  1781. }
  1782. }
  1783. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1784. {
  1785. int id, sr, ch, ba, tag, bps;
  1786. id = avctx->codec_id;
  1787. sr = avctx->sample_rate;
  1788. ch = avctx->channels;
  1789. ba = avctx->block_align;
  1790. tag = avctx->codec_tag;
  1791. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1792. /* codecs with an exact constant bits per sample */
  1793. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1794. return (frame_bytes * 8) / (bps * ch);
  1795. bps = avctx->bits_per_coded_sample;
  1796. /* codecs with a fixed packet duration */
  1797. switch (id) {
  1798. case CODEC_ID_ADPCM_ADX: return 32;
  1799. case CODEC_ID_ADPCM_IMA_QT: return 64;
  1800. case CODEC_ID_ADPCM_EA_XAS: return 128;
  1801. case CODEC_ID_AMR_NB:
  1802. case CODEC_ID_GSM:
  1803. case CODEC_ID_QCELP:
  1804. case CODEC_ID_RA_144:
  1805. case CODEC_ID_RA_288: return 160;
  1806. case CODEC_ID_IMC: return 256;
  1807. case CODEC_ID_AMR_WB:
  1808. case CODEC_ID_GSM_MS: return 320;
  1809. case CODEC_ID_MP1: return 384;
  1810. case CODEC_ID_ATRAC1: return 512;
  1811. case CODEC_ID_ATRAC3: return 1024;
  1812. case CODEC_ID_MP2:
  1813. case CODEC_ID_MUSEPACK7: return 1152;
  1814. case CODEC_ID_AC3: return 1536;
  1815. }
  1816. if (sr > 0) {
  1817. /* calc from sample rate */
  1818. if (id == CODEC_ID_TTA)
  1819. return 256 * sr / 245;
  1820. if (ch > 0) {
  1821. /* calc from sample rate and channels */
  1822. if (id == CODEC_ID_BINKAUDIO_DCT)
  1823. return (480 << (sr / 22050)) / ch;
  1824. }
  1825. }
  1826. if (ba > 0) {
  1827. /* calc from block_align */
  1828. if (id == CODEC_ID_SIPR) {
  1829. switch (ba) {
  1830. case 20: return 160;
  1831. case 19: return 144;
  1832. case 29: return 288;
  1833. case 37: return 480;
  1834. }
  1835. }
  1836. }
  1837. if (frame_bytes > 0) {
  1838. /* calc from frame_bytes only */
  1839. if (id == CODEC_ID_TRUESPEECH)
  1840. return 240 * (frame_bytes / 32);
  1841. if (id == CODEC_ID_NELLYMOSER)
  1842. return 256 * (frame_bytes / 64);
  1843. if (bps > 0) {
  1844. /* calc from frame_bytes and bits_per_coded_sample */
  1845. if (id == CODEC_ID_ADPCM_G726)
  1846. return frame_bytes * 8 / bps;
  1847. }
  1848. if (ch > 0) {
  1849. /* calc from frame_bytes and channels */
  1850. switch (id) {
  1851. case CODEC_ID_ADPCM_4XM:
  1852. case CODEC_ID_ADPCM_IMA_ISS:
  1853. return (frame_bytes - 4 * ch) * 2 / ch;
  1854. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1855. return (frame_bytes - 4) * 2 / ch;
  1856. case CODEC_ID_ADPCM_IMA_AMV:
  1857. return (frame_bytes - 8) * 2 / ch;
  1858. case CODEC_ID_ADPCM_XA:
  1859. return (frame_bytes / 128) * 224 / ch;
  1860. case CODEC_ID_INTERPLAY_DPCM:
  1861. return (frame_bytes - 6 - ch) / ch;
  1862. case CODEC_ID_ROQ_DPCM:
  1863. return (frame_bytes - 8) / ch;
  1864. case CODEC_ID_XAN_DPCM:
  1865. return (frame_bytes - 2 * ch) / ch;
  1866. case CODEC_ID_MACE3:
  1867. return 3 * frame_bytes / ch;
  1868. case CODEC_ID_MACE6:
  1869. return 6 * frame_bytes / ch;
  1870. case CODEC_ID_PCM_LXF:
  1871. return 2 * (frame_bytes / (5 * ch));
  1872. }
  1873. if (tag) {
  1874. /* calc from frame_bytes, channels, and codec_tag */
  1875. if (id == CODEC_ID_SOL_DPCM) {
  1876. if (tag == 3)
  1877. return frame_bytes / ch;
  1878. else
  1879. return frame_bytes * 2 / ch;
  1880. }
  1881. }
  1882. if (ba > 0) {
  1883. /* calc from frame_bytes, channels, and block_align */
  1884. int blocks = frame_bytes / ba;
  1885. switch (avctx->codec_id) {
  1886. case CODEC_ID_ADPCM_IMA_WAV:
  1887. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1888. case CODEC_ID_ADPCM_IMA_DK3:
  1889. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1890. case CODEC_ID_ADPCM_IMA_DK4:
  1891. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1892. case CODEC_ID_ADPCM_MS:
  1893. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1894. }
  1895. }
  1896. if (bps > 0) {
  1897. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1898. switch (avctx->codec_id) {
  1899. case CODEC_ID_PCM_DVD:
  1900. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1901. case CODEC_ID_PCM_BLURAY:
  1902. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1903. case CODEC_ID_S302M:
  1904. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1905. }
  1906. }
  1907. }
  1908. }
  1909. return 0;
  1910. }
  1911. #if !HAVE_THREADS
  1912. int ff_thread_init(AVCodecContext *s){
  1913. return -1;
  1914. }
  1915. #endif
  1916. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1917. {
  1918. unsigned int n = 0;
  1919. while(v >= 0xff) {
  1920. *s++ = 0xff;
  1921. v -= 0xff;
  1922. n++;
  1923. }
  1924. *s = v;
  1925. n++;
  1926. return n;
  1927. }
  1928. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1929. int i;
  1930. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1931. return i;
  1932. }
  1933. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1934. {
  1935. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1936. "version to the newest one from Git. If the problem still "
  1937. "occurs, it means that your file has a feature which has not "
  1938. "been implemented.\n", feature);
  1939. if(want_sample)
  1940. av_log_ask_for_sample(avc, NULL);
  1941. }
  1942. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1943. {
  1944. va_list argument_list;
  1945. va_start(argument_list, msg);
  1946. if (msg)
  1947. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1948. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1949. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1950. "and contact the ffmpeg-devel mailing list.\n");
  1951. va_end(argument_list);
  1952. }
  1953. static AVHWAccel *first_hwaccel = NULL;
  1954. void av_register_hwaccel(AVHWAccel *hwaccel)
  1955. {
  1956. AVHWAccel **p = &first_hwaccel;
  1957. while (*p)
  1958. p = &(*p)->next;
  1959. *p = hwaccel;
  1960. hwaccel->next = NULL;
  1961. }
  1962. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1963. {
  1964. return hwaccel ? hwaccel->next : first_hwaccel;
  1965. }
  1966. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1967. {
  1968. AVHWAccel *hwaccel=NULL;
  1969. while((hwaccel= av_hwaccel_next(hwaccel))){
  1970. if ( hwaccel->id == codec_id
  1971. && hwaccel->pix_fmt == pix_fmt)
  1972. return hwaccel;
  1973. }
  1974. return NULL;
  1975. }
  1976. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1977. {
  1978. if (ff_lockmgr_cb) {
  1979. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1980. return -1;
  1981. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1982. return -1;
  1983. }
  1984. ff_lockmgr_cb = cb;
  1985. if (ff_lockmgr_cb) {
  1986. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1987. return -1;
  1988. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1989. return -1;
  1990. }
  1991. return 0;
  1992. }
  1993. int avpriv_lock_avformat(void)
  1994. {
  1995. if (ff_lockmgr_cb) {
  1996. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1997. return -1;
  1998. }
  1999. return 0;
  2000. }
  2001. int avpriv_unlock_avformat(void)
  2002. {
  2003. if (ff_lockmgr_cb) {
  2004. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2005. return -1;
  2006. }
  2007. return 0;
  2008. }
  2009. unsigned int avpriv_toupper4(unsigned int x)
  2010. {
  2011. return toupper( x &0xFF)
  2012. + (toupper((x>>8 )&0xFF)<<8 )
  2013. + (toupper((x>>16)&0xFF)<<16)
  2014. + (toupper((x>>24)&0xFF)<<24);
  2015. }
  2016. #if !HAVE_THREADS
  2017. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2018. {
  2019. f->owner = avctx;
  2020. ff_init_buffer_info(avctx, f);
  2021. return avctx->get_buffer(avctx, f);
  2022. }
  2023. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2024. {
  2025. f->owner->release_buffer(f->owner, f);
  2026. }
  2027. void ff_thread_finish_setup(AVCodecContext *avctx)
  2028. {
  2029. }
  2030. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2031. {
  2032. }
  2033. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2034. {
  2035. }
  2036. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2037. {
  2038. return 1;
  2039. }
  2040. #endif
  2041. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  2042. {
  2043. AVCodec *c= avcodec_find_decoder(codec_id);
  2044. if(!c)
  2045. c= avcodec_find_encoder(codec_id);
  2046. if(c)
  2047. return c->type;
  2048. if (codec_id <= CODEC_ID_NONE)
  2049. return AVMEDIA_TYPE_UNKNOWN;
  2050. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  2051. return AVMEDIA_TYPE_VIDEO;
  2052. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  2053. return AVMEDIA_TYPE_AUDIO;
  2054. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  2055. return AVMEDIA_TYPE_SUBTITLE;
  2056. return AVMEDIA_TYPE_UNKNOWN;
  2057. }
  2058. int avcodec_is_open(AVCodecContext *s)
  2059. {
  2060. return !!s->internal;
  2061. }