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.

613 lines
16KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(unsigned int size)
  23. {
  24. void *ptr;
  25. ptr = av_malloc(size);
  26. if (!ptr)
  27. return NULL;
  28. memset(ptr, 0, size);
  29. return ptr;
  30. }
  31. char *av_strdup(const char *s)
  32. {
  33. char *ptr;
  34. int len;
  35. len = strlen(s) + 1;
  36. ptr = av_malloc(len);
  37. if (!ptr)
  38. return NULL;
  39. memcpy(ptr, s, len);
  40. return ptr;
  41. }
  42. /**
  43. * realloc which does nothing if the block is large enough
  44. */
  45. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  46. {
  47. if(min_size < *size)
  48. return ptr;
  49. *size= min_size + 10*1024;
  50. return av_realloc(ptr, *size);
  51. }
  52. /* allocation of static arrays - do not use for normal allocation */
  53. static unsigned int last_static = 0;
  54. static char*** array_static = NULL;
  55. static const unsigned int grow_static = 64; // ^2
  56. void *__av_mallocz_static(void** location, unsigned int size)
  57. {
  58. unsigned int l = (last_static + grow_static) & ~(grow_static - 1);
  59. void *ptr = av_mallocz(size);
  60. if (!ptr)
  61. return NULL;
  62. if (location)
  63. {
  64. if (l > last_static)
  65. array_static = av_realloc(array_static, l);
  66. array_static[last_static++] = (char**) location;
  67. *location = ptr;
  68. }
  69. return ptr;
  70. }
  71. /* free all static arrays and reset pointers to 0 */
  72. void av_free_static()
  73. {
  74. if (array_static)
  75. {
  76. unsigned i;
  77. for (i = 0; i < last_static; i++)
  78. {
  79. av_free(*array_static[i]);
  80. *array_static[i] = NULL;
  81. }
  82. av_free(array_static);
  83. array_static = 0;
  84. }
  85. last_static = 0;
  86. }
  87. /* cannot call it directly because of 'void **' casting is not automatic */
  88. void __av_freep(void **ptr)
  89. {
  90. av_free(*ptr);
  91. *ptr = NULL;
  92. }
  93. /* encoder management */
  94. AVCodec *first_avcodec;
  95. void register_avcodec(AVCodec *format)
  96. {
  97. AVCodec **p;
  98. p = &first_avcodec;
  99. while (*p != NULL) p = &(*p)->next;
  100. *p = format;
  101. format->next = NULL;
  102. }
  103. typedef struct DefaultPicOpaque{
  104. int last_pic_num;
  105. uint8_t *data[4];
  106. }DefaultPicOpaque;
  107. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  108. int i;
  109. const int width = s->width;
  110. const int height= s->height;
  111. DefaultPicOpaque *opaque;
  112. assert(pic->data[0]==NULL);
  113. assert(pic->type==0 || pic->type==FF_TYPE_INTERNAL);
  114. if(pic->opaque){
  115. opaque= (DefaultPicOpaque *)pic->opaque;
  116. for(i=0; i<3; i++)
  117. pic->data[i]= opaque->data[i];
  118. // printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num);
  119. pic->age= pic->coded_picture_number - opaque->last_pic_num;
  120. opaque->last_pic_num= pic->coded_picture_number;
  121. //printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number);
  122. }else{
  123. int align, h_chroma_shift, v_chroma_shift;
  124. int w, h, pixel_size;
  125. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  126. switch(s->pix_fmt){
  127. case PIX_FMT_YUV422:
  128. pixel_size=2;
  129. break;
  130. case PIX_FMT_RGB24:
  131. case PIX_FMT_BGR24:
  132. pixel_size=3;
  133. break;
  134. case PIX_FMT_RGBA32:
  135. pixel_size=4;
  136. break;
  137. default:
  138. pixel_size=1;
  139. }
  140. if(s->codec_id==CODEC_ID_SVQ1) align=63;
  141. else align=15;
  142. w= (width +align)&~align;
  143. h= (height+align)&~align;
  144. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  145. w+= EDGE_WIDTH*2;
  146. h+= EDGE_WIDTH*2;
  147. }
  148. opaque= av_mallocz(sizeof(DefaultPicOpaque));
  149. if(opaque==NULL) return -1;
  150. pic->opaque= opaque;
  151. opaque->last_pic_num= -256*256*256*64;
  152. for(i=0; i<3; i++){
  153. int h_shift= i==0 ? 0 : h_chroma_shift;
  154. int v_shift= i==0 ? 0 : v_chroma_shift;
  155. pic->linesize[i]= pixel_size*w>>h_shift;
  156. pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
  157. if(pic->base[i]==NULL) return -1;
  158. memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
  159. if(s->flags&CODEC_FLAG_EMU_EDGE)
  160. pic->data[i] = pic->base[i] + 16; //FIXME 16
  161. else
  162. pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16
  163. opaque->data[i]= pic->data[i];
  164. }
  165. pic->age= 256*256*256*64;
  166. pic->type= FF_BUFFER_TYPE_INTERNAL;
  167. }
  168. return 0;
  169. }
  170. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  171. int i;
  172. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  173. for(i=0; i<3; i++)
  174. pic->data[i]=NULL;
  175. //printf("R%X\n", pic->opaque);
  176. }
  177. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
  178. return fmt[0];
  179. }
  180. void avcodec_get_context_defaults(AVCodecContext *s){
  181. s->bit_rate= 800*1000;
  182. s->bit_rate_tolerance= s->bit_rate*10;
  183. s->qmin= 2;
  184. s->qmax= 31;
  185. s->mb_qmin= 2;
  186. s->mb_qmax= 31;
  187. s->rc_eq= "tex^qComp";
  188. s->qcompress= 0.5;
  189. s->max_qdiff= 3;
  190. s->b_quant_factor=1.25;
  191. s->b_quant_offset=1.25;
  192. s->i_quant_factor=-0.8;
  193. s->i_quant_offset=0.0;
  194. s->error_concealment= 3;
  195. s->error_resilience= 1;
  196. s->workaround_bugs= FF_BUG_AUTODETECT;
  197. s->frame_rate = 25 * FRAME_RATE_BASE;
  198. s->gop_size= 50;
  199. s->me_method= ME_EPZS;
  200. s->get_buffer= avcodec_default_get_buffer;
  201. s->release_buffer= avcodec_default_release_buffer;
  202. s->get_format= avcodec_default_get_format;
  203. s->me_subpel_quality=8;
  204. }
  205. /**
  206. * allocates a AVCodecContext and set it to defaults.
  207. * this can be deallocated by simply calling free()
  208. */
  209. AVCodecContext *avcodec_alloc_context(void){
  210. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  211. if(avctx==NULL) return NULL;
  212. avcodec_get_context_defaults(avctx);
  213. return avctx;
  214. }
  215. /**
  216. * allocates a AVPFrame and set it to defaults.
  217. * this can be deallocated by simply calling free()
  218. */
  219. AVFrame *avcodec_alloc_frame(void){
  220. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  221. return pic;
  222. }
  223. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  224. {
  225. int ret;
  226. avctx->codec = codec;
  227. avctx->codec_id = codec->id;
  228. avctx->frame_number = 0;
  229. if (codec->priv_data_size > 0) {
  230. avctx->priv_data = av_mallocz(codec->priv_data_size);
  231. if (!avctx->priv_data)
  232. return -ENOMEM;
  233. } else {
  234. avctx->priv_data = NULL;
  235. }
  236. ret = avctx->codec->init(avctx);
  237. if (ret < 0) {
  238. av_freep(&avctx->priv_data);
  239. return ret;
  240. }
  241. return 0;
  242. }
  243. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  244. const short *samples)
  245. {
  246. int ret;
  247. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  248. avctx->frame_number++;
  249. return ret;
  250. }
  251. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  252. const AVFrame *pict)
  253. {
  254. int ret;
  255. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  256. emms_c(); //needed to avoid a emms_c() call before every return;
  257. avctx->frame_number++;
  258. return ret;
  259. }
  260. /* decode a frame. return -1 if error, otherwise return the number of
  261. bytes used. If no frame could be decompressed, *got_picture_ptr is
  262. zero. Otherwise, it is non zero */
  263. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  264. int *got_picture_ptr,
  265. uint8_t *buf, int buf_size)
  266. {
  267. int ret;
  268. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  269. buf, buf_size);
  270. emms_c(); //needed to avoid a emms_c() call before every return;
  271. if (*got_picture_ptr)
  272. avctx->frame_number++;
  273. return ret;
  274. }
  275. /* decode an audio frame. return -1 if error, otherwise return the
  276. *number of bytes used. If no frame could be decompressed,
  277. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  278. *size in BYTES. */
  279. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  280. int *frame_size_ptr,
  281. uint8_t *buf, int buf_size)
  282. {
  283. int ret;
  284. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  285. buf, buf_size);
  286. avctx->frame_number++;
  287. return ret;
  288. }
  289. int avcodec_close(AVCodecContext *avctx)
  290. {
  291. if (avctx->codec->close)
  292. avctx->codec->close(avctx);
  293. av_freep(&avctx->priv_data);
  294. avctx->codec = NULL;
  295. return 0;
  296. }
  297. AVCodec *avcodec_find_encoder(enum CodecID id)
  298. {
  299. AVCodec *p;
  300. p = first_avcodec;
  301. while (p) {
  302. if (p->encode != NULL && p->id == id)
  303. return p;
  304. p = p->next;
  305. }
  306. return NULL;
  307. }
  308. AVCodec *avcodec_find_encoder_by_name(const char *name)
  309. {
  310. AVCodec *p;
  311. p = first_avcodec;
  312. while (p) {
  313. if (p->encode != NULL && strcmp(name,p->name) == 0)
  314. return p;
  315. p = p->next;
  316. }
  317. return NULL;
  318. }
  319. AVCodec *avcodec_find_decoder(enum CodecID id)
  320. {
  321. AVCodec *p;
  322. p = first_avcodec;
  323. while (p) {
  324. if (p->decode != NULL && p->id == id)
  325. return p;
  326. p = p->next;
  327. }
  328. return NULL;
  329. }
  330. AVCodec *avcodec_find_decoder_by_name(const char *name)
  331. {
  332. AVCodec *p;
  333. p = first_avcodec;
  334. while (p) {
  335. if (p->decode != NULL && strcmp(name,p->name) == 0)
  336. return p;
  337. p = p->next;
  338. }
  339. return NULL;
  340. }
  341. AVCodec *avcodec_find(enum CodecID id)
  342. {
  343. AVCodec *p;
  344. p = first_avcodec;
  345. while (p) {
  346. if (p->id == id)
  347. return p;
  348. p = p->next;
  349. }
  350. return NULL;
  351. }
  352. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  353. {
  354. const char *codec_name;
  355. AVCodec *p;
  356. char buf1[32];
  357. char channels_str[100];
  358. int bitrate;
  359. if (encode)
  360. p = avcodec_find_encoder(enc->codec_id);
  361. else
  362. p = avcodec_find_decoder(enc->codec_id);
  363. if (p) {
  364. codec_name = p->name;
  365. } else if (enc->codec_name[0] != '\0') {
  366. codec_name = enc->codec_name;
  367. } else {
  368. /* output avi tags */
  369. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  370. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  371. enc->codec_tag & 0xff,
  372. (enc->codec_tag >> 8) & 0xff,
  373. (enc->codec_tag >> 16) & 0xff,
  374. (enc->codec_tag >> 24) & 0xff);
  375. } else {
  376. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  377. }
  378. codec_name = buf1;
  379. }
  380. switch(enc->codec_type) {
  381. case CODEC_TYPE_VIDEO:
  382. snprintf(buf, buf_size,
  383. "Video: %s%s",
  384. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  385. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  386. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  387. ", %s",
  388. avcodec_get_pix_fmt_name(enc->pix_fmt));
  389. }
  390. if (enc->width) {
  391. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  392. ", %dx%d, %0.2f fps",
  393. enc->width, enc->height,
  394. (float)enc->frame_rate / FRAME_RATE_BASE);
  395. }
  396. if (encode) {
  397. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  398. ", q=%d-%d", enc->qmin, enc->qmax);
  399. }
  400. bitrate = enc->bit_rate;
  401. break;
  402. case CODEC_TYPE_AUDIO:
  403. snprintf(buf, buf_size,
  404. "Audio: %s",
  405. codec_name);
  406. switch (enc->channels) {
  407. case 1:
  408. strcpy(channels_str, "mono");
  409. break;
  410. case 2:
  411. strcpy(channels_str, "stereo");
  412. break;
  413. case 6:
  414. strcpy(channels_str, "5:1");
  415. break;
  416. default:
  417. sprintf(channels_str, "%d channels", enc->channels);
  418. break;
  419. }
  420. if (enc->sample_rate) {
  421. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  422. ", %d Hz, %s",
  423. enc->sample_rate,
  424. channels_str);
  425. }
  426. /* for PCM codecs, compute bitrate directly */
  427. switch(enc->codec_id) {
  428. case CODEC_ID_PCM_S16LE:
  429. case CODEC_ID_PCM_S16BE:
  430. case CODEC_ID_PCM_U16LE:
  431. case CODEC_ID_PCM_U16BE:
  432. bitrate = enc->sample_rate * enc->channels * 16;
  433. break;
  434. case CODEC_ID_PCM_S8:
  435. case CODEC_ID_PCM_U8:
  436. case CODEC_ID_PCM_ALAW:
  437. case CODEC_ID_PCM_MULAW:
  438. bitrate = enc->sample_rate * enc->channels * 8;
  439. break;
  440. default:
  441. bitrate = enc->bit_rate;
  442. break;
  443. }
  444. break;
  445. default:
  446. av_abort();
  447. }
  448. if (encode) {
  449. if (enc->flags & CODEC_FLAG_PASS1)
  450. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  451. ", pass 1");
  452. if (enc->flags & CODEC_FLAG_PASS2)
  453. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  454. ", pass 2");
  455. }
  456. if (bitrate != 0) {
  457. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  458. ", %d kb/s", bitrate / 1000);
  459. }
  460. }
  461. unsigned avcodec_version( void )
  462. {
  463. return LIBAVCODEC_VERSION_INT;
  464. }
  465. unsigned avcodec_build( void )
  466. {
  467. return LIBAVCODEC_BUILD;
  468. }
  469. /* must be called before any other functions */
  470. void avcodec_init(void)
  471. {
  472. static int inited = 0;
  473. if (inited != 0)
  474. return;
  475. inited = 1;
  476. //dsputil_init();
  477. }
  478. /* this can be called after seeking and before trying to decode the next keyframe */
  479. void avcodec_flush_buffers(AVCodecContext *avctx)
  480. {
  481. int i;
  482. MpegEncContext *s = avctx->priv_data;
  483. switch(avctx->codec_id){
  484. case CODEC_ID_MPEG1VIDEO:
  485. case CODEC_ID_H263:
  486. case CODEC_ID_RV10:
  487. case CODEC_ID_MJPEG:
  488. case CODEC_ID_MJPEGB:
  489. case CODEC_ID_MPEG4:
  490. case CODEC_ID_MSMPEG4V1:
  491. case CODEC_ID_MSMPEG4V2:
  492. case CODEC_ID_MSMPEG4V3:
  493. case CODEC_ID_WMV1:
  494. case CODEC_ID_WMV2:
  495. case CODEC_ID_H263P:
  496. case CODEC_ID_H263I:
  497. case CODEC_ID_SVQ1:
  498. for(i=0; i<MAX_PICTURE_COUNT; i++){
  499. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  500. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  501. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  502. }
  503. s->last_picture.data[0] = s->next_picture.data[0] = NULL;
  504. break;
  505. default:
  506. //FIXME
  507. break;
  508. }
  509. }
  510. static int raw_encode_init(AVCodecContext *s)
  511. {
  512. return 0;
  513. }
  514. static int raw_decode_frame(AVCodecContext *avctx,
  515. void *data, int *data_size,
  516. uint8_t *buf, int buf_size)
  517. {
  518. return -1;
  519. }
  520. static int raw_encode_frame(AVCodecContext *avctx,
  521. unsigned char *frame, int buf_size, void *data)
  522. {
  523. return -1;
  524. }
  525. AVCodec rawvideo_codec = {
  526. "rawvideo",
  527. CODEC_TYPE_VIDEO,
  528. CODEC_ID_RAWVIDEO,
  529. 0,
  530. raw_encode_init,
  531. raw_encode_frame,
  532. NULL,
  533. raw_decode_frame,
  534. };