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.

2310 lines
72KB

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