forked from mpv-player/mpv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_pvr.c
1615 lines (1357 loc) · 46.3 KB
/
stream_pvr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* stream layer for hardware MPEG 1/2/4 encoders a.k.a PVR
* (such as WinTV PVR-150/250/350/500 (a.k.a IVTV), pvrusb2 and cx88)
* See http://ivtvdriver.org/index.php/Main_Page for more details on the
* cards supported by the ivtv driver.
*
* Copyright (C) 2006 Benjamin Zores
* Copyright (C) 2007 Sven Gothel (channel navigation)
*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <inttypes.h>
#include <poll.h>
#include <linux/videodev2.h>
#include <libavutil/common.h>
#include <libavutil/avstring.h>
#include "osdep/io.h"
#include "common/msg.h"
#include "common/common.h"
#include "options/options.h"
#include "stream.h"
#include "tv.h"
#include "frequencies.h"
#define PVR_DEFAULT_DEVICE "/dev/video0"
#define PVR_MAX_CONTROLS 10
/* audio codec mode */
#define PVR_AUDIO_MODE_ARG_STEREO "stereo"
#define PVR_AUDIO_MODE_ARG_JOINT_STEREO "joint_stereo"
#define PVR_AUDIO_MODE_ARG_DUAL "dual"
#define PVR_AUDIO_MODE_ARG_MONO "mono"
/* video codec bitrate mode */
#define PVR_VIDEO_BITRATE_MODE_ARG_VBR "vbr"
#define PVR_VIDEO_BITRATE_MODE_ARG_CBR "cbr"
/* video codec stream type */
#define PVR_VIDEO_STREAM_TYPE_PS "ps"
#define PVR_VIDEO_STREAM_TYPE_TS "ts"
#define PVR_VIDEO_STREAM_TYPE_MPEG1 "mpeg1"
#define PVR_VIDEO_STREAM_TYPE_DVD "dvd"
#define PVR_VIDEO_STREAM_TYPE_VCD "vcd"
#define PVR_VIDEO_STREAM_TYPE_SVCD "svcd"
#define PVR_STATION_NAME_SIZE 256
/* command line arguments */
struct pvr_params {
int aspect_ratio;
int sample_rate;
int audio_layer;
int audio_bitrate;
char *audio_mode;
int bitrate;
char *bitrate_mode;
int bitrate_peak;
char *stream_type;
};
#define OPT_BASE_STRUCT struct pvr_params
const struct m_sub_options stream_pvr_conf = {
.opts = (const m_option_t[]) {
OPT_INT("aspect", aspect_ratio, 0),
OPT_INT("arate", sample_rate, 0),
OPT_INT("alayer", audio_layer, 0),
OPT_INT("abitrate", audio_bitrate, 0),
OPT_STRING("amode", audio_mode, 0),
OPT_INT("vbitrate", bitrate, 0),
OPT_STRING("vmode", bitrate_mode, 0),
OPT_INT("vpeak", bitrate_peak, 0),
OPT_STRING("fmt", stream_type, 0),
{0}
},
.size = sizeof(struct pvr_params),
};
#define BUFSTRCPY(d, s) av_strlcpy(d, s, sizeof(d))
#define BUFPRINTF(d, ...) snprintf(d, sizeof(d), __VA_ARGS__)
typedef struct station_elem_s {
char name[PVR_STATION_NAME_SIZE];
int freq;
char station[PVR_STATION_NAME_SIZE];
int enabled;
int priority;
} station_elem_t;
typedef struct stationlist_s {
char name[PVR_STATION_NAME_SIZE];
station_elem_t *list;
int total; /* total number */
int used; /* used number */
int enabled; /* enabled number */
} stationlist_t;
struct pvr_t {
struct mp_log *log;
tv_param_t *tv_params;
struct pvr_params *params;
int dev_fd;
char *video_dev;
int chantab;
/* v4l2 params */
int mute;
int input;
int normid;
int brightness;
int contrast;
int hue;
int saturation;
int width;
int height;
int freq;
int chan_idx;
int chan_idx_last;
stationlist_t stationlist;
/* dups the tv_param_channel, or the url's channel param */
char *param_channel;
/* encoder params */
int aspect;
int samplerate;
int layer;
int audio_rate;
int audio_mode;
int bitrate;
int bitrate_mode;
int bitrate_peak;
int stream_type;
};
static int pvr_stream_control(struct stream *s, int cmd, void *arg);
static struct pvr_t *pvr_init(void)
{
struct pvr_t *pvr = NULL;
pvr = calloc(1, sizeof (struct pvr_t));
pvr->dev_fd = -1;
pvr->video_dev = strdup(PVR_DEFAULT_DEVICE);
pvr->chantab = 5;
/* v4l2 params */
pvr->mute = 0;
pvr->input = 0;
pvr->normid = -1;
pvr->brightness = 0;
pvr->contrast = 0;
pvr->hue = 0;
pvr->saturation = 0;
pvr->width = -1;
pvr->height = -1;
pvr->freq = -1;
pvr->chan_idx = -1;
pvr->chan_idx_last = -1;
/* set default encoding settings
* may be overlapped by user parameters
* Use VBR MPEG_PS encoding at 6 Mbps (peak at 9.6 Mbps)
* with 48 KHz L2 384 kbps audio.
*/
pvr->aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_STEREO;
pvr->bitrate = 6000000;
pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
pvr->bitrate_peak = 9600000;
pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
return pvr;
}
static void pvr_uninit(struct pvr_t *pvr)
{
if (!pvr)
return;
/* close device */
if (pvr->dev_fd != -1)
close(pvr->dev_fd);
free(pvr->video_dev);
free(pvr->stationlist.list);
free(pvr->param_channel);
free(pvr);
}
/**
* @brief Copy Constructor for stationlist
*
* @see parse_setup_stationlist
*/
static int copycreate_stationlist(struct pvr_t *pvr, stationlist_t *stationlist,
int num)
{
int i;
if (pvr->chantab < 0 || !stationlist)
return -1;
num = FFMAX(num, chanlists[pvr->chantab].count);
free(stationlist->list);
stationlist->list = NULL;
stationlist->total = 0;
stationlist->enabled = 0;
stationlist->used = 0;
stationlist->list = calloc(num, sizeof (station_elem_t));
if (!stationlist->list) {
MP_ERR(pvr, "No memory allocated for station list, giving up\n");
return -1;
}
/* transport the channel list data to our extented struct */
stationlist->total = num;
BUFSTRCPY(stationlist->name, chanlists[pvr->chantab].name);
for (i = 0; i < chanlists[pvr->chantab].count; i++) {
stationlist->list[i].station[0] = '\0'; /* no station name yet */
BUFSTRCPY(stationlist->list[i].name,
chanlists[pvr->chantab].list[i].name);
stationlist->list[i].freq = chanlists[pvr->chantab].list[i].freq;
stationlist->list[i].enabled = 1; /* default enabled */
stationlist->enabled++;
stationlist->used++;
}
return 0;
}
static int print_all_stations(struct pvr_t *pvr)
{
int i;
if (!pvr || !pvr->stationlist.list)
return -1;
for (i = 0; i < pvr->stationlist.total; i++) {
MP_VERBOSE(pvr, "%3d: [%c] channel: %8s - freq: %8d - station: %s\n",
i, (pvr->stationlist.list[i].enabled) ? 'X' : ' ',
pvr->stationlist.list[i].name, pvr->stationlist.list[i].freq,
pvr->stationlist.list[i].station);
}
return 0;
}
/**
* Disables all stations
*
* @see parse_setup_stationlist
*/
static void disable_all_stations(struct pvr_t *pvr)
{
int i;
for (i = 0; i < pvr->stationlist.total; i++)
pvr->stationlist.list[i].enabled = 0;
pvr->stationlist.enabled = 0;
}
/**
* Update or add a station
*
* @see parse_setup_stationlist
*/
static int set_station(struct pvr_t *pvr, const char *station,
const char *channel, int freq, int priority)
{
int i;
if (!pvr || !pvr->stationlist.list)
return -1;
if (0 >= pvr->stationlist.total || (!channel && !freq))
return -1;
/* select channel */
for (i = 0; i < pvr->stationlist.used; i++) {
if (channel && !strcasecmp(pvr->stationlist.list[i].name, channel))
break; /* found existing channel entry */
if (freq > 0 && pvr->stationlist.list[i].freq == freq)
break; /* found existing frequency entry */
}
if (i < pvr->stationlist.used) {
/**
* found an existing entry,
* which is about to change with the user data.
* it is also enabled ..
*/
if (!pvr->stationlist.list[i].enabled) {
pvr->stationlist.list[i].enabled = 1;
pvr->stationlist.enabled++;
}
if (station)
BUFSTRCPY(pvr->stationlist.list[i].station, station);
else if (channel)
BUFSTRCPY(pvr->stationlist.list[i].station, channel);
else
BUFPRINTF(pvr->stationlist.list[i].station, "F %d", freq);
pvr->stationlist.list[i].priority = priority;
MP_DBG(pvr, "Set user station channel: %8s - freq: %8d - station: %s\n",
pvr->stationlist.list[i].name,
pvr->stationlist.list[i].freq,
pvr->stationlist.list[i].station);
return 0;
}
/* from here on, we have to create a new entry, frequency is mandatory */
if (freq < 0) {
MP_ERR(pvr, "Cannot add new station/channel without frequency\n");
return -1;
}
if (pvr->stationlist.total < i) {
/**
* we have to extend the stationlist about
* an arbitrary size, even though this path is not performance critical
*/
pvr->stationlist.total += 10;
pvr->stationlist.list =
realloc(pvr->stationlist.list,
pvr->stationlist.total * sizeof (station_elem_t));
if (!pvr->stationlist.list) {
MP_ERR(pvr, "No memory allocated for station list, giving up\n");
return -1;
}
/* clear the new space ..*/
memset(&(pvr->stationlist.list[pvr->stationlist.used]), 0,
(pvr->stationlist.total - pvr->stationlist.used)
* sizeof (station_elem_t));
}
/* here we go, our actual new entry */
pvr->stationlist.used++;
pvr->stationlist.list[i].enabled = 1;
pvr->stationlist.list[i].priority = priority;
pvr->stationlist.enabled++;
if (station)
BUFSTRCPY(pvr->stationlist.list[i].station, station);
if (channel)
BUFSTRCPY(pvr->stationlist.list[i].name, channel);
else
BUFPRINTF(pvr->stationlist.list[i].name, "F %d", freq);
pvr->stationlist.list[i].freq = freq;
MP_DBG(pvr, "Add user station channel: %8s - freq: %8d - station: %s\n",
pvr->stationlist.list[i].name,
pvr->stationlist.list[i].freq,
pvr->stationlist.list[i].station);
return 0;
}
static int compare_priority(const void *pa, const void *pb)
{
const station_elem_t *a = pa;
const station_elem_t *b = pb;
if (a->priority < b->priority)
return -1;
if (a->priority > b->priority)
return 1;
return 0;
}
/**
* Here we set our stationlist, as follow
* - choose the frequency channel table, e.g. ntsc-cable
* - create our stationlist, same element size as the channellist
* - copy the channellist content to our stationlist
* - IF the user provides his channel-mapping, THEN:
* - disable all stations
* - update and/or create entries in the stationlist and enable them
*/
static int parse_setup_stationlist(struct pvr_t *pvr)
{
int i;
if (!pvr)
return -1;
/* Create our station/channel list */
if (pvr->tv_params->chanlist) {
/* select channel list */
for (i = 0; chanlists[i].name != NULL; i++) {
if (!strcasecmp(chanlists[i].name, pvr->tv_params->chanlist)) {
pvr->chantab = i;
break;
}
}
if (!chanlists[i].name) {
MP_ERR(pvr, "unable to find channel list %s, using default %s\n",
pvr->tv_params->chanlist, chanlists[pvr->chantab].name);
} else {
MP_INFO(pvr, "select channel list %s, entries %d\n",
chanlists[pvr->chantab].name, chanlists[pvr->chantab].count);
}
}
if (0 > pvr->chantab) {
MP_FATAL(pvr, "No channel list selected, giving up\n");
return -1;
}
if (copycreate_stationlist(pvr, &(pvr->stationlist), -1) < 0) {
MP_FATAL(pvr, "No memory allocated for station list, giving up\n");
return -1;
}
/* Handle user channel mappings */
if (pvr->tv_params->channels) {
char channel[PVR_STATION_NAME_SIZE];
char station[PVR_STATION_NAME_SIZE];
char **channels = pvr->tv_params->channels;
disable_all_stations(pvr);
int prio = 0;
for (i = 0; i < pvr->stationlist.total; i++)
pvr->stationlist.list[i].priority = ++prio;
while (*channels) {
char *tmp = *(channels++);
char *sep = strchr(tmp, '-');
int freq = -1;
if (!sep)
continue; /* Wrong syntax, but mplayer should not crash */
BUFSTRCPY(station, sep + 1);
sep[0] = '\0';
BUFSTRCPY(channel, tmp);
while ((sep = strchr(station, '_')))
sep[0] = ' ';
/* if channel number is a number and larger than 1000 treat it as
* frequency tmp still contain pointer to null-terminated string with
* channel number here
*/
if ((freq = atoi(channel)) <= 1000)
freq = -1;
if (set_station(pvr, station, (freq <= 0) ? channel : NULL, freq,
++prio) < 0)
{
MP_ERR(pvr, "Unable to set user station channel: %8s - "
"freq: %8d - station: %s\n", channel, freq, station);
}
}
qsort(pvr->stationlist.list, pvr->stationlist.total,
sizeof(station_elem_t), compare_priority);
}
return print_all_stations(pvr);
}
static int get_v4l2_freq(struct pvr_t *pvr)
{
int freq;
struct v4l2_frequency vf;
struct v4l2_tuner vt;
if (!pvr)
return -1;
if (pvr->dev_fd < 0)
return -1;
memset(&vt, 0, sizeof (vt));
memset(&vf, 0, sizeof (vf));
if (ioctl(pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0) {
MP_ERR(pvr, "can't set tuner (%s).\n", mp_strerror(errno));
return -1;
}
if (ioctl(pvr->dev_fd, VIDIOC_G_FREQUENCY, &vf) < 0) {
MP_ERR(pvr, "can't get frequency (%s).\n", mp_strerror(errno));
return -1;
}
freq = vf.frequency;
if (!(vt.capability & V4L2_TUNER_CAP_LOW))
freq *= 1000;
freq /= 16;
return freq;
}
static int set_v4l2_freq(struct pvr_t *pvr)
{
struct v4l2_frequency vf;
struct v4l2_tuner vt;
if (!pvr)
return -1;
if (0 >= pvr->freq) {
MP_ERR(pvr, "Frequency invalid %d !!!\n", pvr->freq);
return -1;
}
/* don't set the frequency, if it's already set.
* setting it here would interrupt the stream.
*/
if (get_v4l2_freq(pvr) == pvr->freq) {
MP_INFO(pvr, "Frequency %d already set.\n", pvr->freq);
return 0;
}
if (pvr->dev_fd < 0)
return -1;
memset(&vf, 0, sizeof (vf));
memset(&vt, 0, sizeof (vt));
if (ioctl(pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0) {
MP_ERR(pvr, "can't get tuner (%s).\n", mp_strerror(errno));
return -1;
}
vf.type = vt.type;
vf.frequency = pvr->freq * 16;
if (!(vt.capability & V4L2_TUNER_CAP_LOW))
vf.frequency /= 1000;
if (ioctl(pvr->dev_fd, VIDIOC_S_FREQUENCY, &vf) < 0) {
MP_ERR(pvr, "can't set frequency (%s).\n", mp_strerror(errno));
return -1;
}
memset(&vt, 0, sizeof(vt));
if (ioctl(pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0) {
MP_ERR(pvr, "can't set tuner (%s).\n", mp_strerror(errno));
return -1;
}
/* just a notification */
if (!vt.signal) {
MP_ERR(pvr, "NO SIGNAL at frequency %d (%d)\n", pvr->freq, vf.frequency);
} else {
MP_INFO(pvr, "Got signal at frequency %d (%d)\n", pvr->freq,
vf.frequency);
}
return 0;
}
static int set_station_by_step(struct pvr_t *pvr, int step, int v4lAction)
{
if (!pvr || !pvr->stationlist.list)
return -1;
if (pvr->stationlist.enabled >= abs(step)) {
int gotcha = 0;
int chidx = pvr->chan_idx + step;
while (!gotcha) {
chidx = (chidx + pvr->stationlist.used) % pvr->stationlist.used;
MP_DBG(pvr, "Offset switch: current %d, enabled %d, step %d -> %d\n",
pvr->chan_idx, pvr->stationlist.enabled, step,
chidx);
if (!pvr->stationlist.list[chidx].enabled) {
MP_DBG(pvr, "Switch disabled to user station channel: %8s - "
"freq: %8d - station: %s\n",
pvr->stationlist.list[chidx].name,
pvr->stationlist.list[chidx].freq,
pvr->stationlist.list[chidx].station);
chidx += FFSIGN(step);
} else {
gotcha = 1;
}
}
pvr->freq = pvr->stationlist.list[chidx].freq;
pvr->chan_idx_last = pvr->chan_idx;
pvr->chan_idx = chidx;
MP_INFO(pvr, "Switch to user station channel: %8s - freq: %8d - station: %s\n",
pvr->stationlist.list[chidx].name,
pvr->stationlist.list[chidx].freq,
pvr->stationlist.list[chidx].station);
if (v4lAction)
return set_v4l2_freq(pvr);
return (pvr->freq > 0) ? 0 : -1;
}
MP_ERR(pvr, "Ooops couldn't set freq by channel entry step %d to "
"current %d, enabled %d\n",
step, pvr->chan_idx, pvr->stationlist.enabled);
return -1;
}
static int set_station_by_channelname_or_freq(struct pvr_t *pvr,
const char *channel,
int freq, int v4lAction)
{
int i = 0;
if (!pvr || !pvr->stationlist.list)
return -1;
if (0 >= pvr->stationlist.enabled) {
MP_WARN(pvr, "No enabled station, cannot switch channel/frequency\n");
return -1;
}
if (channel) {
/* select by channel */
for (i = 0; i < pvr->stationlist.used; i++) {
if (!strcasecmp(pvr->stationlist.list[i].name, channel)) {
if (!pvr->stationlist.list[i].enabled) {
MP_WARN(pvr, "Switch disabled to user station channel: %8s "
"- freq: %8d - station: %s\n",
pvr->stationlist.list[i].name,
pvr->stationlist.list[i].freq,
pvr->stationlist.list[i].station);
return -1;
}
pvr->freq = pvr->stationlist.list[i].freq;
pvr->chan_idx_last = pvr->chan_idx;
pvr->chan_idx = i;
break;
}
}
} else if (freq >= 0) {
/* select by freq */
for (i = 0; i < pvr->stationlist.used; i++) {
if (pvr->stationlist.list[i].freq == freq) {
if (!pvr->stationlist.list[i].enabled) {
MP_WARN(pvr, "Switch disabled to user station channel: "
"%8s - freq: %8d - station: %s\n",
pvr->stationlist.list[i].name,
pvr->stationlist.list[i].freq,
pvr->stationlist.list[i].station);
return -1;
}
pvr->freq = pvr->stationlist.list[i].freq;
pvr->chan_idx_last = pvr->chan_idx;
pvr->chan_idx = i;
break;
}
}
}
if (i >= pvr->stationlist.used) {
if (channel) {
MP_WARN(pvr, "unable to find channel %s\n", channel);
} else {
MP_WARN(pvr, "unable to find frequency %d\n", freq);
}
return -1;
}
MP_INFO(pvr, "Switch to user station channel: %8s - freq: %8d - station: %s\n",
pvr->stationlist.list[i].name,
pvr->stationlist.list[i].freq,
pvr->stationlist.list[i].station);
if (v4lAction)
return set_v4l2_freq(pvr);
return (pvr->freq > 0) ? 0 : -1;
}
static void parse_encoder_options(struct pvr_t *pvr)
{
if (!pvr)
return;
/* -pvr aspect=digit */
if (pvr->params->aspect_ratio >= 0 && pvr->params->aspect_ratio <= 3)
pvr->aspect = pvr->params->aspect_ratio;
/* -pvr arate=x */
if (pvr->params->sample_rate != 0) {
switch (pvr->params->sample_rate) {
case 32000:
pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
break;
case 44100:
pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
break;
case 48000:
pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
break;
default:
break;
}
}
/* -pvr alayer=x */
if (pvr->params->audio_layer == 1) {
pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
} else if (pvr->params->audio_layer == 2) {
pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
} else if (pvr->params->audio_layer == 3) {
pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_3;
}
/* -pvr abitrate=x */
if (pvr->params->audio_bitrate != 0) {
if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_1) {
switch (pvr->params->audio_bitrate) {
case 32:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_32K;
break;
case 64:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_64K;
break;
case 96:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_96K;
break;
case 128:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_128K;
break;
case 160:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_160K;
break;
case 192:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_192K;
break;
case 224:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_224K;
break;
case 256:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_256K;
break;
case 288:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_288K;
break;
case 320:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_320K;
break;
case 352:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_352K;
break;
case 384:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_384K;
break;
case 416:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_416K;
break;
case 448:
pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_448K;
break;
default:
break;
}
} else if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_2) {
switch (pvr->params->audio_bitrate) {
case 32:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_32K;
break;
case 48:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_48K;
break;
case 56:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_56K;
break;
case 64:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_64K;
break;
case 80:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_80K;
break;
case 96:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_96K;
break;
case 112:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_112K;
break;
case 128:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_128K;
break;
case 160:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_160K;
break;
case 192:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_192K;
break;
case 224:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_224K;
break;
case 256:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
break;
case 320:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_320K;
break;
case 384:
pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
break;
default:
break;
}
} else if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_3) {
switch (pvr->params->audio_bitrate) {
case 32:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_32K;
break;
case 40:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_40K;
break;
case 48:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_48K;
break;
case 56:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_56K;
break;
case 64:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_64K;
break;
case 80:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_80K;
break;
case 96:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_96K;
break;
case 112:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_112K;
break;
case 128:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_128K;
break;
case 160:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_160K;
break;
case 192:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_192K;
break;
case 224:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_224K;
break;
case 256:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_256K;
break;
case 320:
pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_320K;
break;
default:
break;
}
}
}
/* -pvr amode=x */
if (pvr->params->audio_mode) {
if (!strcmp(pvr->params->audio_mode, PVR_AUDIO_MODE_ARG_STEREO)) {
pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_STEREO;
} else if (!strcmp(pvr->params->audio_mode,
PVR_AUDIO_MODE_ARG_JOINT_STEREO))
{
pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
} else if (!strcmp(pvr->params->audio_mode, PVR_AUDIO_MODE_ARG_DUAL)) {
pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_DUAL;
} else if (!strcmp(pvr->params->audio_mode, PVR_AUDIO_MODE_ARG_MONO)) {
pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_MONO;
}
}
/* -pvr vbitrate=x */
if (pvr->params->bitrate)
pvr->bitrate = pvr->params->bitrate;
/* -pvr vmode=x */
if (pvr->params->bitrate_mode) {
if (!strcmp(pvr->params->bitrate_mode, PVR_VIDEO_BITRATE_MODE_ARG_VBR)) {
pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
} else if (!strcmp(pvr->params->bitrate_mode,
PVR_VIDEO_BITRATE_MODE_ARG_CBR))
{
pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
}
}
/* -pvr vpeak=x */
if (pvr->params->bitrate_peak)
pvr->bitrate_peak = pvr->params->bitrate_peak;
/* -pvr fmt=x */
if (pvr->params->stream_type) {
if (!strcmp(pvr->params->stream_type, PVR_VIDEO_STREAM_TYPE_PS)) {
pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
} else if (!strcmp(pvr->params->stream_type, PVR_VIDEO_STREAM_TYPE_TS)) {
pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
} else if (!strcmp(pvr->params->stream_type, PVR_VIDEO_STREAM_TYPE_MPEG1)) {
pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG1_SS;
} else if (!strcmp(pvr->params->stream_type, PVR_VIDEO_STREAM_TYPE_DVD)) {
pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
} else if (!strcmp(pvr->params->stream_type, PVR_VIDEO_STREAM_TYPE_VCD)) {
pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG1_VCD;
} else if (!strcmp(pvr->params->stream_type, PVR_VIDEO_STREAM_TYPE_SVCD)) {
pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD;
}
}
}
static void add_v4l2_ext_control(struct v4l2_ext_control *ctrl,
uint32_t id, int32_t value)
{
ctrl->id = id;
ctrl->value = value;
}
static int set_encoder_settings(struct pvr_t *pvr)
{
struct v4l2_ext_control *ext_ctrl = NULL;
struct v4l2_ext_controls ctrls;
uint32_t count = 0;
if (!pvr)
return -1;
if (pvr->dev_fd < 0)
return -1;
ext_ctrl = malloc(PVR_MAX_CONTROLS * sizeof (struct v4l2_ext_control));
add_v4l2_ext_control(&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_ASPECT,
pvr->aspect);
add_v4l2_ext_control(&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
pvr->samplerate);
add_v4l2_ext_control(&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_ENCODING,
pvr->layer);
switch (pvr->layer) {
case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
add_v4l2_ext_control(&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L1_BITRATE,
pvr->audio_rate);
break;
case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
add_v4l2_ext_control(&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L2_BITRATE,
pvr->audio_rate);
break;
case V4L2_MPEG_AUDIO_ENCODING_LAYER_3:
add_v4l2_ext_control(&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L3_BITRATE,
pvr->audio_rate);
break;
default:
break;