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.

2394 lines
74KB

  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,
  1053. samples_size, 1)))
  1054. return ret;
  1055. /* fabricate frame pts from sample count.
  1056. this is needed because the avcodec_encode_audio() API does not have
  1057. a way for the user to provide pts */
  1058. if(avctx->sample_rate && avctx->time_base.num)
  1059. frame->pts = ff_samples_to_time_base(avctx,
  1060. avctx->internal->sample_count);
  1061. else
  1062. frame->pts = AV_NOPTS_VALUE;
  1063. avctx->internal->sample_count += frame->nb_samples;
  1064. } else {
  1065. frame = NULL;
  1066. }
  1067. got_packet = 0;
  1068. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1069. if (!ret && got_packet && avctx->coded_frame) {
  1070. avctx->coded_frame->pts = pkt.pts;
  1071. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1072. }
  1073. /* free any side data since we cannot return it */
  1074. ff_packet_free_side_data(&pkt);
  1075. if (frame && frame->extended_data != frame->data)
  1076. av_freep(&frame->extended_data);
  1077. return ret ? ret : pkt.size;
  1078. }
  1079. #endif
  1080. #if FF_API_OLD_ENCODE_VIDEO
  1081. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1082. const AVFrame *pict)
  1083. {
  1084. AVPacket pkt;
  1085. int ret, got_packet = 0;
  1086. if(buf_size < FF_MIN_BUFFER_SIZE){
  1087. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1088. return -1;
  1089. }
  1090. av_init_packet(&pkt);
  1091. pkt.data = buf;
  1092. pkt.size = buf_size;
  1093. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1094. if (!ret && got_packet && avctx->coded_frame) {
  1095. avctx->coded_frame->pts = pkt.pts;
  1096. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1097. }
  1098. /* free any side data since we cannot return it */
  1099. if (pkt.side_data_elems > 0) {
  1100. int i;
  1101. for (i = 0; i < pkt.side_data_elems; i++)
  1102. av_free(pkt.side_data[i].data);
  1103. av_freep(&pkt.side_data);
  1104. pkt.side_data_elems = 0;
  1105. }
  1106. return ret ? ret : pkt.size;
  1107. }
  1108. #endif
  1109. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1110. AVPacket *avpkt,
  1111. const AVFrame *frame,
  1112. int *got_packet_ptr)
  1113. {
  1114. int ret;
  1115. AVPacket user_pkt = *avpkt;
  1116. int needs_realloc = !user_pkt.data;
  1117. *got_packet_ptr = 0;
  1118. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1119. av_free_packet(avpkt);
  1120. av_init_packet(avpkt);
  1121. avpkt->size = 0;
  1122. return 0;
  1123. }
  1124. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1125. return AVERROR(EINVAL);
  1126. av_assert0(avctx->codec->encode2);
  1127. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1128. av_assert0(ret <= 0);
  1129. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1130. needs_realloc = 0;
  1131. if (user_pkt.data) {
  1132. if (user_pkt.size >= avpkt->size) {
  1133. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1134. } else {
  1135. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1136. avpkt->size = user_pkt.size;
  1137. ret = -1;
  1138. }
  1139. avpkt->data = user_pkt.data;
  1140. avpkt->destruct = user_pkt.destruct;
  1141. } else {
  1142. if (av_dup_packet(avpkt) < 0) {
  1143. ret = AVERROR(ENOMEM);
  1144. }
  1145. }
  1146. }
  1147. if (!ret) {
  1148. if (!*got_packet_ptr)
  1149. avpkt->size = 0;
  1150. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1151. avpkt->pts = avpkt->dts = frame->pts;
  1152. if (needs_realloc && avpkt->data &&
  1153. avpkt->destruct == av_destruct_packet) {
  1154. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1155. if (new_data)
  1156. avpkt->data = new_data;
  1157. }
  1158. avctx->frame_number++;
  1159. }
  1160. if (ret < 0 || !*got_packet_ptr)
  1161. av_free_packet(avpkt);
  1162. emms_c();
  1163. return ret;
  1164. }
  1165. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1166. const AVSubtitle *sub)
  1167. {
  1168. int ret;
  1169. if(sub->start_display_time) {
  1170. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1171. return -1;
  1172. }
  1173. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
  1174. avctx->frame_number++;
  1175. return ret;
  1176. }
  1177. /**
  1178. * Attempt to guess proper monotonic timestamps for decoded video frames
  1179. * which might have incorrect times. Input timestamps may wrap around, in
  1180. * which case the output will as well.
  1181. *
  1182. * @param pts the pts field of the decoded AVPacket, as passed through
  1183. * AVFrame.pkt_pts
  1184. * @param dts the dts field of the decoded AVPacket
  1185. * @return one of the input values, may be AV_NOPTS_VALUE
  1186. */
  1187. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1188. int64_t reordered_pts, int64_t dts)
  1189. {
  1190. int64_t pts = AV_NOPTS_VALUE;
  1191. if (dts != AV_NOPTS_VALUE) {
  1192. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1193. ctx->pts_correction_last_dts = dts;
  1194. }
  1195. if (reordered_pts != AV_NOPTS_VALUE) {
  1196. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1197. ctx->pts_correction_last_pts = reordered_pts;
  1198. }
  1199. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1200. && reordered_pts != AV_NOPTS_VALUE)
  1201. pts = reordered_pts;
  1202. else
  1203. pts = dts;
  1204. return pts;
  1205. }
  1206. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1207. {
  1208. int size = 0;
  1209. const uint8_t *data;
  1210. uint32_t flags;
  1211. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1212. return;
  1213. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1214. if (!data || size < 4)
  1215. return;
  1216. flags = bytestream_get_le32(&data);
  1217. size -= 4;
  1218. if (size < 4) /* Required for any of the changes */
  1219. return;
  1220. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1221. avctx->channels = bytestream_get_le32(&data);
  1222. size -= 4;
  1223. }
  1224. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1225. if (size < 8)
  1226. return;
  1227. avctx->channel_layout = bytestream_get_le64(&data);
  1228. size -= 8;
  1229. }
  1230. if (size < 4)
  1231. return;
  1232. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1233. avctx->sample_rate = bytestream_get_le32(&data);
  1234. size -= 4;
  1235. }
  1236. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1237. if (size < 8)
  1238. return;
  1239. avctx->width = bytestream_get_le32(&data);
  1240. avctx->height = bytestream_get_le32(&data);
  1241. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1242. size -= 8;
  1243. }
  1244. }
  1245. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1246. int *got_picture_ptr,
  1247. const AVPacket *avpkt)
  1248. {
  1249. int ret;
  1250. // copy to ensure we do not change avpkt
  1251. AVPacket tmp = *avpkt;
  1252. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1253. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1254. return AVERROR(EINVAL);
  1255. }
  1256. *got_picture_ptr= 0;
  1257. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1258. return -1;
  1259. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1260. int did_split = av_packet_split_side_data(&tmp);
  1261. apply_param_change(avctx, &tmp);
  1262. avctx->pkt = &tmp;
  1263. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1264. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1265. &tmp);
  1266. else {
  1267. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1268. &tmp);
  1269. picture->pkt_dts= avpkt->dts;
  1270. if(!avctx->has_b_frames){
  1271. picture->pkt_pos= avpkt->pos;
  1272. }
  1273. //FIXME these should be under if(!avctx->has_b_frames)
  1274. if (!picture->sample_aspect_ratio.num)
  1275. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1276. if (!picture->width)
  1277. picture->width = avctx->width;
  1278. if (!picture->height)
  1279. picture->height = avctx->height;
  1280. if (picture->format == PIX_FMT_NONE)
  1281. picture->format = avctx->pix_fmt;
  1282. }
  1283. emms_c(); //needed to avoid an emms_c() call before every return;
  1284. avctx->pkt = NULL;
  1285. if (did_split) {
  1286. ff_packet_free_side_data(&tmp);
  1287. if(ret == tmp.size)
  1288. ret = avpkt->size;
  1289. }
  1290. if (*got_picture_ptr){
  1291. avctx->frame_number++;
  1292. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1293. picture->pkt_pts,
  1294. picture->pkt_dts);
  1295. }
  1296. }else
  1297. ret= 0;
  1298. return ret;
  1299. }
  1300. #if FF_API_OLD_DECODE_AUDIO
  1301. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1302. int *frame_size_ptr,
  1303. AVPacket *avpkt)
  1304. {
  1305. AVFrame frame;
  1306. int ret, got_frame = 0;
  1307. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1308. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1309. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1310. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1311. "avcodec_decode_audio4()\n");
  1312. avctx->get_buffer = avcodec_default_get_buffer;
  1313. avctx->release_buffer = avcodec_default_release_buffer;
  1314. }
  1315. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1316. if (ret >= 0 && got_frame) {
  1317. int ch, plane_size;
  1318. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1319. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1320. frame.nb_samples,
  1321. avctx->sample_fmt, 1);
  1322. if (*frame_size_ptr < data_size) {
  1323. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1324. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1325. return AVERROR(EINVAL);
  1326. }
  1327. memcpy(samples, frame.extended_data[0], plane_size);
  1328. if (planar && avctx->channels > 1) {
  1329. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1330. for (ch = 1; ch < avctx->channels; ch++) {
  1331. memcpy(out, frame.extended_data[ch], plane_size);
  1332. out += plane_size;
  1333. }
  1334. }
  1335. *frame_size_ptr = data_size;
  1336. } else {
  1337. *frame_size_ptr = 0;
  1338. }
  1339. return ret;
  1340. }
  1341. #endif
  1342. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1343. AVFrame *frame,
  1344. int *got_frame_ptr,
  1345. const AVPacket *avpkt)
  1346. {
  1347. int ret = 0;
  1348. *got_frame_ptr = 0;
  1349. if (!avpkt->data && avpkt->size) {
  1350. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1351. return AVERROR(EINVAL);
  1352. }
  1353. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1354. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1355. return AVERROR(EINVAL);
  1356. }
  1357. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1358. // copy to ensure we do not change avpkt
  1359. AVPacket tmp = *avpkt;
  1360. int did_split = av_packet_split_side_data(&tmp);
  1361. apply_param_change(avctx, &tmp);
  1362. avctx->pkt = &tmp;
  1363. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1364. if (ret >= 0 && *got_frame_ptr) {
  1365. avctx->frame_number++;
  1366. frame->pkt_dts = avpkt->dts;
  1367. if (frame->format == AV_SAMPLE_FMT_NONE)
  1368. frame->format = avctx->sample_fmt;
  1369. if (!frame->channel_layout)
  1370. frame->channel_layout = avctx->channel_layout;
  1371. if (!frame->sample_rate)
  1372. frame->sample_rate = avctx->sample_rate;
  1373. }
  1374. avctx->pkt = NULL;
  1375. if (did_split) {
  1376. ff_packet_free_side_data(&tmp);
  1377. if(ret == tmp.size)
  1378. ret = avpkt->size;
  1379. }
  1380. }
  1381. return ret;
  1382. }
  1383. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1384. int *got_sub_ptr,
  1385. AVPacket *avpkt)
  1386. {
  1387. int ret;
  1388. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1389. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1390. return AVERROR(EINVAL);
  1391. }
  1392. avctx->pkt = avpkt;
  1393. *got_sub_ptr = 0;
  1394. avcodec_get_subtitle_defaults(sub);
  1395. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1396. if (*got_sub_ptr)
  1397. avctx->frame_number++;
  1398. return ret;
  1399. }
  1400. void avsubtitle_free(AVSubtitle *sub)
  1401. {
  1402. int i;
  1403. for (i = 0; i < sub->num_rects; i++)
  1404. {
  1405. av_freep(&sub->rects[i]->pict.data[0]);
  1406. av_freep(&sub->rects[i]->pict.data[1]);
  1407. av_freep(&sub->rects[i]->pict.data[2]);
  1408. av_freep(&sub->rects[i]->pict.data[3]);
  1409. av_freep(&sub->rects[i]->text);
  1410. av_freep(&sub->rects[i]->ass);
  1411. av_freep(&sub->rects[i]);
  1412. }
  1413. av_freep(&sub->rects);
  1414. memset(sub, 0, sizeof(AVSubtitle));
  1415. }
  1416. av_cold int avcodec_close(AVCodecContext *avctx)
  1417. {
  1418. /* If there is a user-supplied mutex locking routine, call it. */
  1419. if (ff_lockmgr_cb) {
  1420. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1421. return -1;
  1422. }
  1423. entangled_thread_counter++;
  1424. if(entangled_thread_counter != 1){
  1425. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1426. entangled_thread_counter--;
  1427. return -1;
  1428. }
  1429. if (avcodec_is_open(avctx)) {
  1430. if (HAVE_THREADS && avctx->thread_opaque)
  1431. ff_thread_free(avctx);
  1432. if (avctx->codec && avctx->codec->close)
  1433. avctx->codec->close(avctx);
  1434. avcodec_default_free_buffers(avctx);
  1435. avctx->coded_frame = NULL;
  1436. avctx->internal->byte_buffer_size = 0;
  1437. av_freep(&avctx->internal->byte_buffer);
  1438. av_freep(&avctx->internal);
  1439. }
  1440. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1441. av_opt_free(avctx->priv_data);
  1442. av_opt_free(avctx);
  1443. av_freep(&avctx->priv_data);
  1444. if (av_codec_is_encoder(avctx->codec))
  1445. av_freep(&avctx->extradata);
  1446. avctx->codec = NULL;
  1447. avctx->active_thread_type = 0;
  1448. entangled_thread_counter--;
  1449. /* Release any user-supplied mutex. */
  1450. if (ff_lockmgr_cb) {
  1451. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1452. }
  1453. return 0;
  1454. }
  1455. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1456. {
  1457. switch(id){
  1458. //This is for future deprecatec codec ids, its empty since
  1459. //last major bump but will fill up again over time, please dont remove it
  1460. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1461. default : return id;
  1462. }
  1463. }
  1464. AVCodec *avcodec_find_encoder(enum CodecID id)
  1465. {
  1466. AVCodec *p, *experimental=NULL;
  1467. p = first_avcodec;
  1468. id= remap_deprecated_codec_id(id);
  1469. while (p) {
  1470. if (av_codec_is_encoder(p) && p->id == id) {
  1471. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1472. experimental = p;
  1473. } else
  1474. return p;
  1475. }
  1476. p = p->next;
  1477. }
  1478. return experimental;
  1479. }
  1480. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1481. {
  1482. AVCodec *p;
  1483. if (!name)
  1484. return NULL;
  1485. p = first_avcodec;
  1486. while (p) {
  1487. if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1488. return p;
  1489. p = p->next;
  1490. }
  1491. return NULL;
  1492. }
  1493. AVCodec *avcodec_find_decoder(enum CodecID id)
  1494. {
  1495. AVCodec *p, *experimental=NULL;
  1496. p = first_avcodec;
  1497. id= remap_deprecated_codec_id(id);
  1498. while (p) {
  1499. if (av_codec_is_decoder(p) && p->id == id) {
  1500. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1501. experimental = p;
  1502. } else
  1503. return p;
  1504. }
  1505. p = p->next;
  1506. }
  1507. return experimental;
  1508. }
  1509. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1510. {
  1511. AVCodec *p;
  1512. if (!name)
  1513. return NULL;
  1514. p = first_avcodec;
  1515. while (p) {
  1516. if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1517. return p;
  1518. p = p->next;
  1519. }
  1520. return NULL;
  1521. }
  1522. const char *avcodec_get_name(enum CodecID id)
  1523. {
  1524. AVCodec *codec;
  1525. #if !CONFIG_SMALL
  1526. switch (id) {
  1527. #include "libavcodec/codec_names.h"
  1528. }
  1529. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1530. #endif
  1531. codec = avcodec_find_decoder(id);
  1532. if (codec)
  1533. return codec->name;
  1534. codec = avcodec_find_encoder(id);
  1535. if (codec)
  1536. return codec->name;
  1537. return "unknown_codec";
  1538. }
  1539. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1540. {
  1541. int i, len, ret = 0;
  1542. for (i = 0; i < 4; i++) {
  1543. len = snprintf(buf, buf_size,
  1544. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1545. buf += len;
  1546. buf_size = buf_size > len ? buf_size - len : 0;
  1547. ret += len;
  1548. codec_tag>>=8;
  1549. }
  1550. return ret;
  1551. }
  1552. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1553. {
  1554. const char *codec_type;
  1555. const char *codec_name;
  1556. const char *profile = NULL;
  1557. AVCodec *p;
  1558. int bitrate;
  1559. AVRational display_aspect_ratio;
  1560. if (!buf || buf_size <= 0)
  1561. return;
  1562. codec_type = av_get_media_type_string(enc->codec_type);
  1563. codec_name = avcodec_get_name(enc->codec_id);
  1564. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1565. if (enc->codec)
  1566. p = enc->codec;
  1567. else
  1568. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1569. avcodec_find_decoder(enc->codec_id);
  1570. if (p)
  1571. profile = av_get_profile_name(p, enc->profile);
  1572. }
  1573. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1574. codec_name, enc->mb_decision ? " (hq)" : "");
  1575. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1576. if (profile)
  1577. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1578. if (enc->codec_tag) {
  1579. char tag_buf[32];
  1580. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1581. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1582. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1583. }
  1584. switch(enc->codec_type) {
  1585. case AVMEDIA_TYPE_VIDEO:
  1586. if (enc->pix_fmt != PIX_FMT_NONE) {
  1587. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1588. ", %s",
  1589. av_get_pix_fmt_name(enc->pix_fmt));
  1590. }
  1591. if (enc->width) {
  1592. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1593. ", %dx%d",
  1594. enc->width, enc->height);
  1595. if (enc->sample_aspect_ratio.num) {
  1596. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1597. enc->width*enc->sample_aspect_ratio.num,
  1598. enc->height*enc->sample_aspect_ratio.den,
  1599. 1024*1024);
  1600. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1601. " [SAR %d:%d DAR %d:%d]",
  1602. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1603. display_aspect_ratio.num, display_aspect_ratio.den);
  1604. }
  1605. if(av_log_get_level() >= AV_LOG_DEBUG){
  1606. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1607. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1608. ", %d/%d",
  1609. enc->time_base.num/g, enc->time_base.den/g);
  1610. }
  1611. }
  1612. if (encode) {
  1613. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1614. ", q=%d-%d", enc->qmin, enc->qmax);
  1615. }
  1616. break;
  1617. case AVMEDIA_TYPE_AUDIO:
  1618. if (enc->sample_rate) {
  1619. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1620. ", %d Hz", enc->sample_rate);
  1621. }
  1622. av_strlcat(buf, ", ", buf_size);
  1623. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1624. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1625. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1626. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1627. }
  1628. break;
  1629. default:
  1630. return;
  1631. }
  1632. if (encode) {
  1633. if (enc->flags & CODEC_FLAG_PASS1)
  1634. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1635. ", pass 1");
  1636. if (enc->flags & CODEC_FLAG_PASS2)
  1637. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1638. ", pass 2");
  1639. }
  1640. bitrate = get_bit_rate(enc);
  1641. if (bitrate != 0) {
  1642. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1643. ", %d kb/s", bitrate / 1000);
  1644. }
  1645. }
  1646. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1647. {
  1648. const AVProfile *p;
  1649. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1650. return NULL;
  1651. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1652. if (p->profile == profile)
  1653. return p->name;
  1654. return NULL;
  1655. }
  1656. unsigned avcodec_version( void )
  1657. {
  1658. // av_assert0(CODEC_ID_V410==164);
  1659. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1660. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1661. // av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1662. av_assert0(CODEC_ID_SRT==94216);
  1663. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1664. return LIBAVCODEC_VERSION_INT;
  1665. }
  1666. const char *avcodec_configuration(void)
  1667. {
  1668. return FFMPEG_CONFIGURATION;
  1669. }
  1670. const char *avcodec_license(void)
  1671. {
  1672. #define LICENSE_PREFIX "libavcodec license: "
  1673. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1674. }
  1675. void avcodec_flush_buffers(AVCodecContext *avctx)
  1676. {
  1677. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1678. ff_thread_flush(avctx);
  1679. else if(avctx->codec->flush)
  1680. avctx->codec->flush(avctx);
  1681. avctx->pts_correction_last_pts =
  1682. avctx->pts_correction_last_dts = INT64_MIN;
  1683. }
  1684. static void video_free_buffers(AVCodecContext *s)
  1685. {
  1686. AVCodecInternal *avci = s->internal;
  1687. int i, j;
  1688. if (!avci->buffer)
  1689. return;
  1690. if (avci->buffer_count)
  1691. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1692. avci->buffer_count);
  1693. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1694. InternalBuffer *buf = &avci->buffer[i];
  1695. for(j=0; j<4; j++){
  1696. av_freep(&buf->base[j]);
  1697. buf->data[j]= NULL;
  1698. }
  1699. }
  1700. av_freep(&avci->buffer);
  1701. avci->buffer_count=0;
  1702. }
  1703. static void audio_free_buffers(AVCodecContext *avctx)
  1704. {
  1705. AVCodecInternal *avci = avctx->internal;
  1706. InternalBuffer *buf;
  1707. if (!avci->buffer)
  1708. return;
  1709. buf = avci->buffer;
  1710. if (buf->extended_data) {
  1711. av_free(buf->extended_data[0]);
  1712. if (buf->extended_data != buf->data)
  1713. av_freep(&buf->extended_data);
  1714. }
  1715. av_freep(&avci->buffer);
  1716. }
  1717. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1718. {
  1719. switch (avctx->codec_type) {
  1720. case AVMEDIA_TYPE_VIDEO:
  1721. video_free_buffers(avctx);
  1722. break;
  1723. case AVMEDIA_TYPE_AUDIO:
  1724. audio_free_buffers(avctx);
  1725. break;
  1726. default:
  1727. break;
  1728. }
  1729. }
  1730. int av_get_exact_bits_per_sample(enum CodecID codec_id)
  1731. {
  1732. switch(codec_id){
  1733. case CODEC_ID_ADPCM_CT:
  1734. case CODEC_ID_ADPCM_IMA_APC:
  1735. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1736. case CODEC_ID_ADPCM_IMA_WS:
  1737. case CODEC_ID_ADPCM_G722:
  1738. case CODEC_ID_ADPCM_YAMAHA:
  1739. return 4;
  1740. case CODEC_ID_PCM_ALAW:
  1741. case CODEC_ID_PCM_MULAW:
  1742. case CODEC_ID_PCM_S8:
  1743. case CODEC_ID_PCM_U8:
  1744. case CODEC_ID_PCM_ZORK:
  1745. return 8;
  1746. case CODEC_ID_PCM_S16BE:
  1747. case CODEC_ID_PCM_S16LE:
  1748. case CODEC_ID_PCM_S16LE_PLANAR:
  1749. case CODEC_ID_PCM_U16BE:
  1750. case CODEC_ID_PCM_U16LE:
  1751. return 16;
  1752. case CODEC_ID_PCM_S24DAUD:
  1753. case CODEC_ID_PCM_S24BE:
  1754. case CODEC_ID_PCM_S24LE:
  1755. case CODEC_ID_PCM_U24BE:
  1756. case CODEC_ID_PCM_U24LE:
  1757. return 24;
  1758. case CODEC_ID_PCM_S32BE:
  1759. case CODEC_ID_PCM_S32LE:
  1760. case CODEC_ID_PCM_U32BE:
  1761. case CODEC_ID_PCM_U32LE:
  1762. case CODEC_ID_PCM_F32BE:
  1763. case CODEC_ID_PCM_F32LE:
  1764. return 32;
  1765. case CODEC_ID_PCM_F64BE:
  1766. case CODEC_ID_PCM_F64LE:
  1767. return 64;
  1768. default:
  1769. return 0;
  1770. }
  1771. }
  1772. enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1773. {
  1774. static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
  1775. [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1776. [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1777. [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1778. [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1779. [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1780. [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1781. [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1782. [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1783. [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1784. [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1785. };
  1786. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1787. return CODEC_ID_NONE;
  1788. if (be < 0 || be > 1)
  1789. be = AV_NE(1, 0);
  1790. return map[fmt][be];
  1791. }
  1792. int av_get_bits_per_sample(enum CodecID codec_id)
  1793. {
  1794. switch (codec_id) {
  1795. case CODEC_ID_ADPCM_SBPRO_2:
  1796. return 2;
  1797. case CODEC_ID_ADPCM_SBPRO_3:
  1798. return 3;
  1799. case CODEC_ID_ADPCM_SBPRO_4:
  1800. case CODEC_ID_ADPCM_IMA_WAV:
  1801. case CODEC_ID_ADPCM_IMA_QT:
  1802. case CODEC_ID_ADPCM_SWF:
  1803. case CODEC_ID_ADPCM_MS:
  1804. return 4;
  1805. default:
  1806. return av_get_exact_bits_per_sample(codec_id);
  1807. }
  1808. }
  1809. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1810. {
  1811. int id, sr, ch, ba, tag, bps;
  1812. id = avctx->codec_id;
  1813. sr = avctx->sample_rate;
  1814. ch = avctx->channels;
  1815. ba = avctx->block_align;
  1816. tag = avctx->codec_tag;
  1817. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1818. /* codecs with an exact constant bits per sample */
  1819. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1820. return (frame_bytes * 8LL) / (bps * ch);
  1821. bps = avctx->bits_per_coded_sample;
  1822. /* codecs with a fixed packet duration */
  1823. switch (id) {
  1824. case CODEC_ID_ADPCM_ADX: return 32;
  1825. case CODEC_ID_ADPCM_IMA_QT: return 64;
  1826. case CODEC_ID_ADPCM_EA_XAS: return 128;
  1827. case CODEC_ID_AMR_NB:
  1828. case CODEC_ID_GSM:
  1829. case CODEC_ID_QCELP:
  1830. case CODEC_ID_RA_144:
  1831. case CODEC_ID_RA_288: return 160;
  1832. case CODEC_ID_IMC: return 256;
  1833. case CODEC_ID_AMR_WB:
  1834. case CODEC_ID_GSM_MS: return 320;
  1835. case CODEC_ID_MP1: return 384;
  1836. case CODEC_ID_ATRAC1: return 512;
  1837. case CODEC_ID_ATRAC3: return 1024;
  1838. case CODEC_ID_MP2:
  1839. case CODEC_ID_MUSEPACK7: return 1152;
  1840. case CODEC_ID_AC3: return 1536;
  1841. }
  1842. if (sr > 0) {
  1843. /* calc from sample rate */
  1844. if (id == CODEC_ID_TTA)
  1845. return 256 * sr / 245;
  1846. if (ch > 0) {
  1847. /* calc from sample rate and channels */
  1848. if (id == CODEC_ID_BINKAUDIO_DCT)
  1849. return (480 << (sr / 22050)) / ch;
  1850. }
  1851. }
  1852. if (ba > 0) {
  1853. /* calc from block_align */
  1854. if (id == CODEC_ID_SIPR) {
  1855. switch (ba) {
  1856. case 20: return 160;
  1857. case 19: return 144;
  1858. case 29: return 288;
  1859. case 37: return 480;
  1860. }
  1861. }
  1862. }
  1863. if (frame_bytes > 0) {
  1864. /* calc from frame_bytes only */
  1865. if (id == CODEC_ID_TRUESPEECH)
  1866. return 240 * (frame_bytes / 32);
  1867. if (id == CODEC_ID_NELLYMOSER)
  1868. return 256 * (frame_bytes / 64);
  1869. if (bps > 0) {
  1870. /* calc from frame_bytes and bits_per_coded_sample */
  1871. if (id == CODEC_ID_ADPCM_G726)
  1872. return frame_bytes * 8 / bps;
  1873. }
  1874. if (ch > 0) {
  1875. /* calc from frame_bytes and channels */
  1876. switch (id) {
  1877. case CODEC_ID_ADPCM_4XM:
  1878. case CODEC_ID_ADPCM_IMA_ISS:
  1879. return (frame_bytes - 4 * ch) * 2 / ch;
  1880. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1881. return (frame_bytes - 4) * 2 / ch;
  1882. case CODEC_ID_ADPCM_IMA_AMV:
  1883. return (frame_bytes - 8) * 2 / ch;
  1884. case CODEC_ID_ADPCM_XA:
  1885. return (frame_bytes / 128) * 224 / ch;
  1886. case CODEC_ID_INTERPLAY_DPCM:
  1887. return (frame_bytes - 6 - ch) / ch;
  1888. case CODEC_ID_ROQ_DPCM:
  1889. return (frame_bytes - 8) / ch;
  1890. case CODEC_ID_XAN_DPCM:
  1891. return (frame_bytes - 2 * ch) / ch;
  1892. case CODEC_ID_MACE3:
  1893. return 3 * frame_bytes / ch;
  1894. case CODEC_ID_MACE6:
  1895. return 6 * frame_bytes / ch;
  1896. case CODEC_ID_PCM_LXF:
  1897. return 2 * (frame_bytes / (5 * ch));
  1898. }
  1899. if (tag) {
  1900. /* calc from frame_bytes, channels, and codec_tag */
  1901. if (id == CODEC_ID_SOL_DPCM) {
  1902. if (tag == 3)
  1903. return frame_bytes / ch;
  1904. else
  1905. return frame_bytes * 2 / ch;
  1906. }
  1907. }
  1908. if (ba > 0) {
  1909. /* calc from frame_bytes, channels, and block_align */
  1910. int blocks = frame_bytes / ba;
  1911. switch (avctx->codec_id) {
  1912. case CODEC_ID_ADPCM_IMA_WAV:
  1913. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1914. case CODEC_ID_ADPCM_IMA_DK3:
  1915. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1916. case CODEC_ID_ADPCM_IMA_DK4:
  1917. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1918. case CODEC_ID_ADPCM_MS:
  1919. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1920. }
  1921. }
  1922. if (bps > 0) {
  1923. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1924. switch (avctx->codec_id) {
  1925. case CODEC_ID_PCM_DVD:
  1926. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1927. case CODEC_ID_PCM_BLURAY:
  1928. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1929. case CODEC_ID_S302M:
  1930. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1931. }
  1932. }
  1933. }
  1934. }
  1935. return 0;
  1936. }
  1937. #if !HAVE_THREADS
  1938. int ff_thread_init(AVCodecContext *s){
  1939. return -1;
  1940. }
  1941. #endif
  1942. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1943. {
  1944. unsigned int n = 0;
  1945. while(v >= 0xff) {
  1946. *s++ = 0xff;
  1947. v -= 0xff;
  1948. n++;
  1949. }
  1950. *s = v;
  1951. n++;
  1952. return n;
  1953. }
  1954. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1955. int i;
  1956. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1957. return i;
  1958. }
  1959. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1960. {
  1961. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1962. "version to the newest one from Git. If the problem still "
  1963. "occurs, it means that your file has a feature which has not "
  1964. "been implemented.\n", feature);
  1965. if(want_sample)
  1966. av_log_ask_for_sample(avc, NULL);
  1967. }
  1968. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1969. {
  1970. va_list argument_list;
  1971. va_start(argument_list, msg);
  1972. if (msg)
  1973. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1974. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1975. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1976. "and contact the ffmpeg-devel mailing list.\n");
  1977. va_end(argument_list);
  1978. }
  1979. static AVHWAccel *first_hwaccel = NULL;
  1980. void av_register_hwaccel(AVHWAccel *hwaccel)
  1981. {
  1982. AVHWAccel **p = &first_hwaccel;
  1983. while (*p)
  1984. p = &(*p)->next;
  1985. *p = hwaccel;
  1986. hwaccel->next = NULL;
  1987. }
  1988. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1989. {
  1990. return hwaccel ? hwaccel->next : first_hwaccel;
  1991. }
  1992. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1993. {
  1994. AVHWAccel *hwaccel=NULL;
  1995. while((hwaccel= av_hwaccel_next(hwaccel))){
  1996. if ( hwaccel->id == codec_id
  1997. && hwaccel->pix_fmt == pix_fmt)
  1998. return hwaccel;
  1999. }
  2000. return NULL;
  2001. }
  2002. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2003. {
  2004. if (ff_lockmgr_cb) {
  2005. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2006. return -1;
  2007. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2008. return -1;
  2009. }
  2010. ff_lockmgr_cb = cb;
  2011. if (ff_lockmgr_cb) {
  2012. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2013. return -1;
  2014. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2015. return -1;
  2016. }
  2017. return 0;
  2018. }
  2019. int avpriv_lock_avformat(void)
  2020. {
  2021. if (ff_lockmgr_cb) {
  2022. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2023. return -1;
  2024. }
  2025. return 0;
  2026. }
  2027. int avpriv_unlock_avformat(void)
  2028. {
  2029. if (ff_lockmgr_cb) {
  2030. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2031. return -1;
  2032. }
  2033. return 0;
  2034. }
  2035. unsigned int avpriv_toupper4(unsigned int x)
  2036. {
  2037. return toupper( x &0xFF)
  2038. + (toupper((x>>8 )&0xFF)<<8 )
  2039. + (toupper((x>>16)&0xFF)<<16)
  2040. + (toupper((x>>24)&0xFF)<<24);
  2041. }
  2042. #if !HAVE_THREADS
  2043. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2044. {
  2045. f->owner = avctx;
  2046. ff_init_buffer_info(avctx, f);
  2047. return avctx->get_buffer(avctx, f);
  2048. }
  2049. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2050. {
  2051. f->owner->release_buffer(f->owner, f);
  2052. }
  2053. void ff_thread_finish_setup(AVCodecContext *avctx)
  2054. {
  2055. }
  2056. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2057. {
  2058. }
  2059. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2060. {
  2061. }
  2062. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2063. {
  2064. return 1;
  2065. }
  2066. #endif
  2067. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  2068. {
  2069. AVCodec *c= avcodec_find_decoder(codec_id);
  2070. if(!c)
  2071. c= avcodec_find_encoder(codec_id);
  2072. if(c)
  2073. return c->type;
  2074. if (codec_id <= CODEC_ID_NONE)
  2075. return AVMEDIA_TYPE_UNKNOWN;
  2076. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  2077. return AVMEDIA_TYPE_VIDEO;
  2078. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  2079. return AVMEDIA_TYPE_AUDIO;
  2080. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  2081. return AVMEDIA_TYPE_SUBTITLE;
  2082. return AVMEDIA_TYPE_UNKNOWN;
  2083. }
  2084. int avcodec_is_open(AVCodecContext *s)
  2085. {
  2086. return !!s->internal;
  2087. }