20a21,24
> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
> 
33a38,53
> #include <stdint.h>
> #include "libavutil/common.h"
> #include "libavutil/dict.h"
> #include "libavutil/log.h"
> #include "libavformat/version.h"
> #include "libavformat/avio.h"
> extern "C" {
> #include <libswscale/swscale.h>
> }
> #include <SDL.h>
> #include <SDL_thread.h>
> 
> #define SDL_AUDIO_BUFFER_SIZE 4096
> #define MAX_AUDIO_FRAME_SIZE 192000
> 
> 
64a85,88
> static bool play_video;          /* JWT: TRUE IF USER IS CURRENTLY PLAYING VIDEO (KILLINV VID. WINDOW TURNS OFF! */
> static bool play_video_wason;    /* JWT: TRUE IF USER WANTS VIDEO PART OF VIDEO FILES DISPLAYED */
> static bool fromstdin;           /* JWT: TRUE IF INPUT IS STREAMED IN VIA STDIN, FALSE OTHERWISE */
> 
72a97,98
> static CodecInfo cinfo;          /* JWT: SAVE THE AUDIO & VIDEO CODEC STRUCTURES SINCE NOW REFERENCED IN 2 FUNCTIONS: */
> static CodecInfo vcinfo;
140a167
>     play_video_wason = aud_get_bool ("ffaudio", "play_video");
241c268
< static AVFormatContext * open_input_file (const char * name, VFSFile & file)
---
> bool isFromStdin ()  /* JWT: TRUE IF INPUT IS STREAMED IN VIA STDIN, FALSE OTHERWISE */
243,253c270,271
<     AVInputFormat * f = get_format (name, file);
< 
<     if (! f)
<     {
<         AUDERR ("Unknown format for %s.\n", name);
<         return nullptr;
<     }
< 
<     AVFormatContext * c = avformat_alloc_context ();
<     AVIOContext * io = io_context_new (file);
<     c->pb = io;
---
>     return fromstdin;
> }
255c273,275
<     int ret = avformat_open_input (& c, name, f, nullptr);
---
> static AVFormatContext * open_input_file (const char * name, VFSFile & file)
> {
>     AVFormatContext * c = NULL;
257c277,282
<     if (ret < 0)
---
>     play_video = aud_get_bool ("ffaudio", "play_video");   //JWT:RESET PLAY-VIDEO, CASE TURNED OFF ON PREV. PLAY
>     const char * xname = name;
>     if (fromstdin)
>         xname = "pipe:";
>     int ret = avformat_open_input (&c, xname, NULL, NULL);
>     if (ret)
259,260c284
<         AUDERR ("avformat_open_input failed for %s: %s.\n", name, ffaudio_strerror (ret));
<         io_context_free (io);
---
>         AUDERR ("avformat_open_input failed for %s: %s.\n", xname, ffaudio_strerror (ret));
267c291
< static void close_input_file (AVFormatContext * c)
---
> static void close_input_file (AVFormatContext * c, CodecInfo * cinfo, CodecInfo * vcinfo)
269,270c293,297
<     AVIOContext * io = c->pb;
< 
---
>     if (play_video_wason) {
>         avcodec_close(vcinfo->context);
>     }
>     play_video_wason = play_video;
>     avcodec_close(cinfo->context);
277d303
<     io_context_free (io);
280c306
< static bool find_codec (AVFormatContext * c, CodecInfo * cinfo)
---
> static bool find_codec (AVFormatContext * c, CodecInfo * cinfo, CodecInfo * vcinfo)
282c308
<     avformat_find_stream_info (c, nullptr);
---
>     avformat_find_stream_info (c, NULL);
283a310,311
>     int videoStream=-1;
>     int audioStream=-1;
286c314,320
<         AVStream * stream = c->streams[i];
---
>         if (c->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
>             videoStream=i;
>         else if (c->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO && audioStream < 0)
>             audioStream=i;
>     }
>     if (audioStream==-1)   //PUNT IF NO AUDIO SINCE AUDACIOUS IS AN *AUDIO* PLAYER!
>         return false;
288,290c322,328
<         if (stream && stream->codec && stream->codec->codec_type == AVMEDIA_TYPE_AUDIO)
<         {
<             AVCodec * codec = avcodec_find_decoder (stream->codec->codec_id);
---
>     AVCodec * codec = avcodec_find_decoder (c->streams[audioStream]->codec->codec_id);
>     if (codec)
>     {
>         cinfo->stream_idx = audioStream;
>         cinfo->stream = c->streams[audioStream];
>         cinfo->context = c->streams[audioStream]->codec;
>         cinfo->codec = codec;
292c330,334
<             if (codec)
---
>         //JWT:NOW SEE IF WE GOT A VIDEO STREAM TOO:
>         if (play_video && videoStream >= 0)
>         {
>             AVCodec * vcodec = avcodec_find_decoder (c->streams[videoStream]->codec->codec_id);
>             if (vcodec)
294,299c336,339
<                 cinfo->stream_idx = i;
<                 cinfo->stream = stream;
<                 cinfo->context = stream->codec;
<                 cinfo->codec = codec;
< 
<                 return true;
---
>                 vcinfo->stream_idx = videoStream;
>                 vcinfo->stream = c->streams[videoStream];
>                 vcinfo->context = c->streams[videoStream]->codec;
>                 vcinfo->codec = vcodec;
301a342
>         return true;
351,353c392,393
<     AVFormatContext * ic = open_input_file (filename, file);
< 
<     if (ic)
---
>     fromstdin = strstr(filename, "://-.") ? true : false;
>     if (fromstdin)
355c395,399
<         CodecInfo cinfo;
---
>         tuple.set_filename (filename);
>     }
>     else   /* JWT:THIS STUFF DEFERRED UNTIL PLAY FOR STDIN, BUT SEEMS TO HAVE TO BE HERE FOR DIRECT */
>     {
>         AVFormatContext * ic = open_input_file (filename, file);
357c401
<         if (find_codec (ic, & cinfo))
---
>         if (ic)
359,362c403,405
<             tuple.set_filename (filename);
< 
<             tuple.set_int (Tuple::Length, ic->duration / 1000);
<             tuple.set_int (Tuple::Bitrate, ic->bit_rate / 1000);
---
>             if (find_codec (ic, & cinfo, & vcinfo))
>             {
>                 tuple.set_filename (filename);
364,365c407,416
<             if (cinfo.codec->long_name)
<                 tuple.set_str (Tuple::Codec, cinfo.codec->long_name);
---
>                 tuple.set_int (Tuple::Length, ic->duration / 1000);
>                 tuple.set_int (Tuple::Bitrate, ic->bit_rate / 1000);
>                 if (cinfo.codec->long_name)
>                     tuple.set_str (Tuple::Codec, cinfo.codec->long_name);
> 
>                 if (ic->metadata)
>                     read_metadata_dict (tuple, ic->metadata);
>                 if (cinfo.stream->metadata)
>                     read_metadata_dict (tuple, cinfo.stream->metadata);
>             }
367,370c418
<             if (ic->metadata)
<                 read_metadata_dict (tuple, ic->metadata);
<             if (cinfo.stream->metadata)
<                 read_metadata_dict (tuple, cinfo.stream->metadata);
---
>             close_input_file (ic, & cinfo, & vcinfo);
373c421,422
<         close_input_file (ic);
---
>         if (tuple && ! file.fseek (0, VFS_SEEK_SET))
>             audtag::tuple_read (tuple, file);
375,378c424
< 
<     if (tuple && ! file.fseek (0, VFS_SEEK_SET))
<         audtag::tuple_read (tuple, file);
< 
---
> AUDDBG ("---- returning tuple.\n");
404c450
<     bool codec_opened = false;
---
>     /* bool codec_opened = false; */
407a454,462
>   SDL_Overlay     *bmp = nullptr;
>   SDL_Surface     *screen = nullptr;
>   SDL_Rect        rect;
>   SDL_Event       event;
>   int video_width = 0;
>   int video_height = 0;
>   play_video_wason = play_video;
>   float video_aspect_ratio = 0;
>     struct SwsContext *sws_ctx = NULL;
412,417c467,468
<     if (! ic)
<         return false;
< 
<     CodecInfo cinfo;
< 
<     if (! find_codec (ic, & cinfo))
---
>     const char * fileext = strrchr (filename, '.');
>     if (ic)
419,420c470,502
<         AUDERR ("No codec found for %s.\n", filename);
<         goto error_exit;
---
>         if (fromstdin)  /* JWT:FOR STDIN: TRY TO GET "read_tuple()" STUFF NOW, SINCE FILE COULD NOT BE READ EARLIER IN read_tuple()! */
>         {
>             Tuple tuple;
> AUDDBG ("---- playing from STDIN: get TUPLE stuff now: IC is defined\n");
>             if (find_codec (ic, & cinfo, & vcinfo))
>             {
>                 tuple.set_filename (filename);
> 
>                 tuple.set_int (Tuple::Length, ic->duration / 1000);
>                 tuple.set_int (Tuple::Bitrate, ic->bit_rate / 1000);
>                 if (cinfo.codec->long_name)
>                     tuple.set_str (Tuple::Codec, cinfo.codec->long_name);
> 
>                 if (ic->metadata)
>                     read_metadata_dict (tuple, ic->metadata);
>                 if (cinfo.stream->metadata)
>                     read_metadata_dict (tuple, cinfo.stream->metadata);
>                 set_playback_tuple (tuple.ref ());
>             }
>             else
>             {
>                 AUDERR ("No codec found for %s.\n", filename);
>                 goto error_exit;
>             }
>         }
>         else
>         {
>             if (! find_codec (ic, & cinfo, & vcinfo))
>             {
>                 AUDERR ("No codec found for %s.\n", filename);
>                 goto error_exit;
>             }
>         }
421a504,505
>     else
>         return false;
425c509
<     if (avcodec_open2 (cinfo.context, cinfo.codec, nullptr) < 0)
---
>     if (avcodec_open2 (cinfo.context, cinfo.codec, NULL) < 0)
426a511,526
>     if (play_video)   //JWT:abUSER ALSO WANTS TO PLAY VIDEO, SO SET UP POP-UP VIDEO SCREEN:
>     {
>         String video_windowtitle;
>         int video_sws_scale = 0;
>         if (avcodec_open2 (vcinfo.context, vcinfo.codec, NULL) < 0)
>             goto error_exit;
> 
>         // Allocate a place to put our YUV image on that screen (initialize to video's size,
>         // then (later) resize to user's size prefs if specified (must do this way to get video to scale:
> #ifndef __DARWIN__
>         screen = SDL_SetVideoMode(vcinfo.context->width, vcinfo.context->height, 0, SDL_RESIZABLE);
> AUDERR("--DARWIN--\n");
> #else
>         screen = SDL_SetVideoMode(vcinfo.context->width, vcinfo.context->height, 24, SDL_RESIZABLE);
> AUDERR("--NOT Darwin--\n");
> #endif
428c528,611
<     codec_opened = true;
---
>         video_windowtitle = aud_get_str ("ffaudio", "video_windowtitle");
>         if (video_windowtitle)
>             SDL_WM_SetCaption(video_windowtitle, NULL);
>         if (aud_get_int ("ffaudio", "video_sws_scale"))
>             video_sws_scale = aud_get_int ("ffaudio", "video_sws_scale");
>         else
>             video_sws_scale = SWS_BICUBIC;  //default=4
> 
>         bmp = SDL_CreateYUVOverlay(
>             vcinfo.context->width,
>             vcinfo.context->height,
>             SDL_YV12_OVERLAY,
>             screen
>         );
> AUDERR("-sws_scale=%d=\n", video_sws_scale);
>         sws_ctx = sws_getContext (
>           	 vcinfo.context->width,
>           	 vcinfo.context->height,
>           	 vcinfo.context->pix_fmt,
>           	 vcinfo.context->width,
>         	   vcinfo.context->height,
>           	 PIX_FMT_YUV420P,
>           	 video_sws_scale,
>           	 NULL,
>           	 NULL,
>         	   NULL
>         );
> 
>         //NOW CALCULATE THE WIDTH, HEIGHT, & ASPECT BASED ON VIDEO'S SIZE & AND ANY USER PARAMATERS GIVEN:
>         video_aspect_ratio = vcinfo.context->height
>             ? (float)vcinfo.context->width / (float)vcinfo.context->height : 1.0;
> AUDDBG("-BEF: wxh =%d=%d= aspect=%f=\n", vcinfo.context->width, vcinfo.context->height, video_aspect_ratio);
>         int vx = aud_get_int ("ffaudio", "video_xsize");
>         int vy = aud_get_int ("ffaudio", "video_ysize");
>         if (! strcmp_nocase (fileext, ".flv"))   //JWT:FIXME:FLV STREAMS ERROR OUT ON ATTEMPT TO RESIZE?!
>             vx = vy = 0;
>         if (vx && !vy)   //user specified width only, calc. height based on aspect:
>         {
>             vcinfo.context->width = video_width = vx;
>             vcinfo.context->height = video_height = (int)((float)vx / video_aspect_ratio);
>         }
>         else if (!vx && vy)   //user specified height only, calc. height based on aspect:
>         {
>             vcinfo.context->height = video_height = vy;
>             vcinfo.context->width = video_width = (int)((float)vy * video_aspect_ratio);
>             
>         }
>         else if (vx && vy)   //user specified fixed width and height:
>         {
>             vcinfo.context->width = video_width = vx;
>             vcinfo.context->height = video_height = vy;
>         }
>         else   //user specified nothing, use video's desired wXh:
>         {
>             video_width = vcinfo.context->width;
>             video_height = vcinfo.context->height;
>         }
>         video_aspect_ratio = video_height
>             ? (float)video_width / (float)video_height : 1.0;   //fall thru to square to avoid possibliity of "/0"!
> 
>         //NOW "RESIZE" screen to user's wXh, if user set something:
>         if (vx || vy)
>         {
> #ifndef __DARWIN__
>             screen = SDL_SetVideoMode(video_width, video_height, 0, SDL_RESIZABLE);
> #else
>             screen = SDL_SetVideoMode(video_width, video_height, 24, SDL_RESIZABLE);
> #endif
> 
> AUDDBG("-AFT: wxh =%d=%d= =%d=%d= aspect=%f=\n", vcinfo.context->width, vcinfo.context->height, video_width, video_height, video_aspect_ratio);
>             if(!screen) {
>                 AUDERR("SDL: could not re-set video mode - exiting\n");
>                 goto error_exit;
>             }
>         }
>         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)) {
>             AUDERR("Could not initialize SDL - %s\n", SDL_GetError());
>             goto error_exit;
>         }
>         rect.x = 0;
>         rect.y = 0;
>     }
> 
>     /* codec_opened = true; */
448c631
<     AUDDBG("opening audio output\n");
---
>     AUDDBG("opening audio output - bitrate=%d=\n", ic->bit_rate);
450a634
> 
454a639,640
> //AUDDBG ("--starting loop\n");
>     int seek_value, ret, frameFinished, decoded, len, size;
457c643
<         int seek_value = check_seek ();
---
>         seek_value = check_seek ();
460a647,648
> AUDDBG("-----POSITIVE SEEK (%d)!\n", seek_value);
>             SDL_Delay(100);
472d659
<         int ret;
497,498c684,688
<             av_free_packet(&pkt);
<             continue;
---
>             if (!play_video || pkt.stream_index != vcinfo.stream_idx)
>             {
>                 av_free_packet(&pkt);
>                 continue;
>             }
502a693
>         frameFinished = 0;
506c697
<             if (seek_value < 0)
---
>             if (seek_value < 0) {
508,509c699,702
< 
<             if (seek_value >= 0)
---
>             }
>             if (seek_value >= 0) {
> AUDDBG("--SEEK_VALUE >= 0 (%d), *** EXEC BREAK! ***\n", seek_value);
>                 SDL_Delay(50);
510a704
>             }
517,518d710
<             int decoded = 0;
<             int len = avcodec_decode_audio4 (cinfo.context, frame, & decoded, & tmp);
520c712,713
<             if (len < 0)
---
>             decoded = 0;
>             if (pkt.stream_index == cinfo.stream_idx)  //PROCESS AUDIO PACKETS:
522,524c715,721
<                 AUDERR ("decode_audio() failed, code %d\n", len);
<                 break;
<             }
---
> //AUDDBG("-audio frame\n");
>                 len = avcodec_decode_audio4 (cinfo.context, frame, & decoded, & tmp);
>                 if (len < 0)
>                 {
>                     AUDERR ("decode_audio() failed, code %d\n", len);
>                     break;
>                 }
526,527c723,724
<             tmp.size -= len;
<             tmp.data += len;
---
>                 tmp.size -= len;
>                 tmp.data += len;
529,530c726,737
<             if (! decoded)
<                 continue;
---
>                 if (! decoded)
>                 {
> #if CHECK_LIBAVCODEC_VERSION (55, 45, 101, 55, 28, 1)
>                     av_frame_free (& frame);
> #elif CHECK_LIBAVCODEC_VERSION (54, 59, 100, 54, 28, 0)
>                     avcodec_free_frame (& frame);
> #else
>                     av_free (frame);
> #endif
> AUDERR("****** CONTINUING! ******\n");  /* JWT:NOT SURE WHY THIS IS A CONTINUE INSTEAD OF A BREAK?! */
>                     continue;
>                 }
532c739
<             int size = FMT_SIZEOF (out_fmt) * cinfo.context->channels * frame->nb_samples;
---
>                 size = FMT_SIZEOF (out_fmt) * cinfo.context->channels * frame->nb_samples;
534c741,753
<             if (planar)
---
>                 if (planar)
>                 {
>                     if (size > buf.len ())
>                         buf.resize (size);
> 
>                     audio_interlace ((const void * *) frame->data, out_fmt,
>                      cinfo.context->channels, buf.begin (), frame->nb_samples);
>                     write_audio (buf.begin (), size);
>                 }
>                 else
>                     write_audio (frame->data[0], size);
>             }
>             else    /* JWT:PROCESS VIDEO PACKETS HERE: */
536,537c755,762
<                 if (size > buf.len ())
<                     buf.resize (size);
---
> //AUDDBG("-video frame\n");
>                 len = avcodec_decode_video2(vcinfo.context, frame, &frameFinished, &pkt);
>                 // Did we get a video frame?
>                 if (len < 0)
>                 {
>                     AUDERR ("decode_video() failed, code %d\n", len);
>                     break;
>                 }
539,541c764,783
<                 audio_interlace ((const void * *) frame->data, out_fmt,
<                  cinfo.context->channels, buf.begin (), frame->nb_samples);
<                 write_audio (buf.begin (), size);
---
>                 if (frameFinished) {
>                     SDL_LockYUVOverlay(bmp);
>                     AVPicture pict;
>                     pict.data[0] = bmp->pixels[0];
>                     pict.data[1] = bmp->pixels[2];
>                     pict.data[2] = bmp->pixels[1];
>                     pict.linesize[0] = bmp->pitches[0];
>                     pict.linesize[1] = bmp->pitches[2];
>                     pict.linesize[2] = bmp->pitches[1];
>                     // Convert the image into YUV format that SDL uses
>                     sws_scale(sws_ctx, (uint8_t const * const *)frame->data,
>                         frame->linesize, 0, vcinfo.context->height,
>                         pict.data, pict.linesize);
>                     SDL_UnlockYUVOverlay(bmp);
> 	
>                     rect.w = video_width;
>                     rect.h = video_height;
>                     SDL_DisplayYUVOverlay(bmp, &rect);
>                     break;
>                 }
543,544d784
<             else
<                 write_audio (frame->data[0], size);
553d792
<         }
554a794,838
>             if (play_video)
>             {
>                 SDL_PollEvent(&event);
>                 switch(event.type) {
>                 case SDL_QUIT:
>                     SDL_Quit();
>                     play_video = false;
>                     //goto error_exit;
>                     break;
>                 case SDL_VIDEORESIZE:
>                     //Resize the screen
>                     float new_aspect_ratio;  //FOR SOME REASON C++ REQUIRES 2 LINES HERE?!
>                     new_aspect_ratio = event.resize.h
>                         ? (float)event.resize.w / (float)event.resize.h : 1.0;
> AUDDBG("-Resize:  w=%d= h=%d=\n", vcinfo.context->width, vcinfo.context->height);
>                     if (new_aspect_ratio > video_aspect_ratio)
>                     {
>                         video_height = event.resize.h;
>       //FIXEDNOW?!                  if (video_height > vcinfo.context->height)  //JWT:SEGFAULTS OTHERWISE?!
>       //                      video_height = vcinfo.context->height;
>                         video_width = (int)(video_aspect_ratio * (float)video_height);
>                     }
>                     else
>                     {
>                         video_width = event.resize.w;
>       //                  if (video_width > vcinfo.context->width)  //JWT:SEGFAULTS OTHERWISE?!
>       //                      video_width = vcinfo.context->width;
>                         video_height = (int)((float)video_width / video_aspect_ratio);
>                     }
> AUDDBG("-RESIZE to %d x %d, %d x %d! aspect=%f=\n", event.resize.w, event.resize.h, video_width, video_height, video_aspect_ratio);
> 
> #ifndef __DARWIN__
>                     screen = SDL_SetVideoMode( video_width, video_height, 0, SDL_RESIZABLE );
> #else
>                     screen = SDL_SetVideoMode( video_width, video_height, 24, SDL_RESIZABLE );
> #endif
> 
>                     //If there's an error
>                     if( screen == NULL )
>                         break;
>                 default:
>                     break;
>                 }
>             }
>         }
559a844,846
> AUDDBG("-at error exit...\n");
>     if (play_video)
>         SDL_Quit();
562,563d848
<     if (codec_opened)
<         avcodec_close(cinfo.context);
565c850
<         close_input_file(ic);
---
>         close_input_file(ic, & cinfo, & vcinfo);
