Error: ‘AVStream’ {aka ‘struct AVStream’} has no member named ‘codec’

Hi everyone,
unfortunately I’m having trouble building OpenCV successfully. I am trying to run a code that uses #if defined(HAVE_OPENCV_CUDACODEC). Therefore, I am trying to install OpenCV2 with the extra modules. I am using gcc-8 in order to circumvent another error message I received. The disable-ffmpeg-versions -disable-ffmpeg-versions=OFF was a try to solve the error message in the topic.
Thank you for helping me out :slightly_smiling_face:

Configuration:
$ cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-master/modules -DCMAKE_C_COMPILER=$(which gcc-8) -DCMAKE_CXX_COMPILER=$(which g++-8) -disable-ffmpeg-versions=OFF -DWITH_CUDA=ON ../opencv-master

Error message:

[…]
– Video I/O:
– DC1394: YES (2.2.5)
– FFMPEG: YES
– avcodec: YES (59.1.100)
– avformat: YES (59.2.101)
– avutil: YES (57.0.100)
– swscale: YES (6.0.100)
– avresample: YES (4.0.0)
– GStreamer: YES (1.16.2)
– v4l/v4l2: YES (linux/videodev2.h)
[…]
– Configuring done
– Generating done
– Build files have been written to: /home/user/Downloads/build

$ cmake --build .
[ 1%] Built target libopenjp2
[ 1%] Built target opencv_videoio_plugins
[ 3%] Built target ippiw
[ 3%] Built target opencv_cudev
[ 3%] Built target ittnotify
[ 8%] Built target opencv_core
[ 12%] Built target opencv_imgproc
[ 17%] Built target libwebp
[ 18%] Built target opencv_imgcodecs
[ 18%] Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o
In file included from /home/user/Downloads/opencv-master/modules/videoio/src/cap_ffmpeg.cpp:50:
/home/user/Downloads/opencv-master/modules/videoio/src/cap_ffmpeg_impl.hpp: In member function ‘void CvCapture_FFMPEG::close()’:
/home/user/Downloads/opencv-master/modules/videoio/src/cap_ffmpeg_impl.hpp:606:34: error: ‘AVStream’ {aka ‘struct AVStream’} has no member named ‘codec’
avcodec_close( video_st->codec );
_________________________^~~~~
/home/user/Downloads/opencv-master/modules/videoio/src/cap_ffmpeg_impl.hpp: At global scope:
/home/user/Downloads/opencv-master/modules/videoio/src/cap_ffmpeg_impl.hpp:789:39: error: ‘AVLockOp’ has not been declared
static int LockCallBack(void **mutex, AVLockOp op)
_____________________________________ ^~~~~~~~
[…]

Sidenote: I have ffmpeg installed and configured as described here.

$ ffmpeg
ffmpeg version N-102605-g4c705a2775 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 9 (Ubuntu 9.3.0-17ubuntu1~20.04)
configuration: --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
libavutil 57. 0.100 / 57. 0.100
libavcodec 59. 1.100 / 59. 1.100
libavformat 59. 2.101 / 59. 2.101
libavdevice 59. 0.100 / 59. 0.100
libavfilter 8. 0.101 / 8. 0.101
libswscale 6. 0.100 / 6. 0.100
libswresample 4. 0.100 / 4. 0.100
Hyper fast Audio and Video encoder

1 Like

looks like your available ffmpeg is “too new” and they (deprecated and) removed the codec member from AVStream recently, in favor of codecpar.

what version of OpenCV did you do this with? I’m assuming latest.

OpenCV would need a fix for that. feel free to open an issue on OpenCV’s github. don’t worry too much about all the things they ask for in the issue template.

for now you could use an older version of ffmpeg (its libraries). I think that should work.

the deprecated field is still available if #define FF_API_LAVF_AVCTX (LIBAVFORMAT_VERSION_MAJOR < 59) so you could choose your ffmpeg based on that information.

I see that you’ve opened an issue. I did as well:

Hi Crackwitz,
thank you for your prompt reply. I just closed the issue after I noticed yours.
Yes, you are correct. I was following this guide, which suggest to use this git.
Thanks for your help!

I dug around, LIBAVFORMAT_VERSION_MAJOR is 58 even for ffmpeg v4.4, but it’s 59 in the master branch.

I’d suggest using 4.4 or earlier, rather than master.

Ok, I’ll give that a try now. I’ll have to find it first though, as the launchpad is highly confusing to me. I’ll let you know how it goes.

I think nobody likes launchpad.

their official git is at git.ffmpeg.org Git - ffmpeg.git/summary or you can get the source from their mirror on github

Good idea!

I went ahead and did a
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/
then, I found the n4.4 in the repository here and clicked on the commit. Then, I did a checkout of that specific commit as follows:
git checkout dc91b913b6260e85e1304c74ff7bb3c22a8c9fb1

I guess we could call it progress. Now, I have a new error:

[ 1%] Built target libopenjp2
[ 1%] Built target opencv_videoio_plugins
[ 3%] Built target ippiw
[ 3%] Built target opencv_cudev
[ 3%] Built target ittnotify
[ 3%] Linking CXX shared library …/…/lib/libopencv_core.so
[ 8%] Built target opencv_core
[ 8%] Linking CXX shared library …/…/lib/libopencv_imgproc.so
[ 12%] Built target opencv_imgproc
[ 17%] Built target libwebp
[ 17%] Linking CXX shared library …/…/lib/libopencv_imgcodecs.so
[ 18%] Built target opencv_imgcodecs
[ 18%] Linking CXX shared library …/…/lib/libopencv_videoio.so
/usr/bin/ld: /usr/local/lib/libavcodec.a(vc1dsp_mmx.o): relocation R_X86_64_PC32 against symbol `ff_pw_9’ can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:311: lib/libopencv_videoio.so.4.5.2] Error 1
make[1]: *** [CMakeFiles/Makefile2:6716: modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
make: *** [Makefile:163: all] Error 2

or check out the tag by name, whatever hash it has:
git checkout tags/n4.4
or optionally create a branch, if “detached head” seems inconvenient:
git checkout tags/n4.4 -b tagged-n4.4

looks like your build directory still has some .a files, which are for static linking. I think you’re supposed to build ffmpeg as a shared library (.so files). OpenCV usually wants ffmpeg like that.

Thanks, I also stumbled into that page. Doesn’t solve the issue though :grimacing:.

those go into ffmpeg’s build, i.e. its ./configure step.

Ugh,… don’t know what I was thinking!

SOLUTION:
So for ffmpeg I did:
./configure --enable-shared --disable-static --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64

Then, for openCV I did:
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-master/modules -DCMAKE_C_COMPILER=$(which gcc-8) -DCMAKE_CXX_COMPILER=$(which g++-8) -DWITH_CUDA=ON -DWITH_OPENGL=ON ../opencv-master

Finally I get a [100%] again! Thanks a ton!

1 Like

RESOLVED! Thanks again :slightly_smiling_face:

SOLUTION IN SHORT:
For now (until openCV resolves the issue) use ffmpeg v4.4 and if you get an error message with ff_pw_9, include --enable-shared --disable-static in the ./configure step.