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.

1493 lines
44KB

  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/avstring.h"
  27. #include "libavutil/crc.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "libavutil/dict.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "libavutil/opt.h"
  37. #include "imgconvert.h"
  38. #include "thread.h"
  39. #include "audioconvert.h"
  40. #include "internal.h"
  41. #include <stdlib.h>
  42. #include <stdarg.h>
  43. #include <limits.h>
  44. #include <float.h>
  45. static int volatile entangled_thread_counter=0;
  46. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  47. static void *codec_mutex;
  48. static void *avformat_mutex;
  49. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  50. {
  51. if(min_size < *size)
  52. return ptr;
  53. min_size= FFMAX(17*min_size/16 + 32, min_size);
  54. ptr= av_realloc(ptr, min_size);
  55. 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
  56. min_size= 0;
  57. *size= min_size;
  58. return ptr;
  59. }
  60. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  61. {
  62. void **p = ptr;
  63. if (min_size < *size)
  64. return;
  65. min_size= FFMAX(17*min_size/16 + 32, min_size);
  66. av_free(*p);
  67. *p = av_malloc(min_size);
  68. if (!*p) min_size = 0;
  69. *size= min_size;
  70. }
  71. /* encoder management */
  72. static AVCodec *first_avcodec = NULL;
  73. AVCodec *av_codec_next(AVCodec *c){
  74. if(c) return c->next;
  75. else return first_avcodec;
  76. }
  77. #if !FF_API_AVCODEC_INIT
  78. static
  79. #endif
  80. void avcodec_init(void)
  81. {
  82. static int initialized = 0;
  83. if (initialized != 0)
  84. return;
  85. initialized = 1;
  86. dsputil_static_init();
  87. }
  88. void avcodec_register(AVCodec *codec)
  89. {
  90. AVCodec **p;
  91. avcodec_init();
  92. p = &first_avcodec;
  93. while (*p != NULL) p = &(*p)->next;
  94. *p = codec;
  95. codec->next = NULL;
  96. if (codec->init_static_data)
  97. codec->init_static_data(codec);
  98. }
  99. unsigned avcodec_get_edge_width(void)
  100. {
  101. return EDGE_WIDTH;
  102. }
  103. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  104. s->coded_width = width;
  105. s->coded_height= height;
  106. s->width = -((-width )>>s->lowres);
  107. s->height= -((-height)>>s->lowres);
  108. }
  109. #define INTERNAL_BUFFER_SIZE (32+1)
  110. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  111. int w_align= 1;
  112. int h_align= 1;
  113. switch(s->pix_fmt){
  114. case PIX_FMT_YUV420P:
  115. case PIX_FMT_YUYV422:
  116. case PIX_FMT_UYVY422:
  117. case PIX_FMT_YUV422P:
  118. case PIX_FMT_YUV440P:
  119. case PIX_FMT_YUV444P:
  120. case PIX_FMT_GBRP:
  121. case PIX_FMT_GRAY8:
  122. case PIX_FMT_GRAY16BE:
  123. case PIX_FMT_GRAY16LE:
  124. case PIX_FMT_YUVJ420P:
  125. case PIX_FMT_YUVJ422P:
  126. case PIX_FMT_YUVJ440P:
  127. case PIX_FMT_YUVJ444P:
  128. case PIX_FMT_YUVA420P:
  129. case PIX_FMT_YUV420P9LE:
  130. case PIX_FMT_YUV420P9BE:
  131. case PIX_FMT_YUV420P10LE:
  132. case PIX_FMT_YUV420P10BE:
  133. case PIX_FMT_YUV422P9LE:
  134. case PIX_FMT_YUV422P9BE:
  135. case PIX_FMT_YUV422P10LE:
  136. case PIX_FMT_YUV422P10BE:
  137. case PIX_FMT_YUV444P9LE:
  138. case PIX_FMT_YUV444P9BE:
  139. case PIX_FMT_YUV444P10LE:
  140. case PIX_FMT_YUV444P10BE:
  141. case PIX_FMT_GBRP9LE:
  142. case PIX_FMT_GBRP9BE:
  143. case PIX_FMT_GBRP10LE:
  144. case PIX_FMT_GBRP10BE:
  145. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  146. h_align= 16;
  147. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264 || s->codec_id == CODEC_ID_PRORES)
  148. h_align= 32; // interlaced is rounded up to 2 MBs
  149. break;
  150. case PIX_FMT_YUV411P:
  151. case PIX_FMT_UYYVYY411:
  152. w_align=32;
  153. h_align=8;
  154. break;
  155. case PIX_FMT_YUV410P:
  156. if(s->codec_id == CODEC_ID_SVQ1){
  157. w_align=64;
  158. h_align=64;
  159. }
  160. case PIX_FMT_RGB555:
  161. if(s->codec_id == CODEC_ID_RPZA){
  162. w_align=4;
  163. h_align=4;
  164. }
  165. case PIX_FMT_PAL8:
  166. case PIX_FMT_BGR8:
  167. case PIX_FMT_RGB8:
  168. if(s->codec_id == CODEC_ID_SMC){
  169. w_align=4;
  170. h_align=4;
  171. }
  172. break;
  173. case PIX_FMT_BGR24:
  174. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  175. w_align=4;
  176. h_align=4;
  177. }
  178. break;
  179. default:
  180. w_align= 1;
  181. h_align= 1;
  182. break;
  183. }
  184. *width = FFALIGN(*width , w_align);
  185. *height= FFALIGN(*height, h_align);
  186. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  187. *height+=2; // some of the optimized chroma MC reads one line too much
  188. // which is also done in mpeg decoders with lowres > 0
  189. linesize_align[0] =
  190. linesize_align[1] =
  191. linesize_align[2] =
  192. linesize_align[3] = STRIDE_ALIGN;
  193. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  194. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  195. //picture size unneccessarily in some cases. The solution here is not
  196. //pretty and better ideas are welcome!
  197. #if HAVE_MMX
  198. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  199. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  200. s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
  201. linesize_align[0] =
  202. linesize_align[1] =
  203. linesize_align[2] = 16;
  204. }
  205. #endif
  206. }
  207. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  208. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  209. int linesize_align[4];
  210. int align;
  211. avcodec_align_dimensions2(s, width, height, linesize_align);
  212. align = FFMAX(linesize_align[0], linesize_align[3]);
  213. linesize_align[1] <<= chroma_shift;
  214. linesize_align[2] <<= chroma_shift;
  215. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  216. *width=FFALIGN(*width, align);
  217. }
  218. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  219. {
  220. if (s->pkt) {
  221. pic->pkt_pts = s->pkt->pts;
  222. pic->pkt_pos = s->pkt->pos;
  223. } else {
  224. pic->pkt_pts = AV_NOPTS_VALUE;
  225. pic->pkt_pos = -1;
  226. }
  227. pic->reordered_opaque= s->reordered_opaque;
  228. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  229. pic->width = s->width;
  230. pic->height = s->height;
  231. pic->format = s->pix_fmt;
  232. }
  233. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  234. int i;
  235. int w= s->width;
  236. int h= s->height;
  237. InternalBuffer *buf;
  238. int *picture_number;
  239. AVCodecInternal *avci = s->internal;
  240. if(pic->data[0]!=NULL) {
  241. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  242. return -1;
  243. }
  244. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  245. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  246. return -1;
  247. }
  248. if(av_image_check_size(w, h, 0, s))
  249. return -1;
  250. if (!avci->buffer) {
  251. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  252. sizeof(InternalBuffer));
  253. }
  254. buf = &avci->buffer[avci->buffer_count];
  255. picture_number = &(avci->buffer[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  256. (*picture_number)++;
  257. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  258. if(s->active_thread_type&FF_THREAD_FRAME) {
  259. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  260. return -1;
  261. }
  262. for(i=0; i<4; i++){
  263. av_freep(&buf->base[i]);
  264. buf->data[i]= NULL;
  265. }
  266. }
  267. if(buf->base[0]){
  268. pic->age= *picture_number - buf->last_pic_num;
  269. buf->last_pic_num= *picture_number;
  270. }else{
  271. int h_chroma_shift, v_chroma_shift;
  272. int size[4] = {0};
  273. int tmpsize;
  274. int unaligned;
  275. AVPicture picture;
  276. int stride_align[4];
  277. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  278. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  279. avcodec_align_dimensions2(s, &w, &h, stride_align);
  280. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  281. w+= EDGE_WIDTH*2;
  282. h+= EDGE_WIDTH*2;
  283. }
  284. do {
  285. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  286. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  287. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  288. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  289. w += w & ~(w-1);
  290. unaligned = 0;
  291. for (i=0; i<4; i++){
  292. unaligned |= picture.linesize[i] % stride_align[i];
  293. }
  294. } while (unaligned);
  295. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  296. if (tmpsize < 0)
  297. return -1;
  298. for (i=0; i<3 && picture.data[i+1]; i++)
  299. size[i] = picture.data[i+1] - picture.data[i];
  300. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  301. buf->last_pic_num= -256*256*256*64;
  302. memset(buf->base, 0, sizeof(buf->base));
  303. memset(buf->data, 0, sizeof(buf->data));
  304. for(i=0; i<4 && size[i]; i++){
  305. const int h_shift= i==0 ? 0 : h_chroma_shift;
  306. const int v_shift= i==0 ? 0 : v_chroma_shift;
  307. buf->linesize[i]= picture.linesize[i];
  308. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  309. if(buf->base[i]==NULL) return -1;
  310. memset(buf->base[i], 128, size[i]);
  311. // no edge if EDGE EMU or not planar YUV
  312. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  313. buf->data[i] = buf->base[i];
  314. else
  315. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  316. }
  317. if(size[1] && !size[2])
  318. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  319. buf->width = s->width;
  320. buf->height = s->height;
  321. buf->pix_fmt= s->pix_fmt;
  322. pic->age= 256*256*256*64;
  323. }
  324. pic->type= FF_BUFFER_TYPE_INTERNAL;
  325. for(i=0; i<4; i++){
  326. pic->base[i]= buf->base[i];
  327. pic->data[i]= buf->data[i];
  328. pic->linesize[i]= buf->linesize[i];
  329. }
  330. avci->buffer_count++;
  331. if (s->pkt) {
  332. pic->pkt_pts = s->pkt->pts;
  333. pic->pkt_pos = s->pkt->pos;
  334. } else {
  335. pic->pkt_pts = AV_NOPTS_VALUE;
  336. pic->pkt_pos = -1;
  337. }
  338. pic->reordered_opaque= s->reordered_opaque;
  339. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  340. pic->width = s->width;
  341. pic->height = s->height;
  342. pic->format = s->pix_fmt;
  343. if(s->debug&FF_DEBUG_BUFFERS)
  344. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  345. "buffers used\n", pic, avci->buffer_count);
  346. return 0;
  347. }
  348. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  349. int i;
  350. InternalBuffer *buf, *last;
  351. AVCodecInternal *avci = s->internal;
  352. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  353. assert(avci->buffer_count);
  354. if (avci->buffer) {
  355. buf = NULL; /* avoids warning */
  356. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  357. buf = &avci->buffer[i];
  358. if (buf->data[0] == pic->data[0])
  359. break;
  360. }
  361. assert(i < avci->buffer_count);
  362. avci->buffer_count--;
  363. last = &avci->buffer[avci->buffer_count];
  364. FFSWAP(InternalBuffer, *buf, *last);
  365. }
  366. for(i=0; i<4; i++){
  367. pic->data[i]=NULL;
  368. // pic->base[i]=NULL;
  369. }
  370. //printf("R%X\n", pic->opaque);
  371. if(s->debug&FF_DEBUG_BUFFERS)
  372. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  373. "buffers used\n", pic, avci->buffer_count);
  374. }
  375. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  376. AVFrame temp_pic;
  377. int i;
  378. /* If no picture return a new buffer */
  379. if(pic->data[0] == NULL) {
  380. /* We will copy from buffer, so must be readable */
  381. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  382. return s->get_buffer(s, pic);
  383. }
  384. /* If internal buffer type return the same buffer */
  385. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  386. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  387. else pic->pkt_pts= AV_NOPTS_VALUE;
  388. pic->reordered_opaque= s->reordered_opaque;
  389. return 0;
  390. }
  391. /*
  392. * Not internal type and reget_buffer not overridden, emulate cr buffer
  393. */
  394. temp_pic = *pic;
  395. for(i = 0; i < 4; i++)
  396. pic->data[i] = pic->base[i] = NULL;
  397. pic->opaque = NULL;
  398. /* Allocate new frame */
  399. if (s->get_buffer(s, pic))
  400. return -1;
  401. /* Copy image data from old buffer to new buffer */
  402. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  403. s->height);
  404. s->release_buffer(s, &temp_pic); // Release old frame
  405. return 0;
  406. }
  407. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  408. int i;
  409. for(i=0; i<count; i++){
  410. int r= func(c, (char*)arg + i*size);
  411. if(ret) ret[i]= r;
  412. }
  413. return 0;
  414. }
  415. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  416. int i;
  417. for(i=0; i<count; i++){
  418. int r= func(c, arg, i, 0);
  419. if(ret) ret[i]= r;
  420. }
  421. return 0;
  422. }
  423. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  424. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  425. ++fmt;
  426. return fmt[0];
  427. }
  428. void avcodec_get_frame_defaults(AVFrame *pic){
  429. memset(pic, 0, sizeof(AVFrame));
  430. pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  431. pic->pkt_pos = -1;
  432. pic->key_frame= 1;
  433. pic->sample_aspect_ratio = (AVRational){0, 1};
  434. pic->format = -1; /* unknown */
  435. }
  436. AVFrame *avcodec_alloc_frame(void){
  437. AVFrame *pic= av_malloc(sizeof(AVFrame));
  438. if(pic==NULL) return NULL;
  439. avcodec_get_frame_defaults(pic);
  440. return pic;
  441. }
  442. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  443. {
  444. memset(sub, 0, sizeof(*sub));
  445. sub->pts = AV_NOPTS_VALUE;
  446. }
  447. #if FF_API_AVCODEC_OPEN
  448. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  449. {
  450. return avcodec_open2(avctx, codec, NULL);
  451. }
  452. #endif
  453. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  454. {
  455. int ret = 0;
  456. AVDictionary *tmp = NULL;
  457. if (options)
  458. av_dict_copy(&tmp, *options, 0);
  459. /* If there is a user-supplied mutex locking routine, call it. */
  460. if (ff_lockmgr_cb) {
  461. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  462. return -1;
  463. }
  464. entangled_thread_counter++;
  465. if(entangled_thread_counter != 1){
  466. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  467. ret = -1;
  468. goto end;
  469. }
  470. if(avctx->codec || !codec) {
  471. ret = AVERROR(EINVAL);
  472. goto end;
  473. }
  474. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  475. if (!avctx->internal) {
  476. ret = AVERROR(ENOMEM);
  477. goto end;
  478. }
  479. if (codec->priv_data_size > 0) {
  480. if(!avctx->priv_data){
  481. avctx->priv_data = av_mallocz(codec->priv_data_size);
  482. if (!avctx->priv_data) {
  483. ret = AVERROR(ENOMEM);
  484. goto end;
  485. }
  486. if (codec->priv_class) {
  487. *(AVClass**)avctx->priv_data= codec->priv_class;
  488. av_opt_set_defaults(avctx->priv_data);
  489. }
  490. }
  491. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  492. goto free_and_end;
  493. } else {
  494. avctx->priv_data = NULL;
  495. }
  496. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  497. goto free_and_end;
  498. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  499. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  500. if(avctx->coded_width && avctx->coded_height)
  501. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  502. else if(avctx->width && avctx->height)
  503. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  504. }
  505. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  506. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  507. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  508. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  509. avcodec_set_dimensions(avctx, 0, 0);
  510. }
  511. /* if the decoder init function was already called previously,
  512. free the already allocated subtitle_header before overwriting it */
  513. if (codec->decode)
  514. av_freep(&avctx->subtitle_header);
  515. #define SANE_NB_CHANNELS 128U
  516. if (avctx->channels > SANE_NB_CHANNELS) {
  517. ret = AVERROR(EINVAL);
  518. goto free_and_end;
  519. }
  520. avctx->codec = codec;
  521. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  522. avctx->codec_id == CODEC_ID_NONE) {
  523. avctx->codec_type = codec->type;
  524. avctx->codec_id = codec->id;
  525. }
  526. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  527. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  528. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  529. ret = AVERROR(EINVAL);
  530. goto free_and_end;
  531. }
  532. avctx->frame_number = 0;
  533. #if FF_API_ER
  534. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
  535. avctx->error_recognition, avctx->err_recognition);
  536. switch(avctx->error_recognition){
  537. case FF_ER_EXPLODE : avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL;
  538. break;
  539. case FF_ER_VERY_AGGRESSIVE:
  540. case FF_ER_AGGRESSIVE : avctx->err_recognition |= AV_EF_AGGRESSIVE;
  541. case FF_ER_COMPLIANT : avctx->err_recognition |= AV_EF_COMPLIANT;
  542. case FF_ER_CAREFUL : avctx->err_recognition |= AV_EF_CAREFUL;
  543. }
  544. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
  545. avctx->error_recognition, avctx->err_recognition);
  546. #endif
  547. if (!HAVE_THREADS)
  548. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  549. if (HAVE_THREADS && !avctx->thread_opaque) {
  550. ret = ff_thread_init(avctx);
  551. if (ret < 0) {
  552. goto free_and_end;
  553. }
  554. }
  555. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  556. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  557. avctx->codec->max_lowres);
  558. ret = AVERROR(EINVAL);
  559. goto free_and_end;
  560. }
  561. if (avctx->codec->encode) {
  562. int i;
  563. if (avctx->codec->sample_fmts) {
  564. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  565. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  566. break;
  567. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  568. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  569. ret = AVERROR(EINVAL);
  570. goto free_and_end;
  571. }
  572. }
  573. if (avctx->codec->supported_samplerates) {
  574. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  575. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  576. break;
  577. if (avctx->codec->supported_samplerates[i] == 0) {
  578. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  579. ret = AVERROR(EINVAL);
  580. goto free_and_end;
  581. }
  582. }
  583. if (avctx->codec->channel_layouts) {
  584. if (!avctx->channel_layout) {
  585. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  586. } else {
  587. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  588. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  589. break;
  590. if (avctx->codec->channel_layouts[i] == 0) {
  591. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  592. ret = AVERROR(EINVAL);
  593. goto free_and_end;
  594. }
  595. }
  596. }
  597. if (avctx->channel_layout && avctx->channels) {
  598. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  599. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  600. ret = AVERROR(EINVAL);
  601. goto free_and_end;
  602. }
  603. } else if (avctx->channel_layout) {
  604. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  605. }
  606. }
  607. avctx->pts_correction_num_faulty_pts =
  608. avctx->pts_correction_num_faulty_dts = 0;
  609. avctx->pts_correction_last_pts =
  610. avctx->pts_correction_last_dts = INT64_MIN;
  611. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  612. ret = avctx->codec->init(avctx);
  613. if (ret < 0) {
  614. goto free_and_end;
  615. }
  616. }
  617. ret=0;
  618. end:
  619. entangled_thread_counter--;
  620. /* Release any user-supplied mutex. */
  621. if (ff_lockmgr_cb) {
  622. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  623. }
  624. if (options) {
  625. av_dict_free(options);
  626. *options = tmp;
  627. }
  628. return ret;
  629. free_and_end:
  630. av_dict_free(&tmp);
  631. av_freep(&avctx->priv_data);
  632. av_freep(&avctx->internal);
  633. avctx->codec= NULL;
  634. goto end;
  635. }
  636. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  637. const short *samples)
  638. {
  639. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  640. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  641. return -1;
  642. }
  643. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  644. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  645. avctx->frame_number++;
  646. return ret;
  647. }else
  648. return 0;
  649. }
  650. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  651. const AVFrame *pict)
  652. {
  653. if(buf_size < FF_MIN_BUFFER_SIZE){
  654. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  655. return -1;
  656. }
  657. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  658. return -1;
  659. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  660. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  661. avctx->frame_number++;
  662. emms_c(); //needed to avoid an emms_c() call before every return;
  663. return ret;
  664. }else
  665. return 0;
  666. }
  667. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  668. const AVSubtitle *sub)
  669. {
  670. int ret;
  671. if(sub->start_display_time) {
  672. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  673. return -1;
  674. }
  675. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  676. avctx->frame_number++;
  677. return ret;
  678. }
  679. /**
  680. * Attempt to guess proper monotonic timestamps for decoded video frames
  681. * which might have incorrect times. Input timestamps may wrap around, in
  682. * which case the output will as well.
  683. *
  684. * @param pts the pts field of the decoded AVPacket, as passed through
  685. * AVFrame.pkt_pts
  686. * @param dts the dts field of the decoded AVPacket
  687. * @return one of the input values, may be AV_NOPTS_VALUE
  688. */
  689. static int64_t guess_correct_pts(AVCodecContext *ctx,
  690. int64_t reordered_pts, int64_t dts)
  691. {
  692. int64_t pts = AV_NOPTS_VALUE;
  693. if (dts != AV_NOPTS_VALUE) {
  694. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  695. ctx->pts_correction_last_dts = dts;
  696. }
  697. if (reordered_pts != AV_NOPTS_VALUE) {
  698. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  699. ctx->pts_correction_last_pts = reordered_pts;
  700. }
  701. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  702. && reordered_pts != AV_NOPTS_VALUE)
  703. pts = reordered_pts;
  704. else
  705. pts = dts;
  706. return pts;
  707. }
  708. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  709. int *got_picture_ptr,
  710. AVPacket *avpkt)
  711. {
  712. int ret;
  713. *got_picture_ptr= 0;
  714. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  715. return -1;
  716. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  717. av_packet_split_side_data(avpkt);
  718. avctx->pkt = avpkt;
  719. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  720. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  721. avpkt);
  722. else {
  723. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  724. avpkt);
  725. picture->pkt_dts= avpkt->dts;
  726. if(!avctx->has_b_frames){
  727. picture->pkt_pos= avpkt->pos;
  728. }
  729. //FIXME these should be under if(!avctx->has_b_frames)
  730. if (!picture->sample_aspect_ratio.num)
  731. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  732. if (!picture->width)
  733. picture->width = avctx->width;
  734. if (!picture->height)
  735. picture->height = avctx->height;
  736. if (picture->format == PIX_FMT_NONE)
  737. picture->format = avctx->pix_fmt;
  738. }
  739. emms_c(); //needed to avoid an emms_c() call before every return;
  740. if (*got_picture_ptr){
  741. avctx->frame_number++;
  742. picture->best_effort_timestamp = guess_correct_pts(avctx,
  743. picture->pkt_pts,
  744. picture->pkt_dts);
  745. }
  746. }else
  747. ret= 0;
  748. return ret;
  749. }
  750. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  751. int *frame_size_ptr,
  752. AVPacket *avpkt)
  753. {
  754. int ret;
  755. avctx->pkt = avpkt;
  756. if (!avpkt->data && avpkt->size) {
  757. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  758. return AVERROR(EINVAL);
  759. }
  760. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  761. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  762. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  763. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  764. return -1;
  765. }
  766. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  767. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  768. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  769. return -1;
  770. }
  771. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  772. avctx->frame_number++;
  773. }else{
  774. ret= 0;
  775. *frame_size_ptr=0;
  776. }
  777. return ret;
  778. }
  779. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  780. int *got_sub_ptr,
  781. AVPacket *avpkt)
  782. {
  783. int ret;
  784. avctx->pkt = avpkt;
  785. *got_sub_ptr = 0;
  786. avcodec_get_subtitle_defaults(sub);
  787. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  788. if (*got_sub_ptr)
  789. avctx->frame_number++;
  790. return ret;
  791. }
  792. void avsubtitle_free(AVSubtitle *sub)
  793. {
  794. int i;
  795. for (i = 0; i < sub->num_rects; i++)
  796. {
  797. av_freep(&sub->rects[i]->pict.data[0]);
  798. av_freep(&sub->rects[i]->pict.data[1]);
  799. av_freep(&sub->rects[i]->pict.data[2]);
  800. av_freep(&sub->rects[i]->pict.data[3]);
  801. av_freep(&sub->rects[i]->text);
  802. av_freep(&sub->rects[i]->ass);
  803. av_freep(&sub->rects[i]);
  804. }
  805. av_freep(&sub->rects);
  806. memset(sub, 0, sizeof(AVSubtitle));
  807. }
  808. av_cold int avcodec_close(AVCodecContext *avctx)
  809. {
  810. /* If there is a user-supplied mutex locking routine, call it. */
  811. if (ff_lockmgr_cb) {
  812. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  813. return -1;
  814. }
  815. entangled_thread_counter++;
  816. if(entangled_thread_counter != 1){
  817. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  818. entangled_thread_counter--;
  819. return -1;
  820. }
  821. if (HAVE_THREADS && avctx->thread_opaque)
  822. ff_thread_free(avctx);
  823. if (avctx->codec && avctx->codec->close)
  824. avctx->codec->close(avctx);
  825. avcodec_default_free_buffers(avctx);
  826. avctx->coded_frame = NULL;
  827. av_freep(&avctx->internal);
  828. if (avctx->codec && avctx->codec->priv_class)
  829. av_opt_free(avctx->priv_data);
  830. av_opt_free(avctx);
  831. av_freep(&avctx->priv_data);
  832. if(avctx->codec && avctx->codec->encode)
  833. av_freep(&avctx->extradata);
  834. avctx->codec = NULL;
  835. avctx->active_thread_type = 0;
  836. entangled_thread_counter--;
  837. /* Release any user-supplied mutex. */
  838. if (ff_lockmgr_cb) {
  839. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  840. }
  841. return 0;
  842. }
  843. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  844. {
  845. switch(id){
  846. case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
  847. case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
  848. case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  849. default : return id;
  850. }
  851. }
  852. AVCodec *avcodec_find_encoder(enum CodecID id)
  853. {
  854. AVCodec *p, *experimental=NULL;
  855. p = first_avcodec;
  856. id= remap_deprecated_codec_id(id);
  857. while (p) {
  858. if (p->encode != NULL && p->id == id) {
  859. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  860. experimental = p;
  861. } else
  862. return p;
  863. }
  864. p = p->next;
  865. }
  866. return experimental;
  867. }
  868. AVCodec *avcodec_find_encoder_by_name(const char *name)
  869. {
  870. AVCodec *p;
  871. if (!name)
  872. return NULL;
  873. p = first_avcodec;
  874. while (p) {
  875. if (p->encode != NULL && strcmp(name,p->name) == 0)
  876. return p;
  877. p = p->next;
  878. }
  879. return NULL;
  880. }
  881. AVCodec *avcodec_find_decoder(enum CodecID id)
  882. {
  883. AVCodec *p, *experimental=NULL;
  884. p = first_avcodec;
  885. id= remap_deprecated_codec_id(id);
  886. while (p) {
  887. if (p->decode != NULL && p->id == id) {
  888. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  889. experimental = p;
  890. } else
  891. return p;
  892. }
  893. p = p->next;
  894. }
  895. return experimental;
  896. }
  897. AVCodec *avcodec_find_decoder_by_name(const char *name)
  898. {
  899. AVCodec *p;
  900. if (!name)
  901. return NULL;
  902. p = first_avcodec;
  903. while (p) {
  904. if (p->decode != NULL && strcmp(name,p->name) == 0)
  905. return p;
  906. p = p->next;
  907. }
  908. return NULL;
  909. }
  910. static int get_bit_rate(AVCodecContext *ctx)
  911. {
  912. int bit_rate;
  913. int bits_per_sample;
  914. switch(ctx->codec_type) {
  915. case AVMEDIA_TYPE_VIDEO:
  916. case AVMEDIA_TYPE_DATA:
  917. case AVMEDIA_TYPE_SUBTITLE:
  918. case AVMEDIA_TYPE_ATTACHMENT:
  919. bit_rate = ctx->bit_rate;
  920. break;
  921. case AVMEDIA_TYPE_AUDIO:
  922. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  923. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  924. break;
  925. default:
  926. bit_rate = 0;
  927. break;
  928. }
  929. return bit_rate;
  930. }
  931. const char *avcodec_get_name(enum CodecID id)
  932. {
  933. AVCodec *codec;
  934. #if !CONFIG_SMALL
  935. switch (id) {
  936. #include "libavcodec/codec_names.h"
  937. }
  938. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  939. #endif
  940. codec = avcodec_find_decoder(id);
  941. if (codec)
  942. return codec->name;
  943. codec = avcodec_find_encoder(id);
  944. if (codec)
  945. return codec->name;
  946. return "unknown_codec";
  947. }
  948. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  949. {
  950. int i, len, ret = 0;
  951. for (i = 0; i < 4; i++) {
  952. len = snprintf(buf, buf_size,
  953. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  954. buf += len;
  955. buf_size = buf_size > len ? buf_size - len : 0;
  956. ret += len;
  957. codec_tag>>=8;
  958. }
  959. return ret;
  960. }
  961. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  962. {
  963. const char *codec_type;
  964. const char *codec_name;
  965. const char *profile = NULL;
  966. AVCodec *p;
  967. int bitrate;
  968. AVRational display_aspect_ratio;
  969. if (!buf || buf_size <= 0)
  970. return;
  971. codec_type = av_get_media_type_string(enc->codec_type);
  972. codec_name = avcodec_get_name(enc->codec_id);
  973. if (enc->profile != FF_PROFILE_UNKNOWN) {
  974. p = encode ? avcodec_find_encoder(enc->codec_id) :
  975. avcodec_find_decoder(enc->codec_id);
  976. if (p)
  977. profile = av_get_profile_name(p, enc->profile);
  978. }
  979. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  980. codec_name, enc->mb_decision ? " (hq)" : "");
  981. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  982. if (profile)
  983. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  984. if (enc->codec_tag) {
  985. char tag_buf[32];
  986. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  987. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  988. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  989. }
  990. switch(enc->codec_type) {
  991. case AVMEDIA_TYPE_VIDEO:
  992. if (enc->pix_fmt != PIX_FMT_NONE) {
  993. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  994. ", %s",
  995. av_get_pix_fmt_name(enc->pix_fmt));
  996. }
  997. if (enc->width) {
  998. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  999. ", %dx%d",
  1000. enc->width, enc->height);
  1001. if (enc->sample_aspect_ratio.num) {
  1002. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1003. enc->width*enc->sample_aspect_ratio.num,
  1004. enc->height*enc->sample_aspect_ratio.den,
  1005. 1024*1024);
  1006. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1007. " [SAR %d:%d DAR %d:%d]",
  1008. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1009. display_aspect_ratio.num, display_aspect_ratio.den);
  1010. }
  1011. if(av_log_get_level() >= AV_LOG_DEBUG){
  1012. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1013. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1014. ", %d/%d",
  1015. enc->time_base.num/g, enc->time_base.den/g);
  1016. }
  1017. }
  1018. if (encode) {
  1019. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1020. ", q=%d-%d", enc->qmin, enc->qmax);
  1021. }
  1022. break;
  1023. case AVMEDIA_TYPE_AUDIO:
  1024. if (enc->sample_rate) {
  1025. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1026. ", %d Hz", enc->sample_rate);
  1027. }
  1028. av_strlcat(buf, ", ", buf_size);
  1029. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1030. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1031. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1032. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1033. }
  1034. break;
  1035. default:
  1036. return;
  1037. }
  1038. if (encode) {
  1039. if (enc->flags & CODEC_FLAG_PASS1)
  1040. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1041. ", pass 1");
  1042. if (enc->flags & CODEC_FLAG_PASS2)
  1043. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1044. ", pass 2");
  1045. }
  1046. bitrate = get_bit_rate(enc);
  1047. if (bitrate != 0) {
  1048. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1049. ", %d kb/s", bitrate / 1000);
  1050. }
  1051. }
  1052. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1053. {
  1054. const AVProfile *p;
  1055. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1056. return NULL;
  1057. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1058. if (p->profile == profile)
  1059. return p->name;
  1060. return NULL;
  1061. }
  1062. unsigned avcodec_version( void )
  1063. {
  1064. return LIBAVCODEC_VERSION_INT;
  1065. }
  1066. const char *avcodec_configuration(void)
  1067. {
  1068. return FFMPEG_CONFIGURATION;
  1069. }
  1070. const char *avcodec_license(void)
  1071. {
  1072. #define LICENSE_PREFIX "libavcodec license: "
  1073. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1074. }
  1075. void avcodec_flush_buffers(AVCodecContext *avctx)
  1076. {
  1077. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1078. ff_thread_flush(avctx);
  1079. else if(avctx->codec->flush)
  1080. avctx->codec->flush(avctx);
  1081. }
  1082. void avcodec_default_free_buffers(AVCodecContext *s){
  1083. AVCodecInternal *avci = s->internal;
  1084. int i, j;
  1085. if (!avci->buffer)
  1086. return;
  1087. if (avci->buffer_count)
  1088. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1089. avci->buffer_count);
  1090. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1091. InternalBuffer *buf = &avci->buffer[i];
  1092. for(j=0; j<4; j++){
  1093. av_freep(&buf->base[j]);
  1094. buf->data[j]= NULL;
  1095. }
  1096. }
  1097. av_freep(&avci->buffer);
  1098. avci->buffer_count=0;
  1099. }
  1100. #if FF_API_OLD_FF_PICT_TYPES
  1101. char av_get_pict_type_char(int pict_type){
  1102. return av_get_picture_type_char(pict_type);
  1103. }
  1104. #endif
  1105. int av_get_bits_per_sample(enum CodecID codec_id){
  1106. switch(codec_id){
  1107. case CODEC_ID_ADPCM_SBPRO_2:
  1108. return 2;
  1109. case CODEC_ID_ADPCM_SBPRO_3:
  1110. return 3;
  1111. case CODEC_ID_ADPCM_SBPRO_4:
  1112. case CODEC_ID_ADPCM_CT:
  1113. case CODEC_ID_ADPCM_IMA_WAV:
  1114. case CODEC_ID_ADPCM_IMA_QT:
  1115. case CODEC_ID_ADPCM_SWF:
  1116. case CODEC_ID_ADPCM_MS:
  1117. case CODEC_ID_ADPCM_YAMAHA:
  1118. return 4;
  1119. case CODEC_ID_ADPCM_G722:
  1120. case CODEC_ID_PCM_ALAW:
  1121. case CODEC_ID_PCM_MULAW:
  1122. case CODEC_ID_PCM_S8:
  1123. case CODEC_ID_PCM_U8:
  1124. case CODEC_ID_PCM_ZORK:
  1125. return 8;
  1126. case CODEC_ID_PCM_S16BE:
  1127. case CODEC_ID_PCM_S16LE:
  1128. case CODEC_ID_PCM_S16LE_PLANAR:
  1129. case CODEC_ID_PCM_U16BE:
  1130. case CODEC_ID_PCM_U16LE:
  1131. return 16;
  1132. case CODEC_ID_PCM_S24DAUD:
  1133. case CODEC_ID_PCM_S24BE:
  1134. case CODEC_ID_PCM_S24LE:
  1135. case CODEC_ID_PCM_U24BE:
  1136. case CODEC_ID_PCM_U24LE:
  1137. return 24;
  1138. case CODEC_ID_PCM_S32BE:
  1139. case CODEC_ID_PCM_S32LE:
  1140. case CODEC_ID_PCM_U32BE:
  1141. case CODEC_ID_PCM_U32LE:
  1142. case CODEC_ID_PCM_F32BE:
  1143. case CODEC_ID_PCM_F32LE:
  1144. return 32;
  1145. case CODEC_ID_PCM_F64BE:
  1146. case CODEC_ID_PCM_F64LE:
  1147. return 64;
  1148. default:
  1149. return 0;
  1150. }
  1151. }
  1152. #if FF_API_OLD_SAMPLE_FMT
  1153. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1154. return av_get_bytes_per_sample(sample_fmt) << 3;
  1155. }
  1156. #endif
  1157. #if !HAVE_THREADS
  1158. int ff_thread_init(AVCodecContext *s){
  1159. return -1;
  1160. }
  1161. #endif
  1162. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1163. {
  1164. unsigned int n = 0;
  1165. while(v >= 0xff) {
  1166. *s++ = 0xff;
  1167. v -= 0xff;
  1168. n++;
  1169. }
  1170. *s = v;
  1171. n++;
  1172. return n;
  1173. }
  1174. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1175. int i;
  1176. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1177. return i;
  1178. }
  1179. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1180. {
  1181. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1182. "version to the newest one from Git. If the problem still "
  1183. "occurs, it means that your file has a feature which has not "
  1184. "been implemented.\n", feature);
  1185. if(want_sample)
  1186. av_log_ask_for_sample(avc, NULL);
  1187. }
  1188. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1189. {
  1190. va_list argument_list;
  1191. va_start(argument_list, msg);
  1192. if (msg)
  1193. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1194. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1195. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1196. "and contact the ffmpeg-devel mailing list.\n");
  1197. va_end(argument_list);
  1198. }
  1199. static AVHWAccel *first_hwaccel = NULL;
  1200. void av_register_hwaccel(AVHWAccel *hwaccel)
  1201. {
  1202. AVHWAccel **p = &first_hwaccel;
  1203. while (*p)
  1204. p = &(*p)->next;
  1205. *p = hwaccel;
  1206. hwaccel->next = NULL;
  1207. }
  1208. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1209. {
  1210. return hwaccel ? hwaccel->next : first_hwaccel;
  1211. }
  1212. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1213. {
  1214. AVHWAccel *hwaccel=NULL;
  1215. while((hwaccel= av_hwaccel_next(hwaccel))){
  1216. if ( hwaccel->id == codec_id
  1217. && hwaccel->pix_fmt == pix_fmt)
  1218. return hwaccel;
  1219. }
  1220. return NULL;
  1221. }
  1222. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1223. {
  1224. if (ff_lockmgr_cb) {
  1225. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1226. return -1;
  1227. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1228. return -1;
  1229. }
  1230. ff_lockmgr_cb = cb;
  1231. if (ff_lockmgr_cb) {
  1232. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1233. return -1;
  1234. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1235. return -1;
  1236. }
  1237. return 0;
  1238. }
  1239. int avpriv_lock_avformat(void)
  1240. {
  1241. if (ff_lockmgr_cb) {
  1242. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1243. return -1;
  1244. }
  1245. return 0;
  1246. }
  1247. int avpriv_unlock_avformat(void)
  1248. {
  1249. if (ff_lockmgr_cb) {
  1250. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1251. return -1;
  1252. }
  1253. return 0;
  1254. }
  1255. unsigned int avpriv_toupper4(unsigned int x)
  1256. {
  1257. return toupper( x &0xFF)
  1258. + (toupper((x>>8 )&0xFF)<<8 )
  1259. + (toupper((x>>16)&0xFF)<<16)
  1260. + (toupper((x>>24)&0xFF)<<24);
  1261. }
  1262. #if !HAVE_THREADS
  1263. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1264. {
  1265. f->owner = avctx;
  1266. ff_init_buffer_info(avctx, f);
  1267. return avctx->get_buffer(avctx, f);
  1268. }
  1269. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1270. {
  1271. f->owner->release_buffer(f->owner, f);
  1272. }
  1273. void ff_thread_finish_setup(AVCodecContext *avctx)
  1274. {
  1275. }
  1276. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1277. {
  1278. }
  1279. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1280. {
  1281. }
  1282. #endif
  1283. #if FF_API_THREAD_INIT
  1284. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1285. {
  1286. s->thread_count = thread_count;
  1287. return ff_thread_init(s);
  1288. }
  1289. #endif
  1290. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1291. {
  1292. AVCodec *c= avcodec_find_decoder(codec_id);
  1293. if(!c)
  1294. c= avcodec_find_encoder(codec_id);
  1295. if(c)
  1296. return c->type;
  1297. if (codec_id <= CODEC_ID_NONE)
  1298. return AVMEDIA_TYPE_UNKNOWN;
  1299. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1300. return AVMEDIA_TYPE_VIDEO;
  1301. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1302. return AVMEDIA_TYPE_AUDIO;
  1303. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1304. return AVMEDIA_TYPE_SUBTITLE;
  1305. return AVMEDIA_TYPE_UNKNOWN;
  1306. }