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.

1979 lines
61KB

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