annotate libmpcodecs/vd_ffmpeg.c @ 5304:6175c9cfab11

Support for decoder specific config from mp4 header for AAC decoder.
author atmos4
date Sun, 24 Mar 2002 03:08:20 +0000
parents ea8f3e8f39c1
children 6bab0f8a75e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
1 #include <stdio.h>
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
2 #include <stdlib.h>
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
3 #include <assert.h>
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
4
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
5 #include "config.h"
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
6 #include "mp_msg.h"
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
7 #include "help_mp.h"
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
8
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
9 #ifdef USE_LIBAVCODEC
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
10
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
11 #include "bswap.h"
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
12
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
13 #include "vd_internal.h"
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
14
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
15 static vd_info_t info = {
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
16 "FFmpeg's libavcodec codec family",
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
17 "ffmpeg",
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
18 VFM_FFMPEG,
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
19 "A'rpi",
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
20 "http://ffmpeg.sf.net",
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
21 "native codecs"
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
22 };
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
23
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
24 LIBVD_EXTERN(ffmpeg)
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
25
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
26 #ifdef USE_LIBAVCODEC_SO
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
27 #include <libffmpeg/avcodec.h>
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
28 #else
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
29 #include "libavcodec/avcodec.h"
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
30 #endif
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
31
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
32 int avcodec_inited=0;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
33
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
34 typedef struct {
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
35 AVCodec *lavc_codec;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
36 AVCodecContext *avctx;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
37 int last_aspect;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
38 } vd_ffmpeg_ctx;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
39
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
40 //#ifdef FF_POSTPROCESS
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
41 //unsigned int lavc_pp=0;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
42 //#endif
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
43
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
44 // to set/get/query special features/parameters
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
45 static int control(sh_video_t *sh,int cmd,void* arg,...){
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
46 return CONTROL_UNKNOWN;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
47 }
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
48
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
49 // init driver
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
50 static int init(sh_video_t *sh){
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
51 AVCodecContext *avctx;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
52 vd_ffmpeg_ctx *ctx;
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
53
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
54 if(!avcodec_inited){
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
55 avcodec_init();
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
56 avcodec_register_all();
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
57 avcodec_inited=1;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
58 }
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
59
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
60 ctx = sh->context = malloc(sizeof(vd_ffmpeg_ctx));
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
61 if (!ctx)
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
62 return(0);
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
63 memset(ctx, 0, sizeof(vd_ffmpeg_ctx));
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
64
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
65 ctx->lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh->codec->dll);
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
66 if(!ctx->lavc_codec){
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
67 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MissingLAVCcodec,sh->codec->dll);
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
68 return 0;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
69 }
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
70
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
71 ctx->avctx = malloc(sizeof(AVCodecContext));
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
72 memset(ctx->avctx, 0, sizeof(AVCodecContext));
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
73 avctx = ctx->avctx;
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
74
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
75 avctx->width = sh->disp_w;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
76 avctx->height= sh->disp_h;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
77 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"libavcodec.size: %d x %d\n",avctx->width,avctx->height);
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
78 if (sh->format == mmioFOURCC('R', 'V', '1', '3'))
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
79 avctx->sub_id = 3;
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
80 /* open it */
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
81 if (avcodec_open(avctx, ctx->lavc_codec) < 0) {
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
82 mp_msg(MSGT_DECVIDEO,MSGL_ERR, MSGTR_CantOpenCodec);
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
83 return 0;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
84 }
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
85 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: libavcodec init OK!\n");
5124
3dcbf67c0de0 handle error from mpcodecs_config_vo()
arpi
parents: 4986
diff changeset
86 return mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12);
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
87 }
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
88
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
89 // uninit driver
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
90 static void uninit(sh_video_t *sh){
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
91 vd_ffmpeg_ctx *ctx = sh->context;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
92 AVCodecContext *avctx = ctx->avctx;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
93
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
94 if (avcodec_close(avctx) < 0)
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
95 mp_msg(MSGT_DECVIDEO,MSGL_ERR, MSGTR_CantCloseCodec);
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
96 if (avctx)
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
97 free(avctx);
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
98 if (ctx)
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
99 free(ctx);
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
100 }
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
101
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
102 //mp_image_t* mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag, int w, int h);
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
103
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
104 // decode a frame
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
105 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
106 int got_picture=0;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
107 int ret;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
108 AVPicture lavc_picture;
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
109 vd_ffmpeg_ctx *ctx = sh->context;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
110 AVCodecContext *avctx = ctx->avctx;
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
111 mp_image_t* mpi;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
112
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
113 if(len<=0) return NULL; // skipped frame
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
114
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
115 ret = avcodec_decode_video(avctx, &lavc_picture,
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
116 &got_picture, data, len);
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
117
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
118 if(ret<0) mp_msg(MSGT_DECVIDEO,MSGL_WARN, "Error while decoding frame!\n");
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
119 if(!got_picture) return NULL; // skipped image
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
120
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
121 if (avctx->aspect_ratio_info != ctx->last_aspect)
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
122 {
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
123 ctx->last_aspect = avctx->aspect_ratio_info;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
124 switch(avctx->aspect_ratio_info)
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
125 {
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
126 case FF_ASPECT_4_3_625:
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
127 case FF_ASPECT_4_3_525:
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
128 sh->aspect = 4.0/3.0;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
129 break;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
130 case FF_ASPECT_16_9_625:
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
131 case FF_ASPECT_16_9_525:
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
132 sh->aspect = 16.0/9.0;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
133 break;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
134 case FF_ASPECT_SQUARE:
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
135 sh->aspect = 1.0/1.0;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
136 break;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
137 default:
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
138 sh->aspect = 0.0;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
139 break;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
140 }
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
141 if (mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12))
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
142 return NULL;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
143 }
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
144
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
145 if ((avctx->width != sh->disp_w) ||
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
146 (avctx->height != sh->disp_h))
5207
d337cc4ab0ee config vo if resolution changed (after decoded image read the dimensions out of lavc context)
alex
parents: 5124
diff changeset
147 {
5219
alex
parents: 5207
diff changeset
148 /* and what about the sh->bih (BitmapInfoHeader) width/height values? */
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
149 sh->disp_w = avctx->width;
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
150 sh->disp_h = avctx->height;
5207
d337cc4ab0ee config vo if resolution changed (after decoded image read the dimensions out of lavc context)
alex
parents: 5124
diff changeset
151 if (mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12))
d337cc4ab0ee config vo if resolution changed (after decoded image read the dimensions out of lavc context)
alex
parents: 5124
diff changeset
152 return NULL;
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
153 }
5207
d337cc4ab0ee config vo if resolution changed (after decoded image read the dimensions out of lavc context)
alex
parents: 5124
diff changeset
154
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
155 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, MP_IMGFLAG_PRESERVE,
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
156 avctx->width, avctx->height);
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
157 if(!mpi){ // temporary!
5207
d337cc4ab0ee config vo if resolution changed (after decoded image read the dimensions out of lavc context)
alex
parents: 5124
diff changeset
158 printf("couldn't allocate image for codec\n");
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
159 return NULL;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
160 }
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
161
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
162 mpi->planes[0]=lavc_picture.data[0];
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
163 mpi->planes[1]=lavc_picture.data[1];
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
164 mpi->planes[2]=lavc_picture.data[2];
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
165 mpi->stride[0]=lavc_picture.linesize[0];
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
166 mpi->stride[1]=lavc_picture.linesize[1];
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
167 mpi->stride[2]=lavc_picture.linesize[2];
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
168
5280
ea8f3e8f39c1 added aspect ratio support and local ctx
alex
parents: 5219
diff changeset
169 if(avctx->pix_fmt==PIX_FMT_YUV422P){
4952
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
170 mpi->stride[1]*=2;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
171 mpi->stride[2]*=2;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
172 }
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
173
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
174 return mpi;
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
175 }
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
176
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
177 #endif
ffeba1050226 vd_ffmpeg added
arpi
parents:
diff changeset
178