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.

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