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.

1193 lines
34KB

  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/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavcore/audioconvert.h"
  31. #include "libavcore/imgutils.h"
  32. #include "libavcore/internal.h"
  33. #include "libavcore/samplefmt.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "libavutil/opt.h"
  37. #include "imgconvert.h"
  38. #include "audioconvert.h"
  39. #include "internal.h"
  40. #include <stdlib.h>
  41. #include <stdarg.h>
  42. #include <limits.h>
  43. #include <float.h>
  44. static int volatile entangled_thread_counter=0;
  45. int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  46. static void *codec_mutex;
  47. void *av_fast_realloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
  48. {
  49. if(min_size < *size)
  50. return ptr;
  51. min_size= FFMAX(17*min_size/16 + 32, min_size);
  52. ptr= av_realloc(ptr, min_size);
  53. 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
  54. min_size= 0;
  55. *size= min_size;
  56. return ptr;
  57. }
  58. void av_fast_malloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
  59. {
  60. void **p = ptr;
  61. if (min_size < *size)
  62. return;
  63. min_size= FFMAX(17*min_size/16 + 32, min_size);
  64. av_free(*p);
  65. *p = av_malloc(min_size);
  66. if (!*p) min_size = 0;
  67. *size= min_size;
  68. }
  69. /* encoder management */
  70. static AVCodec *first_avcodec = NULL;
  71. AVCodec *av_codec_next(AVCodec *c){
  72. if(c) return c->next;
  73. else return first_avcodec;
  74. }
  75. void avcodec_register(AVCodec *codec)
  76. {
  77. AVCodec **p;
  78. avcodec_init();
  79. p = &first_avcodec;
  80. while (*p != NULL) p = &(*p)->next;
  81. *p = codec;
  82. codec->next = NULL;
  83. }
  84. #if LIBAVCODEC_VERSION_MAJOR < 53
  85. void register_avcodec(AVCodec *codec)
  86. {
  87. avcodec_register(codec);
  88. }
  89. #endif
  90. unsigned avcodec_get_edge_width(void)
  91. {
  92. return EDGE_WIDTH;
  93. }
  94. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  95. s->coded_width = width;
  96. s->coded_height= height;
  97. s->width = -((-width )>>s->lowres);
  98. s->height= -((-height)>>s->lowres);
  99. }
  100. typedef struct InternalBuffer{
  101. int last_pic_num;
  102. uint8_t *base[4];
  103. uint8_t *data[4];
  104. int linesize[4];
  105. int width, height;
  106. enum PixelFormat pix_fmt;
  107. }InternalBuffer;
  108. #define INTERNAL_BUFFER_SIZE 32
  109. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  110. int w_align= 1;
  111. int h_align= 1;
  112. switch(s->pix_fmt){
  113. case PIX_FMT_YUV420P:
  114. case PIX_FMT_YUYV422:
  115. case PIX_FMT_UYVY422:
  116. case PIX_FMT_YUV422P:
  117. case PIX_FMT_YUV440P:
  118. case PIX_FMT_YUV444P:
  119. case PIX_FMT_GRAY8:
  120. case PIX_FMT_GRAY16BE:
  121. case PIX_FMT_GRAY16LE:
  122. case PIX_FMT_YUVJ420P:
  123. case PIX_FMT_YUVJ422P:
  124. case PIX_FMT_YUVJ440P:
  125. case PIX_FMT_YUVJ444P:
  126. case PIX_FMT_YUVA420P:
  127. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  128. h_align= 16;
  129. 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)
  130. h_align= 32; // interlaced is rounded up to 2 MBs
  131. break;
  132. case PIX_FMT_YUV411P:
  133. case PIX_FMT_UYYVYY411:
  134. w_align=32;
  135. h_align=8;
  136. break;
  137. case PIX_FMT_YUV410P:
  138. if(s->codec_id == CODEC_ID_SVQ1){
  139. w_align=64;
  140. h_align=64;
  141. }
  142. case PIX_FMT_RGB555:
  143. if(s->codec_id == CODEC_ID_RPZA){
  144. w_align=4;
  145. h_align=4;
  146. }
  147. case PIX_FMT_PAL8:
  148. case PIX_FMT_BGR8:
  149. case PIX_FMT_RGB8:
  150. if(s->codec_id == CODEC_ID_SMC){
  151. w_align=4;
  152. h_align=4;
  153. }
  154. break;
  155. case PIX_FMT_BGR24:
  156. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  157. w_align=4;
  158. h_align=4;
  159. }
  160. break;
  161. default:
  162. w_align= 1;
  163. h_align= 1;
  164. break;
  165. }
  166. *width = FFALIGN(*width , w_align);
  167. *height= FFALIGN(*height, h_align);
  168. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  169. *height+=2; // some of the optimized chroma MC reads one line too much
  170. // which is also done in mpeg decoders with lowres > 0
  171. linesize_align[0] =
  172. linesize_align[1] =
  173. linesize_align[2] =
  174. linesize_align[3] = STRIDE_ALIGN;
  175. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  176. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  177. //picture size unneccessarily in some cases. The solution here is not
  178. //pretty and better ideas are welcome!
  179. #if HAVE_MMX
  180. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  181. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  182. s->codec_id == CODEC_ID_VP6A) {
  183. linesize_align[0] =
  184. linesize_align[1] =
  185. linesize_align[2] = 16;
  186. }
  187. #endif
  188. }
  189. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  190. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  191. int linesize_align[4];
  192. int align;
  193. avcodec_align_dimensions2(s, width, height, linesize_align);
  194. align = FFMAX(linesize_align[0], linesize_align[3]);
  195. linesize_align[1] <<= chroma_shift;
  196. linesize_align[2] <<= chroma_shift;
  197. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  198. *width=FFALIGN(*width, align);
  199. }
  200. #if LIBAVCODEC_VERSION_MAJOR < 53
  201. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  202. return av_image_check_size(w, h, 0, av_log_ctx);
  203. }
  204. #endif
  205. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  206. int i;
  207. int w= s->width;
  208. int h= s->height;
  209. InternalBuffer *buf;
  210. int *picture_number;
  211. if(pic->data[0]!=NULL) {
  212. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  213. return -1;
  214. }
  215. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  216. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  217. return -1;
  218. }
  219. if(av_image_check_size(w, h, 0, s))
  220. return -1;
  221. if(s->internal_buffer==NULL){
  222. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  223. }
  224. #if 0
  225. s->internal_buffer= av_fast_realloc(
  226. s->internal_buffer,
  227. &s->internal_buffer_size,
  228. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  229. );
  230. #endif
  231. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  232. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  233. (*picture_number)++;
  234. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  235. for(i=0; i<4; i++){
  236. av_freep(&buf->base[i]);
  237. buf->data[i]= NULL;
  238. }
  239. }
  240. if(buf->base[0]){
  241. pic->age= *picture_number - buf->last_pic_num;
  242. buf->last_pic_num= *picture_number;
  243. }else{
  244. int h_chroma_shift, v_chroma_shift;
  245. int size[4] = {0};
  246. int tmpsize;
  247. int unaligned;
  248. AVPicture picture;
  249. int stride_align[4];
  250. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  251. avcodec_align_dimensions2(s, &w, &h, stride_align);
  252. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  253. w+= EDGE_WIDTH*2;
  254. h+= EDGE_WIDTH*2;
  255. }
  256. do {
  257. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  258. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  259. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  260. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  261. w += w & ~(w-1);
  262. unaligned = 0;
  263. for (i=0; i<4; i++){
  264. unaligned |= picture.linesize[i] % stride_align[i];
  265. }
  266. } while (unaligned);
  267. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  268. if (tmpsize < 0)
  269. return -1;
  270. for (i=0; i<3 && picture.data[i+1]; i++)
  271. size[i] = picture.data[i+1] - picture.data[i];
  272. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  273. buf->last_pic_num= -256*256*256*64;
  274. memset(buf->base, 0, sizeof(buf->base));
  275. memset(buf->data, 0, sizeof(buf->data));
  276. for(i=0; i<4 && size[i]; i++){
  277. const int h_shift= i==0 ? 0 : h_chroma_shift;
  278. const int v_shift= i==0 ? 0 : v_chroma_shift;
  279. buf->linesize[i]= picture.linesize[i];
  280. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  281. if(buf->base[i]==NULL) return -1;
  282. memset(buf->base[i], 128, size[i]);
  283. // no edge if EDGE EMU or not planar YUV
  284. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  285. buf->data[i] = buf->base[i];
  286. else
  287. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  288. }
  289. if(size[1] && !size[2])
  290. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  291. buf->width = s->width;
  292. buf->height = s->height;
  293. buf->pix_fmt= s->pix_fmt;
  294. pic->age= 256*256*256*64;
  295. }
  296. pic->type= FF_BUFFER_TYPE_INTERNAL;
  297. for(i=0; i<4; i++){
  298. pic->base[i]= buf->base[i];
  299. pic->data[i]= buf->data[i];
  300. pic->linesize[i]= buf->linesize[i];
  301. }
  302. s->internal_buffer_count++;
  303. pic->reordered_opaque= s->reordered_opaque;
  304. if(s->debug&FF_DEBUG_BUFFERS)
  305. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  306. return 0;
  307. }
  308. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  309. int i;
  310. InternalBuffer *buf, *last;
  311. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  312. assert(s->internal_buffer_count);
  313. buf = NULL; /* avoids warning */
  314. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  315. buf= &((InternalBuffer*)s->internal_buffer)[i];
  316. if(buf->data[0] == pic->data[0])
  317. break;
  318. }
  319. assert(i < s->internal_buffer_count);
  320. s->internal_buffer_count--;
  321. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  322. FFSWAP(InternalBuffer, *buf, *last);
  323. for(i=0; i<4; i++){
  324. pic->data[i]=NULL;
  325. // pic->base[i]=NULL;
  326. }
  327. //printf("R%X\n", pic->opaque);
  328. if(s->debug&FF_DEBUG_BUFFERS)
  329. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  330. }
  331. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  332. AVFrame temp_pic;
  333. int i;
  334. /* If no picture return a new buffer */
  335. if(pic->data[0] == NULL) {
  336. /* We will copy from buffer, so must be readable */
  337. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  338. return s->get_buffer(s, pic);
  339. }
  340. /* If internal buffer type return the same buffer */
  341. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  342. pic->reordered_opaque= s->reordered_opaque;
  343. return 0;
  344. }
  345. /*
  346. * Not internal type and reget_buffer not overridden, emulate cr buffer
  347. */
  348. temp_pic = *pic;
  349. for(i = 0; i < 4; i++)
  350. pic->data[i] = pic->base[i] = NULL;
  351. pic->opaque = NULL;
  352. /* Allocate new frame */
  353. if (s->get_buffer(s, pic))
  354. return -1;
  355. /* Copy image data from old buffer to new buffer */
  356. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  357. s->height);
  358. s->release_buffer(s, &temp_pic); // Release old frame
  359. return 0;
  360. }
  361. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  362. int i;
  363. for(i=0; i<count; i++){
  364. int r= func(c, (char*)arg + i*size);
  365. if(ret) ret[i]= r;
  366. }
  367. return 0;
  368. }
  369. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  370. int i;
  371. for(i=0; i<count; i++){
  372. int r= func(c, arg, i, 0);
  373. if(ret) ret[i]= r;
  374. }
  375. return 0;
  376. }
  377. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  378. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  379. ++fmt;
  380. return fmt[0];
  381. }
  382. void avcodec_get_frame_defaults(AVFrame *pic){
  383. memset(pic, 0, sizeof(AVFrame));
  384. pic->pts= AV_NOPTS_VALUE;
  385. pic->key_frame= 1;
  386. }
  387. AVFrame *avcodec_alloc_frame(void){
  388. AVFrame *pic= av_malloc(sizeof(AVFrame));
  389. if(pic==NULL) return NULL;
  390. avcodec_get_frame_defaults(pic);
  391. return pic;
  392. }
  393. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  394. {
  395. int ret= -1;
  396. /* If there is a user-supplied mutex locking routine, call it. */
  397. if (ff_lockmgr_cb) {
  398. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  399. return -1;
  400. }
  401. entangled_thread_counter++;
  402. if(entangled_thread_counter != 1){
  403. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  404. goto end;
  405. }
  406. if(avctx->codec || !codec)
  407. goto end;
  408. if (codec->priv_data_size > 0) {
  409. if(!avctx->priv_data){
  410. avctx->priv_data = av_mallocz(codec->priv_data_size);
  411. if (!avctx->priv_data) {
  412. ret = AVERROR(ENOMEM);
  413. goto end;
  414. }
  415. if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
  416. *(AVClass**)avctx->priv_data= codec->priv_class;
  417. av_opt_set_defaults(avctx->priv_data);
  418. }
  419. }
  420. } else {
  421. avctx->priv_data = NULL;
  422. }
  423. if(avctx->coded_width && avctx->coded_height)
  424. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  425. else if(avctx->width && avctx->height)
  426. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  427. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  428. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  429. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  430. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  431. avcodec_set_dimensions(avctx, 0, 0);
  432. }
  433. #define SANE_NB_CHANNELS 128U
  434. if (avctx->channels > SANE_NB_CHANNELS) {
  435. ret = AVERROR(EINVAL);
  436. goto free_and_end;
  437. }
  438. avctx->codec = codec;
  439. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  440. avctx->codec_id == CODEC_ID_NONE) {
  441. avctx->codec_type = codec->type;
  442. avctx->codec_id = codec->id;
  443. }
  444. if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){
  445. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  446. goto free_and_end;
  447. }
  448. avctx->frame_number = 0;
  449. if (avctx->codec->max_lowres < avctx->lowres) {
  450. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  451. avctx->codec->max_lowres);
  452. goto free_and_end;
  453. }
  454. if(avctx->codec->init){
  455. ret = avctx->codec->init(avctx);
  456. if (ret < 0) {
  457. goto free_and_end;
  458. }
  459. }
  460. ret=0;
  461. end:
  462. entangled_thread_counter--;
  463. /* Release any user-supplied mutex. */
  464. if (ff_lockmgr_cb) {
  465. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  466. }
  467. return ret;
  468. free_and_end:
  469. av_freep(&avctx->priv_data);
  470. avctx->codec= NULL;
  471. goto end;
  472. }
  473. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  474. const short *samples)
  475. {
  476. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  477. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  478. return -1;
  479. }
  480. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  481. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  482. avctx->frame_number++;
  483. return ret;
  484. }else
  485. return 0;
  486. }
  487. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  488. const AVFrame *pict)
  489. {
  490. if(buf_size < FF_MIN_BUFFER_SIZE){
  491. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  492. return -1;
  493. }
  494. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  495. return -1;
  496. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  497. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  498. avctx->frame_number++;
  499. emms_c(); //needed to avoid an emms_c() call before every return;
  500. return ret;
  501. }else
  502. return 0;
  503. }
  504. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  505. const AVSubtitle *sub)
  506. {
  507. int ret;
  508. if(sub->start_display_time) {
  509. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  510. return -1;
  511. }
  512. if(sub->num_rects == 0 || !sub->rects)
  513. return -1;
  514. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  515. avctx->frame_number++;
  516. return ret;
  517. }
  518. #if FF_API_VIDEO_OLD
  519. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  520. int *got_picture_ptr,
  521. const uint8_t *buf, int buf_size)
  522. {
  523. AVPacket avpkt;
  524. av_init_packet(&avpkt);
  525. avpkt.data = buf;
  526. avpkt.size = buf_size;
  527. // HACK for CorePNG to decode as normal PNG by default
  528. avpkt.flags = AV_PKT_FLAG_KEY;
  529. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  530. }
  531. #endif
  532. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  533. int *got_picture_ptr,
  534. AVPacket *avpkt)
  535. {
  536. int ret;
  537. *got_picture_ptr= 0;
  538. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  539. return -1;
  540. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  541. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  542. avpkt);
  543. emms_c(); //needed to avoid an emms_c() call before every return;
  544. if (*got_picture_ptr)
  545. avctx->frame_number++;
  546. }else
  547. ret= 0;
  548. return ret;
  549. }
  550. #if FF_API_AUDIO_OLD
  551. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  552. int *frame_size_ptr,
  553. const uint8_t *buf, int buf_size)
  554. {
  555. AVPacket avpkt;
  556. av_init_packet(&avpkt);
  557. avpkt.data = buf;
  558. avpkt.size = buf_size;
  559. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  560. }
  561. #endif
  562. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  563. int *frame_size_ptr,
  564. AVPacket *avpkt)
  565. {
  566. int ret;
  567. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  568. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  569. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  570. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  571. return -1;
  572. }
  573. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  574. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  575. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  576. return -1;
  577. }
  578. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  579. avctx->frame_number++;
  580. }else{
  581. ret= 0;
  582. *frame_size_ptr=0;
  583. }
  584. return ret;
  585. }
  586. #if FF_API_SUBTITLE_OLD
  587. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  588. int *got_sub_ptr,
  589. const uint8_t *buf, int buf_size)
  590. {
  591. AVPacket avpkt;
  592. av_init_packet(&avpkt);
  593. avpkt.data = buf;
  594. avpkt.size = buf_size;
  595. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  596. }
  597. #endif
  598. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  599. int *got_sub_ptr,
  600. AVPacket *avpkt)
  601. {
  602. int ret;
  603. *got_sub_ptr = 0;
  604. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  605. if (*got_sub_ptr)
  606. avctx->frame_number++;
  607. return ret;
  608. }
  609. void avsubtitle_free(AVSubtitle *sub)
  610. {
  611. int i;
  612. for (i = 0; i < sub->num_rects; i++)
  613. {
  614. av_freep(&sub->rects[i]->pict.data[0]);
  615. av_freep(&sub->rects[i]->pict.data[1]);
  616. av_freep(&sub->rects[i]->pict.data[2]);
  617. av_freep(&sub->rects[i]->pict.data[3]);
  618. av_freep(&sub->rects[i]->text);
  619. av_freep(&sub->rects[i]->ass);
  620. av_freep(&sub->rects[i]);
  621. }
  622. av_freep(&sub->rects);
  623. memset(sub, 0, sizeof(AVSubtitle));
  624. }
  625. av_cold int avcodec_close(AVCodecContext *avctx)
  626. {
  627. /* If there is a user-supplied mutex locking routine, call it. */
  628. if (ff_lockmgr_cb) {
  629. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  630. return -1;
  631. }
  632. entangled_thread_counter++;
  633. if(entangled_thread_counter != 1){
  634. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  635. entangled_thread_counter--;
  636. return -1;
  637. }
  638. if (HAVE_THREADS && avctx->thread_opaque)
  639. avcodec_thread_free(avctx);
  640. if (avctx->codec && avctx->codec->close)
  641. avctx->codec->close(avctx);
  642. avcodec_default_free_buffers(avctx);
  643. avctx->coded_frame = NULL;
  644. av_freep(&avctx->priv_data);
  645. if(avctx->codec && avctx->codec->encode)
  646. av_freep(&avctx->extradata);
  647. avctx->codec = NULL;
  648. entangled_thread_counter--;
  649. /* Release any user-supplied mutex. */
  650. if (ff_lockmgr_cb) {
  651. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  652. }
  653. return 0;
  654. }
  655. AVCodec *avcodec_find_encoder(enum CodecID id)
  656. {
  657. AVCodec *p, *experimental=NULL;
  658. p = first_avcodec;
  659. while (p) {
  660. if (p->encode != NULL && p->id == id) {
  661. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  662. experimental = p;
  663. } else
  664. return p;
  665. }
  666. p = p->next;
  667. }
  668. return experimental;
  669. }
  670. AVCodec *avcodec_find_encoder_by_name(const char *name)
  671. {
  672. AVCodec *p;
  673. if (!name)
  674. return NULL;
  675. p = first_avcodec;
  676. while (p) {
  677. if (p->encode != NULL && strcmp(name,p->name) == 0)
  678. return p;
  679. p = p->next;
  680. }
  681. return NULL;
  682. }
  683. AVCodec *avcodec_find_decoder(enum CodecID id)
  684. {
  685. AVCodec *p;
  686. p = first_avcodec;
  687. while (p) {
  688. if (p->decode != NULL && p->id == id)
  689. return p;
  690. p = p->next;
  691. }
  692. return NULL;
  693. }
  694. AVCodec *avcodec_find_decoder_by_name(const char *name)
  695. {
  696. AVCodec *p;
  697. if (!name)
  698. return NULL;
  699. p = first_avcodec;
  700. while (p) {
  701. if (p->decode != NULL && strcmp(name,p->name) == 0)
  702. return p;
  703. p = p->next;
  704. }
  705. return NULL;
  706. }
  707. static int get_bit_rate(AVCodecContext *ctx)
  708. {
  709. int bit_rate;
  710. int bits_per_sample;
  711. switch(ctx->codec_type) {
  712. case AVMEDIA_TYPE_VIDEO:
  713. case AVMEDIA_TYPE_DATA:
  714. case AVMEDIA_TYPE_SUBTITLE:
  715. case AVMEDIA_TYPE_ATTACHMENT:
  716. bit_rate = ctx->bit_rate;
  717. break;
  718. case AVMEDIA_TYPE_AUDIO:
  719. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  720. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  721. break;
  722. default:
  723. bit_rate = 0;
  724. break;
  725. }
  726. return bit_rate;
  727. }
  728. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  729. {
  730. int i, len, ret = 0;
  731. for (i = 0; i < 4; i++) {
  732. len = snprintf(buf, buf_size,
  733. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  734. buf += len;
  735. buf_size = buf_size > len ? buf_size - len : 0;
  736. ret += len;
  737. codec_tag>>=8;
  738. }
  739. return ret;
  740. }
  741. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  742. {
  743. const char *codec_name;
  744. AVCodec *p;
  745. char buf1[32];
  746. int bitrate;
  747. AVRational display_aspect_ratio;
  748. if (encode)
  749. p = avcodec_find_encoder(enc->codec_id);
  750. else
  751. p = avcodec_find_decoder(enc->codec_id);
  752. if (p) {
  753. codec_name = p->name;
  754. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  755. /* fake mpeg2 transport stream codec (currently not
  756. registered) */
  757. codec_name = "mpeg2ts";
  758. } else if (enc->codec_name[0] != '\0') {
  759. codec_name = enc->codec_name;
  760. } else {
  761. /* output avi tags */
  762. char tag_buf[32];
  763. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  764. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  765. codec_name = buf1;
  766. }
  767. switch(enc->codec_type) {
  768. case AVMEDIA_TYPE_VIDEO:
  769. snprintf(buf, buf_size,
  770. "Video: %s%s",
  771. codec_name, enc->mb_decision ? " (hq)" : "");
  772. if (enc->pix_fmt != PIX_FMT_NONE) {
  773. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  774. ", %s",
  775. avcodec_get_pix_fmt_name(enc->pix_fmt));
  776. }
  777. if (enc->width) {
  778. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  779. ", %dx%d",
  780. enc->width, enc->height);
  781. if (enc->sample_aspect_ratio.num) {
  782. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  783. enc->width*enc->sample_aspect_ratio.num,
  784. enc->height*enc->sample_aspect_ratio.den,
  785. 1024*1024);
  786. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  787. " [PAR %d:%d DAR %d:%d]",
  788. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  789. display_aspect_ratio.num, display_aspect_ratio.den);
  790. }
  791. if(av_log_get_level() >= AV_LOG_DEBUG){
  792. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  793. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  794. ", %d/%d",
  795. enc->time_base.num/g, enc->time_base.den/g);
  796. }
  797. }
  798. if (encode) {
  799. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  800. ", q=%d-%d", enc->qmin, enc->qmax);
  801. }
  802. break;
  803. case AVMEDIA_TYPE_AUDIO:
  804. snprintf(buf, buf_size,
  805. "Audio: %s",
  806. codec_name);
  807. if (enc->sample_rate) {
  808. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  809. ", %d Hz", enc->sample_rate);
  810. }
  811. av_strlcat(buf, ", ", buf_size);
  812. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  813. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  814. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  815. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  816. }
  817. break;
  818. case AVMEDIA_TYPE_DATA:
  819. snprintf(buf, buf_size, "Data: %s", codec_name);
  820. break;
  821. case AVMEDIA_TYPE_SUBTITLE:
  822. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  823. break;
  824. case AVMEDIA_TYPE_ATTACHMENT:
  825. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  826. break;
  827. default:
  828. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  829. return;
  830. }
  831. if (encode) {
  832. if (enc->flags & CODEC_FLAG_PASS1)
  833. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  834. ", pass 1");
  835. if (enc->flags & CODEC_FLAG_PASS2)
  836. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  837. ", pass 2");
  838. }
  839. bitrate = get_bit_rate(enc);
  840. if (bitrate != 0) {
  841. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  842. ", %d kb/s", bitrate / 1000);
  843. }
  844. }
  845. unsigned avcodec_version( void )
  846. {
  847. return LIBAVCODEC_VERSION_INT;
  848. }
  849. const char *avcodec_configuration(void)
  850. {
  851. return FFMPEG_CONFIGURATION;
  852. }
  853. const char *avcodec_license(void)
  854. {
  855. #define LICENSE_PREFIX "libavcodec license: "
  856. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  857. }
  858. void avcodec_init(void)
  859. {
  860. static int initialized = 0;
  861. if (initialized != 0)
  862. return;
  863. initialized = 1;
  864. dsputil_static_init();
  865. }
  866. void avcodec_flush_buffers(AVCodecContext *avctx)
  867. {
  868. if(avctx->codec->flush)
  869. avctx->codec->flush(avctx);
  870. }
  871. void avcodec_default_free_buffers(AVCodecContext *s){
  872. int i, j;
  873. if(s->internal_buffer==NULL) return;
  874. if (s->internal_buffer_count)
  875. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  876. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  877. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  878. for(j=0; j<4; j++){
  879. av_freep(&buf->base[j]);
  880. buf->data[j]= NULL;
  881. }
  882. }
  883. av_freep(&s->internal_buffer);
  884. s->internal_buffer_count=0;
  885. }
  886. char av_get_pict_type_char(int pict_type){
  887. switch(pict_type){
  888. case FF_I_TYPE: return 'I';
  889. case FF_P_TYPE: return 'P';
  890. case FF_B_TYPE: return 'B';
  891. case FF_S_TYPE: return 'S';
  892. case FF_SI_TYPE:return 'i';
  893. case FF_SP_TYPE:return 'p';
  894. case FF_BI_TYPE:return 'b';
  895. default: return '?';
  896. }
  897. }
  898. int av_get_bits_per_sample(enum CodecID codec_id){
  899. switch(codec_id){
  900. case CODEC_ID_ADPCM_SBPRO_2:
  901. return 2;
  902. case CODEC_ID_ADPCM_SBPRO_3:
  903. return 3;
  904. case CODEC_ID_ADPCM_SBPRO_4:
  905. case CODEC_ID_ADPCM_CT:
  906. case CODEC_ID_ADPCM_IMA_WAV:
  907. case CODEC_ID_ADPCM_MS:
  908. case CODEC_ID_ADPCM_YAMAHA:
  909. return 4;
  910. case CODEC_ID_PCM_ALAW:
  911. case CODEC_ID_PCM_MULAW:
  912. case CODEC_ID_PCM_S8:
  913. case CODEC_ID_PCM_U8:
  914. case CODEC_ID_PCM_ZORK:
  915. return 8;
  916. case CODEC_ID_PCM_S16BE:
  917. case CODEC_ID_PCM_S16LE:
  918. case CODEC_ID_PCM_S16LE_PLANAR:
  919. case CODEC_ID_PCM_U16BE:
  920. case CODEC_ID_PCM_U16LE:
  921. return 16;
  922. case CODEC_ID_PCM_S24DAUD:
  923. case CODEC_ID_PCM_S24BE:
  924. case CODEC_ID_PCM_S24LE:
  925. case CODEC_ID_PCM_U24BE:
  926. case CODEC_ID_PCM_U24LE:
  927. return 24;
  928. case CODEC_ID_PCM_S32BE:
  929. case CODEC_ID_PCM_S32LE:
  930. case CODEC_ID_PCM_U32BE:
  931. case CODEC_ID_PCM_U32LE:
  932. case CODEC_ID_PCM_F32BE:
  933. case CODEC_ID_PCM_F32LE:
  934. return 32;
  935. case CODEC_ID_PCM_F64BE:
  936. case CODEC_ID_PCM_F64LE:
  937. return 64;
  938. default:
  939. return 0;
  940. }
  941. }
  942. #if FF_API_OLD_SAMPLE_FMT
  943. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  944. return av_get_bits_per_sample_fmt(sample_fmt);
  945. }
  946. #endif
  947. #if !HAVE_THREADS
  948. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  949. s->thread_count = thread_count;
  950. return -1;
  951. }
  952. #endif
  953. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  954. {
  955. unsigned int n = 0;
  956. while(v >= 0xff) {
  957. *s++ = 0xff;
  958. v -= 0xff;
  959. n++;
  960. }
  961. *s = v;
  962. n++;
  963. return n;
  964. }
  965. #if LIBAVCODEC_VERSION_MAJOR < 53
  966. #include "libavcore/parseutils.h"
  967. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  968. {
  969. return av_parse_video_size(width_ptr, height_ptr, str);
  970. }
  971. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  972. {
  973. return av_parse_video_rate(frame_rate, arg);
  974. }
  975. #endif
  976. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  977. int i;
  978. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  979. return i;
  980. }
  981. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  982. {
  983. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  984. "version to the newest one from SVN. If the problem still "
  985. "occurs, it means that your file has a feature which has not "
  986. "been implemented.", feature);
  987. if(want_sample)
  988. av_log_ask_for_sample(avc, NULL);
  989. else
  990. av_log(avc, AV_LOG_WARNING, "\n");
  991. }
  992. void av_log_ask_for_sample(void *avc, const char *msg)
  993. {
  994. if (msg)
  995. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  996. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  997. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  998. "and contact the ffmpeg-devel mailing list.\n");
  999. }
  1000. static AVHWAccel *first_hwaccel = NULL;
  1001. void av_register_hwaccel(AVHWAccel *hwaccel)
  1002. {
  1003. AVHWAccel **p = &first_hwaccel;
  1004. while (*p)
  1005. p = &(*p)->next;
  1006. *p = hwaccel;
  1007. hwaccel->next = NULL;
  1008. }
  1009. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1010. {
  1011. return hwaccel ? hwaccel->next : first_hwaccel;
  1012. }
  1013. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1014. {
  1015. AVHWAccel *hwaccel=NULL;
  1016. while((hwaccel= av_hwaccel_next(hwaccel))){
  1017. if ( hwaccel->id == codec_id
  1018. && hwaccel->pix_fmt == pix_fmt)
  1019. return hwaccel;
  1020. }
  1021. return NULL;
  1022. }
  1023. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1024. {
  1025. if (ff_lockmgr_cb) {
  1026. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1027. return -1;
  1028. }
  1029. ff_lockmgr_cb = cb;
  1030. if (ff_lockmgr_cb) {
  1031. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1032. return -1;
  1033. }
  1034. return 0;
  1035. }
  1036. unsigned int ff_toupper4(unsigned int x)
  1037. {
  1038. return toupper( x &0xFF)
  1039. + (toupper((x>>8 )&0xFF)<<8 )
  1040. + (toupper((x>>16)&0xFF)<<16)
  1041. + (toupper((x>>24)&0xFF)<<24);
  1042. }