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.

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